使用itext在pdf中无法正确扩展表边框

使用itext在pdf中无法正确扩展表边框,pdf,itext,Pdf,Itext,我想生成pdf,其中包含带边框的表,该表中有更多数据,因此生成pdf时,它将在两个页面中生成。但问题是表格边界没有扩大 页到页,即在下一页边框(水平),上一页垂直边框再次加框,这是错误的。水平在下一页,垂直在上一页不应该出现 请查找附加的pdf文件和html文件以供参考 您想要一张这样的桌子 正如我在评论中所解释的,您需要将单元格的边框设置为无边框,或者通过更改默认单元格: table.getDefaultCell().setBorder(Rectangle.NO_BORDER); 或者通过

我想生成pdf,其中包含带边框的表,该表中有更多数据,因此生成pdf时,它将在两个页面中生成。但问题是表格边界没有扩大 页到页,即在下一页边框(水平),上一页垂直边框再次加框,这是错误的。水平在下一页,垂直在上一页不应该出现

请查找附加的pdf文件和html文件以供参考


您想要一张这样的桌子

正如我在评论中所解释的,您需要将单元格的边框设置为
无边框
,或者通过更改默认单元格:

table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
或者通过更改特定单元格的属性:

PdfPCell cell = new PdfPCell(new Phrase(TEXT));
cell.setBorder(Rectangle.NO_BORDER);
或者两者兼而有之

然后您必须创建一个表事件:

class BorderEvent implements PdfPTableEventAfterSplit {

    protected boolean bottom = true;
    protected boolean top = true;

    public void splitTable(PdfPTable table) {
        bottom = false;
    }

    public void afterSplitTable(PdfPTable table, PdfPRow startRow, int startIdx) {
        top = false;
    }

    public void tableLayout(PdfPTable table, float[][] width, float[] height,
            int headerRows, int rowStart, PdfContentByte[] canvas) {
        float widths[] = width[0];
        float y1 = height[0];
        float y2 = height[height.length - 1];
        float x1 = widths[0];
        float x2 = widths[widths.length - 1];
        PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
        cb.moveTo(x1, y1);
        cb.lineTo(x1, y2);
        cb.moveTo(x2, y1);
        cb.lineTo(x2, y2);
        if (top) {
            cb.moveTo(x1, y1);
            cb.lineTo(x2, y1);
        }
        if (bottom) {
            cb.moveTo(x1, y2);
            cb.lineTo(x2, y2);
        }
        cb.stroke();
        cb.resetRGBColorStroke();
        bottom = true;
        top = true;
    }
}
splitTable()
afterSplitTable()
方法将跟踪是否需要绘制上边框或下边框。实际边界是在
tableLayout()
方法中绘制的

创建表后,需要立即设置此表事件:

PdfPTable table = new PdfPTable(2);
BorderEvent event = new BorderEvent();
table.setTableEvent(event);

现在,您将获得我在初始评论中解释的所需行为。您可以找到完整的代码示例。我提供了一个更为复杂的示例。

您需要一个如下所示的表

正如我在评论中所解释的,您需要将单元格的边框设置为
无边框
,或者通过更改默认单元格:

table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
或者通过更改特定单元格的属性:

PdfPCell cell = new PdfPCell(new Phrase(TEXT));
cell.setBorder(Rectangle.NO_BORDER);
或者两者兼而有之

然后您必须创建一个表事件:

class BorderEvent implements PdfPTableEventAfterSplit {

    protected boolean bottom = true;
    protected boolean top = true;

    public void splitTable(PdfPTable table) {
        bottom = false;
    }

    public void afterSplitTable(PdfPTable table, PdfPRow startRow, int startIdx) {
        top = false;
    }

    public void tableLayout(PdfPTable table, float[][] width, float[] height,
            int headerRows, int rowStart, PdfContentByte[] canvas) {
        float widths[] = width[0];
        float y1 = height[0];
        float y2 = height[height.length - 1];
        float x1 = widths[0];
        float x2 = widths[widths.length - 1];
        PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
        cb.moveTo(x1, y1);
        cb.lineTo(x1, y2);
        cb.moveTo(x2, y1);
        cb.lineTo(x2, y2);
        if (top) {
            cb.moveTo(x1, y1);
            cb.lineTo(x2, y1);
        }
        if (bottom) {
            cb.moveTo(x1, y2);
            cb.lineTo(x2, y2);
        }
        cb.stroke();
        cb.resetRGBColorStroke();
        bottom = true;
        top = true;
    }
}
splitTable()
afterSplitTable()
方法将跟踪是否需要绘制上边框或下边框。实际边界是在
tableLayout()
方法中绘制的

创建表后,需要立即设置此表事件:

PdfPTable table = new PdfPTable(2);
BorderEvent event = new BorderEvent();
table.setTableEvent(event);

现在,您将获得我在初始评论中解释的所需行为。您可以找到完整的代码示例。我提供了一个更为复杂的示例。

您需要一个如下所示的表

正如我在评论中所解释的,您需要将单元格的边框设置为
无边框
,或者通过更改默认单元格:

table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
或者通过更改特定单元格的属性:

PdfPCell cell = new PdfPCell(new Phrase(TEXT));
cell.setBorder(Rectangle.NO_BORDER);
或者两者兼而有之

然后您必须创建一个表事件:

class BorderEvent implements PdfPTableEventAfterSplit {

    protected boolean bottom = true;
    protected boolean top = true;

