gpt4 book ai didi

java - 在最后一页底部创建pdf表格(错误的官方解决方案)

转载 作者:行者123 更新时间:2023-12-02 02:51:17 25 4
gpt4 key购买 nike

This is official iText solution for creating a table on the last page at the bottom in pdf document.该解决方案将我的表格放在最后一个 pdf 页的底部。太棒了。

不幸的是,这也导致我的 table 变得更窄。这就是我不想要的。我已经尝试了几个小时来让 table 再次变宽,但没有成功。我无法解决它。移动前如何将 table 按原来尺寸放在底部?这个问题的最佳解决方案是什么?

问题图片

Picture of the problem

在移动之前,我的表格的宽度仅基于table.setWidthPercentage(100)创建;然后就开始报异常,表格的宽度必须大于零。

table.setWidths(我的表格中的列数);

我尝试将 table.setTotalWidth() 设置为非零值,然后用 iText 的官方代码覆盖它。但没有成功。我正在寻找一些优雅的解决方案。

代码:

public static void main(String[] args)
{
Document document = new Document(PageSize.A4);
PdfWriter writer = null;
try {
writer = PdfWriter.getInstance(document,
new FileOutputStream("C:/radek-folder/calendar.pdf"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
document.open();

PdfPTable datatable = createHeaderTable();
document.add(datatable);
datatable = createFooterTable();

drawTableAtTheEndOfPage(document, writer, datatable);

document.close();
System.out.println("done");
}

private static void drawTableAtTheEndOfPage(Document document, PdfWriter writer, PdfPTable datatable) {
datatable.setTotalWidth(document.right(document.rightMargin()) - document.left(document.leftMargin()));

datatable.writeSelectedRows(0, -1, document.left(document.leftMargin()),
datatable.getTotalHeight() + document.bottom(document.bottomMargin()),
writer.getDirectContent());
}

private static PdfPTable createFooterTable() throws DocumentException {
int[] columnWidths = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1 };
PdfPTable datatable = new PdfPTable(columnWidths.length);
datatable.setKeepTogether(true);
datatable.setWidthPercentage(100);
datatable.setWidths(columnWidths);
datatable.getDefaultCell().setPadding(5);

datatable.getDefaultCell().setHorizontalAlignment(horizontalAlignment);
datatable.getDefaultCell().setVerticalAlignment(verticalAlignment);

for (int i = 0; i < 100; i++) {
addCellToTable(datatable, horizontalAlignmentLeft,
verticalAlignmentMiddle, "Přehledová tabulka", columnWidths.length, 1,
fontTypeBold, fontSizeRegular, cellLayout_Bottom);
}

return datatable;
}

最佳答案

I inserted(document.leftMargin() - 50) for example, and it moved with table to the side. I tried various values, but i havent found the suitable ones. In three days at work i will try zero :-)

你现在应该尝试零!

确实,that official samplethe answer here on stack overflow当他们在此处应用边距时,该示例派生略有错误:

datatable.setTotalWidth(document.right(document.rightMargin()) - 
document.left(document.leftMargin()));

datatable.writeSelectedRows(0, -1, document.left(document.leftMargin()),
datatable.getTotalHeight() + document.bottom(document.bottomMargin()),
writer.getDirectContent());

因为 Document 方法 leftbottomrighttop他们自己已经应用了匹配边距,例如

public float left(float margin) {
return pageSize.getLeft(marginLeft + margin);
}

因此,推荐的代码有效地应用了两次边距!

所以你的方法drawTableAtTheEndOfPage应该看起来像这样:

private static void drawTableAtTheEndOfPage(Document document, PdfWriter writer, PdfPTable datatable)
{
datatable.setTotalWidth(document.right() - document.left());

datatable.writeSelectedRows(0, -1, document.left(),
datatable.getTotalHeight() + document.bottom(), writer.getDirectContent());
}

关于java - 在最后一页底部创建pdf表格(错误的官方解决方案),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43807931/

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