0
votes

I need to generate a pdf report with complex template. I have an xls file with a complex layout that generally consists of a header with table header, table rows, table footer and a general footer.

Is it possible to generate a pdf report with itext which would be sequentially generated from

  • Header.pdf file with the table's column headers row
  • Dynamically generated in java code table row that would seamlessly continue the table from the Header.pdf
  • Footer.pdf with column footers that would seamlessly continue table rows generated from java code.

Like this:

Header.pdf
+----------------------------------------------+  |                                              |  |                Header                        |  +----------------------------------------------+  |                      |                       |  |   Column 1 Header    |     Column N Header   |  |                      |                       |  +----------------------------------------------+ 

Java-code-generated table rows
 +----------------------------------------------+  | | |  | Value 11 | Value 1M |  | | |  +----------------------------------------------+  ...  +----------------------------------------------+  | | |  | Value N1 | Value NM |  | | |  +----------------------+-----------------------+

Footer.pdf
+----------------------------------------------+  | | |  | Column 1 Footer | Column N Footer |  | | |  | | |  +----------------------+-----------------------+  | |  | Footer |  | |  +----------------------------------------------+

Thank you.

UPDATE

Here's the template in Excel.

The two rows of the table are straight in the middle of the template. And accordingly the general header with table column headers and the footer with column footers.

The templateThe template

1
Please describe header.pdf and footer.pdf. Why are it PDF files? Why not just some content? Why are it PDFs? What is the page size of these 1-page PDFs (assuming that they only have 1 page)? Are header.pdf and footer.pdf interactive PDFs or just flat? - Bruno Lowagie
@BrunoLowagie please see the update of the original post. I've made the screenshot of the source excel file. - Grigory
I don't understand the purpose of the excel file. I don't see any header of footer, nor how this would relate to PDF. I'll show you an example and then you can use that example to clarify what you mean. - Bruno Lowagie
Did my answer correspond with the question? - Bruno Lowagie

1 Answers

2
votes

Suppose that you have a header (header.pdf) and a footer (footer.pdf), then you can integrate these static PDFs as a header and a footer as described in the SimpleTable8 example (see simple_table8.pdf):

enter image description here

enter image description here

Let's take a look at the code:

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    PdfPTable table = new PdfPTable(3);
    table.setWidthPercentage(100);
    PdfReader reader = new PdfReader("resources/pdfs/header.pdf");
    PdfImportedPage header = writer.getImportedPage(reader, 1);
    PdfPCell cell = new PdfPCell(Image.getInstance(header));
    cell.setColspan(3);
    table.addCell(cell);
    for (int row = 1; row <= 50; row++) {
        for (int column = 1; column <= 3; column++) {
            table.addCell(String.format("row %s, column %s", row, column));
        }
    }
    reader = new PdfReader("resources/pdfs/footer.pdf");
    PdfImportedPage footer = writer.getImportedPage(reader, 1);
    cell = new PdfPCell(Image.getInstance(footer));
    cell.setColspan(3);
    table.addCell(cell);
    document.add(table);
    document.close();
}

We import a header (or footer) like this:

PdfReader reader = new PdfReader("resources/pdfs/header.pdf");
PdfImportedPage header = writer.getImportedPage(reader, 1);

Now we create a PdfPCell that wraps this PdfImportedPage inside an Image and we add the cell to the table:

PdfPCell cell = new PdfPCell(Image.getInstance(header));
cell.setColspan(3);
table.addCell(cell);

Note that wrapping the PdfImportedPage inside an Image doesn't rasterize the text. You'll discover that the text "This is a header" can still be selected, copied and pasted.