gpt4 book ai didi

java - 当不同表中的数据在 itext 中增加时,pdfptable 标题重复

转载 作者:行者123 更新时间:2023-11-30 11:01:34 24 4
gpt4 key购买 nike

在我的项目中,我需要在 itext 中创建一个包含两列的 pdfptable。这两列包含两个不同的内表(第 1 列包含一个内表,第 2 列包含一个内表)。现在这两个内表都设置了标题行。我的问题是,当第 2 列内表中的数据增加时,我需要重复第 1 列内表的标题。我已经创建了所有需要的表。但是找不到什么好的解决办法我知道这是一个奇怪的要求,但请尽可能提供帮助。

提前致谢。

最佳答案

实现此目的的唯一方法是使用表事件。参见 NestedTables3nested_tables3.pdf例如:

enter image description here

这是实现此目的所需的代码:

class MyPdfPTableEvent implements PdfPTableEvent {

public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
}

public void tableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
ColumnText ct = new ColumnText(canvases[PdfPTable.TEXTCANVAS]);
for (int i = 1; i < widths[1].length; i++) {
Rectangle position = new Rectangle(widths[1][i - 1], heights[1], widths[1][i], heights[2]);
ct.setSimpleColumn(position);
ct.addText(new Phrase("This inner table header will always be repeated"));
try {
ct.go();
} catch (DocumentException ex) {
}
}
}
}

您必须向您的表声明此事件:

PdfPTable table = new PdfPTable(2);
table.setTableEvent(new MyPdfPTableEvent());

当然:为此,您需要添加一个空单元格,以便为您在表格事件中添加的文本提供足够的空间。

关于java - 当不同表中的数据在 itext 中增加时,pdfptable 标题重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31108488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com