2015-04-01 203 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

相關問題