Creating table in pdf on last page bottom (wrong o

2019-09-15 19:15发布

问题:

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?

Picture of the problem

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(number of columns in my table);

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.

The code:

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

回答1:

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

You should try zero now!

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

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!

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


标签: java pdf itext