我正在使用 iText 5.3.4
和 Eclipse Kepler
。我有一个账单表,通常它不会超过一页,但是为了测试 pourposes 我一直在检查它的行为并且我遇到了一个奇怪的问题,当页面更改时表 width
会发生变化:
我的表格定义:
PdfPTable tableFactura = new PdfPTable(4);
tableFactura.setWidthPercentage(80f);
tableFactura.setWidths(new int[]{55, 15, 15, 15});
tableFactura.setKeepTogether(false);
tableFactura.setHeaderRows(1);
如果我定义不同的宽度,问题仍然存在,但尺寸不同。
表格用for-loop
填充,里面有PdfCell
和Paragraph
,没有什么复杂的:
// ONE of the CELLS of each line
cell = new PdfPCell(new Paragraph(factura.get("descripcion_linea" + numeroLinea), tinyNormal));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setPaddingLeft(10f);
cell.setPaddingTop(5f);
cell.setBorder(Rectangle.LEFT);
tableFactura.addCell(cell);
我的文档是标准的,Document document = new Document();
,我也试过包括 document.setPageSize(PageSize.A4);
但没有任何变化。正如您在图片中看到的,我也尝试过单元格中没有内容。
知道为什么会这样吗?
UPDATE1:正如@Bruno 所建议的,这里是 Link To File .
更新 2: here是一段“模拟”我正在做的事情的代码,但问题没有发生
最佳答案
好吧,我终于解决了。感谢@BrunoLowagie和 @Fahim Parkar .
将根据所有建议快速解释我的所有修改:
首先根据@Bruno注释,首先我删除了未使用的方法 PdfPTable::setKeepTogether(boolean);
。
其次,并根据this question , 发布者 @Fahim (谢谢!)。我们可以通过 PdfPTable::setLockedWitdth(boolean) 强制表的大小在设置TotalWidth
之前。
PdfPTable tableFactura = new PdfPTable(4);
tableFactura.setLockedWidth(true);
tableFactura.setTotalWidth(425f);
tableFactura.setWidths(new int[]{55, 15, 15, 15});
这允许表格在所有页面中具有相同的大小......但不是全部完成,以避免在最后一行和分页符(单页或多页)中出现奇怪的行为,我必须包括:
tableFactura.setSkipLastFooter(true);
第三:现在表格限制是正确的,只需要更正分页符,为此我定义了页脚行。
tableFactura.setHeaderRows(2);
tableFactura.setFooterRows(1);
注意:根据 PdfPTable::setFooterRows() :
Sets the number of rows to be used for the footer. The number of footer rows are subtracted from the header rows. So in this example row 1 will be footer and row 2 will be header.
第四:只需根据我的需要调整边距、边框和填充,我就能得到我想要的准确结果。
他达! :)
关于java - itext,在新页面拆分时表格宽度发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28715181/