2

Does iTextPdf allow to set spacing between cells in table?

I have a table with 2 columns and I am trying to draw a border-bottom on cell. I want space between each border same as cell padding.

I am using below code:

    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);

How do I do this?

4

1 回答 1

3

你可能想要这个效果:

在此处输入图像描述

我的书中对此进行了解释,更具体地说,在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,然后在您的实现中添加或减去p / 2单元格尺寸。PdfPCellEvent

于 2015-04-01T12:52:28.973 回答