在iTextPdf中是否可以在单元格之间留出空间? [英] Is it possible to have space between cells in iTextPdf?

查看:177
本文介绍了在iTextPdf中是否可以在单元格之间留出空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iTextPdf是否允许在表格中设置单元格之间的间距?

Does iTextPdf allow to set spacing between cells in table?

我有一个包含2列的表格,我正在尝试在单元格上绘制边框底部。
我希望每个边框之间的空格与单元格填充相同。

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.

我使用下面的代码:

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

我该怎么做?

推荐答案

您可能想要这样的效果:

You probably want this effect:

我的书,更具体地说,在 PressPreviews 示例中。

That is explained in my book, more specifically in the PressPreviews example.

您需要先删除边框:

cell.setBorder(PdfPCell.NO_BORDER);

您需要自己在单元格事件中绘制边框:

And you need to draw the border yourself in a cell event:

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

您将单元格事件声明为单元格,如下所示:

You declare the cell event to the cell like this:

cell.setCellEvent(new MyBorder());

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

In my example, I add or subtract 2 user units from the cell's dimensions. In your case, you could define a padding p and then add or subtract p / 2 from the cell dimensions in your PdfPCellEvent implementation.

这篇关于在iTextPdf中是否可以在单元格之间留出空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