在最后一页底部的pdf中创建表格(错误的官方解决方案) [英] Creating table in pdf on last page bottom (wrong official solution)

查看:126
本文介绍了在最后一页底部的pdf中创建表格(错误的官方解决方案)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是iText的官方解决方案,用于在pdf文档的最后一页.此解决方案将我的表格放在pdf文档的最后一页的底部.很好.

This is official iText solution for creating a table on the last page at the bottom in pdf document. This solution puts my table at the bottom of the last pdf page. Great.

不幸的是,这也导致我的桌子越来越狭窄.这就是我所不想要的.我已经尝试了几个小时才能再次扩大该表的范围,但是没有成功.我无法解决.移动前如何将桌子放在原始尺寸的底部?这个问题的最佳解决方案是什么?

Unfortunately, it causes getting my table more narrow too. And this is what I don't want. I have tried several hours to get that table wider again, but without success. I cannot resolve it. How to put table at the bottom in original size before moving? What is the best solution of this problem?

问题图片

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

Before moving, width of my table was created only based on table.setWidthPercentage(100); Then it started to report exception that width of the table must be greater than zero.

table.setWidths(表中的列数);

table.setWidths(number of columns in my table);

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

I tried table.setTotalWidth() set on different value than zero and then overwrite it with that official code from iText. But without success. I am looking for some elegant solution of this.

代码:

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

推荐答案

例如,我插入了(document.leftMargin()-50),它与桌子一起向侧面移动.我尝试了各种值,但是我还没有找到合适的值.在工作的三天内,我将尝试零:-)

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

您现在应该尝试零!

确实是该官方样本

Indeed, that official sample and the answer here on stack overflow that sample is derived from are slightly wrong when they apply the margins here:

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本身已经应用了匹配边距,例如

because the Document methods left, bottom, right, and top themselves already apply the matching margin, e.g.

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

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

Thus, the recommended code effectively applies the margins twice!

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

So your method drawTableAtTheEndOfPage should simply look like this:

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

这篇关于在最后一页底部的pdf中创建表格(错误的官方解决方案)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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