    public void splitTable(PdfPTable table) {
        bottom = false;
    }

    public void afterSplitTable(PdfPTable table, PdfPRow startRow, int startIdx) {
        top = false;
    }

    public void tableLayout(PdfPTable table, float[][] width, float[] height,
            int headerRows, int rowStart, PdfContentByte[] canvas) {
        float widths[] = width[0];
        float y1 = height[0];
        float y2 = height[height.length - 1];
        float x1 = widths[0];
        float x2 = widths[widths.length - 1];
        PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
        cb.moveTo(x1, y1);
        cb.lineTo(x1, y2);
        cb.moveTo(x2, y1);
        cb.lineTo(x2, y2);
        if (top) {
            cb.moveTo(x1, y1);
            cb.lineTo(x2, y1);
        }
        if (bottom) {
            cb.moveTo(x1, y2);
            cb.lineTo(x2, y2);
        }
        cb.stroke();
        cb.resetRGBColorStroke();
        bottom = true;
        top = true;
    }
}
splitTable()
afterSplitTable()
方法将跟踪是否需要绘制上边框或下边框。实际边界是在
tableLayout()
方法中绘制的

创建表后,需要立即设置此表事件:

PdfPTable table = new PdfPTable(2);
BorderEvent event = new BorderEvent();
table.setTableEvent(event);

现在,您将获得我在初始评论中解释的所需行为。您可以找到完整的代码示例。我提供了一个更为复杂的示例。

您需要一个如下所示的表

正如我在评论中所解释的,您需要将单元格的边框设置为
无边框
,或者通过更改默认单元格:

table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
或者通过更改特定单元格的属性:

PdfPCell cell = new PdfPCell(new Phrase(TEXT));
cell.setBorder(Rectangle.NO_BORDER);
或者两者兼而有之

然后您必须创建一个表事件:

class BorderEvent implements PdfPTableEventAfterSplit {

    protected boolean bottom = true;
    protected boolean top = true;

    public void splitTable(PdfPTable table) {
        bottom = false;
    }

    public void afterSplitTable(PdfPTable table, PdfPRow startRow, int startIdx) {
        top = false;
    }

    public void tableLayout(PdfPTable table, float[][] width, float[] height,
            int headerRows, int rowStart, PdfContentByte[] canvas) {
        float widths[] = width[0];
        float y1 = height[0];
        float y2 = height[height.length - 1];
        float x1 = widths[0];
        float x2 = widths[widths.length - 1];
        PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
        cb.moveTo(x1, y1);
        cb.lineTo(x1, y2);
        cb.moveTo(x2, y1);
        cb.lineTo(x2, y2);
        if (top) {
            cb.moveTo(x1, y1);
            cb.lineTo(x2, y1);
        }
        if (bottom) {
            cb.moveTo(x1, y2);
            cb.lineTo(x2, y2);
        }
        cb.stroke();
        cb.resetRGBColorStroke();
        bottom = true;
        top = true;
    }
}
splitTable()
afterSplitTable()
方法将跟踪是否需要绘制上边框或下边框。实际边界是在
tableLayout()
方法中绘制的

创建表后,需要立即设置此表事件:

PdfPTable table = new PdfPTable(2);
BorderEvent event = new BorderEvent();
table.setTableEvent(event);

现在,您将获得我在初始评论中解释的所需行为。您可以找到完整的代码示例。我提供了一个更复杂的例子。

边界是设计出来的。如果您想更改默认情况下绘制边框的方式,则需要删除边框(
NO_BORDER
),并实现
PdfPTable
界面来绘制您自己的自定义边框:是的,边框是按设计存在的,但问题是我已通过注释在附加的pdf文件中显示。虽然表格的创建并没有在第一页的水平边框中完成,而是从第二页的水平边框中开始,显示哪一个是对的。请查看随附的pdf文件以了解问题。我了解您的问题。拆分表格时,不需要底部边框。这意味着您必须删除设计中存在的边框,并且需要使用单元格和表事件以您喜欢的方式绘制自定义边框。您可能误解了我的回答,因为我写的是
PdfPTable
,而不是
PdfPTableEvent
。但是,链接引用了正确的类。请注意,您的问题是,iText的行为不正确:上一页垂直边框再次框起,这是错误的。这是一种观点,不是事实。iText的行为方式是大多数iText用户所期望的。如果你想要一个不同的行为,你可以按照我在回答中所解释的那样微调边界的绘制方式。如果您想更改默认情况下绘制边框的方式,则需要删除边框(
NO_BORDER
),并实现
PdfPTable
界面来绘制您自己的自定义边框:是的,边框是按设计存在的,但问题是我已通过注释在附加的pdf文件中显示。虽然表格的创建并没有在第一页的水平边框中完成,而是从第二页的水平边框中开始,显示哪一个是对的。请查看随附的pdf文件以了解问题。我了解您的问题。拆分表格时,不需要底部边框。这意味着您必须删除设计中存在的边框,并且需要使用单元格和表事件以您喜欢的方式绘制自定义边框。您可能误解了我的回答,因为我写的是
PdfPTable
,而不是
PdfPTableEvent
。但是,链接引用了正确的类。请注意,您的问题是,iText的行为不正确:上一页垂直边框再次框起,这是错误的。这是一种观点,不是事实。iText的行为方式是大多数iText用户所期望的。如果你想要一个不同的行为,你可以微调