在itext PdfPCell中绘制带有文本的状态栏

时间:2018-07-19 14:21:36

标签: java pdf graphics itext 2d

尝试在PdfCell中创建/绘制状态栏。 (它将显示在具有不同值的多个表的多层页面中。)

我应该在内存中创建图像并将其设置在单元格中吗? 如果要这样做,如何在内存中创建图像并将其发送到iText。我看到的所有样本都是从文件中获取的。...

iText库可以帮助实际制作带有所有文本和周围多余行的状态栏。 (两端都加长了一行。.在第一行的开头是数字0,在最后一行的上面是数字1000 ...居中文字显示状态为“ 750/1000” ...)

感谢任何想法。

添加更多信息。 这是我到目前为止尝试过的代码。不知道如何在2个单元格之间居中放置文本(将setPaddingTop推到较低的单元格没有效果...)。 我也使用缩进来使文本位于行顶部。任何改进代码的想法都欢迎。

PdfPTableExt innerTable = new PdfPTableExt();

int finalScore = 750;
//PdfPTableExt rectTable = new PdfPTableExt(new int[] {1,1,1,1});
PdfPTableExt rectTable = new PdfPTableExt(new int[] {500,finalScore,1000-finalScore,500});

// Draw cell for padding and number     
Paragraph startParagraph = new Paragraph("0");      
startParagraph.setIndentationLeft(82);
PdfPCellExt startCell = new PdfPCellExt();
startCell.addElement(startParagraph);
startCell.setFixedHeight(25);
rectTable.addCell(startCell);   

PdfPCellExt topMiddle = new PdfPCellExt();
topMiddle.setColspan(2);

Paragraph middleParagraph = new Paragraph(Integer.toString(finalScore));    
middleParagraph.setAlignment(Element.ALIGN_CENTER);
middleParagraph.setPaddingTop(30);
topMiddle.addElement(middleParagraph);        
rectTable.addCell(topMiddle);   

Paragraph limitParagraph = new Paragraph("1000");
limitParagraph.setIndentationLeft(-15);    
PdfPCellExt limitCell = new PdfPCellExt();
limitCell.addElement(limitParagraph);
rectTable.addCell(limitCell);

rectTable.completeRow();

PdfPCellExt middle1 = new PdfPCellExt();
middle1.setFixedHeight(25);
middle1.setBorder(Rectangle.RIGHT);
middle1.setBorderColorRight(BaseColor.BLACK);    
rectTable.addCell(middle1);     

// Draw Rect
PdfPCellExt middle2 = new PdfPCellExt();
Paragraph numParagraph = new Paragraph("750");
numParagraph.setAlignment(Element.ALIGN_CENTER);
middle2.addElement(numParagraph);       

middle2.setBackgroundColor(BaseColor.ORANGE);
rectTable.addCell(middle2);    

PdfPCellExt middle3 = new PdfPCellExt();
middle3.setBackgroundColor(BaseColor.BLUE);
rectTable.addCell(middle3);        

PdfPCellExt middle4 = new PdfPCellExt();
middle4.setBorder(Rectangle.LEFT);
middle4.setBorderColorLeft(BaseColor.BLACK);    
rectTable.addCell(middle4);         

    rectTable.completeRow();

// Draw empty cell for bottom padding rects
PdfPCellExt bottom1 = new PdfPCellExt();
bottom1.setFixedHeight(25f);
rectTable.addCell(bottom1);
PdfPCellExt bottom2 = new PdfPCellExt();
rectTable.addCell(bottom2); 
PdfPCellExt bottom3 = new PdfPCellExt();
rectTable.addCell(bottom3);    
PdfPCellExt bottom4 = new PdfPCellExt();
rectTable.addCell(bottom4);    
rectTable.completeRow();        

PdfPCellExt rectCell = new PdfPCellExt();      
rectCell.addElement(rectTable);
innerTable.addCell(rectCell);        

PdfPCellExt只是包装类

public class PdfPCellExt extends PdfPCell{
    public PdfPCellExt() {
    super();
    this.setPadding(PdfPadding.None);
    this.setBorder(Rectangle.NO_BORDER);
    }
}

enter image description here

0 个答案:

没有答案