2015-04-01 205 views
2

iTextPdf允许设置表格中单元格之间的间距吗?iTextPdf中单元格之间可能有空格吗?

我有一个2列的表,我试图在单元格上绘制边框底部。 我想每个边框之间的空格相同单元格填充。

我使用下面的代码:

PdfPTable table = new PdfPTable(2); 
    table.setTotalWidth(95f); 
    table.setWidths(new float[]{0.5f,0.5f}); 
    table.setHorizontalAlignment(Element.ALIGN_CENTER); 

    Font fontNormal10 = new Font(FontFamily.TIMES_ROMAN, 10, Font.NORMAL); 
    PdfPCell cell = new PdfPCell(new Phrase("Performance", fontNormal10)); 
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
    cell.setHorizontalAlignment(Element.ALIGN_LEFT); 
    cell.setBorder(Rectangle.BOTTOM); 
    cell.setPaddingLeft(10f); 
    cell.setPaddingRight(10f); 

    table.addCell(cell); 
    table.addCell(cell); 
    table.addCell(cell); 
    table.addCell(cell); 

    document.add(table); 

我该怎么办呢?

回答

3

你可能想要这个效果:

enter image description here

即在my book解释,更特别是在PressPreviews例子。

您需要先删除边框:

cell.setBorder(PdfPCell.NO_BORDER); 

而且你需要自己绘制边框的细胞事件:

public class MyBorder implements PdfPCellEvent { 
    public void cellLayout(PdfPCell cell, Rectangle position, 
     PdfContentByte[] canvases) { 
     float x1 = position.getLeft() + 2; 
     float x2 = position.getRight() - 2; 
     float y1 = position.getTop() - 2; 
     float y2 = position.getBottom() + 2; 
     PdfContentByte canvas = canvases[PdfPTable.LINECANVAS]; 
     canvas.rectangle(x1, y1, x2 - x1, y2 - y1); 
     canvas.stroke(); 
    } 
} 

你声明的细胞事件,这样的细胞:

cell.setCellEvent(new MyBorder()); 

在我的示例中,我从单元格的维度中添加或减去2个用户单位。在你的情况下,你可以定义一个填充p,然后在你的PdfPCellEvent实现中从单元尺寸加上或减去p/2

+0

@Jens任何想法如何我只能显示上面的例子中的内部矩形,不包括外部表? – ericleit 2016-07-25 16:36:53

+0

@ericleit“内部矩形”是什么意思? – Jens 2016-07-25 19:43:18

+0

@Jens我的意思是单元格级别的边界,而不是表级边界,但我通过将表格边界设置为0来计算出来。感谢回复! – ericleit 2016-07-26 15:43:26

相关问题