0

We have generated a pdf in landscape mode with header and footer as part of the pdf. The header table and footer display fine in pdf using itextpdf5.1.1 jar. However when we update the jar to 5.5.3, the header table does not show only the footer shows. Below is the code snippet.

document = new Document(PageSize.A4.rotate(), 20, 20, 75, 20);

PdfCopy copy = new PdfCopy(document, new FileOutputStream(strPDFFile));
document.open();
PdfReader pdfReaderIntermediate =
    new PdfReader(strIntermediatePDFFile);
numberOfPages = pdfReaderIntermediate.getNumberOfPages();
Font ffont = new Font(Font.FontFamily.UNDEFINED, 7, Font.NORMAL);
System.out.println("###### No. of Pages: " + numberOfPages);
for (int j = 0; j < numberOfPages; ) {
    page = copy.getImportedPage(pdfReaderIntermediate, ++j);
    stamp = copy.createPageStamp(page);
    Phrase footer =
        new Phrase(String.format("%d of %d", j, numberOfPages), ffont);
    ColumnText.showTextAligned(stamp.getUnderContent(),
                               Element.ALIGN_CENTER, footer,
                               (document.right() - document.left()) /
                               2 + document.leftMargin(),
                               document.bottom() - 10, 0);
    if (j != 1) {
        headerTable = new PdfPTable(2);
        headerTable.setTotalWidth(700);
        headerTable.getDefaultCell().setFixedHeight(10);
        headerTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        headerTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
        headerTable.addCell(new Phrase(String.format(header1), ffont));
        headerTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
        headerTable.addCell(new Phrase(String.format(header2), ffont));
        headerTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
        headerTable.addCell(new Phrase(String.format(header3), ffont));
        headerTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
        headerTable.addCell(new Phrase(String.format(header5, j),
                                       ffont));
        headerTable.completeRow();
        headerTable.writeSelectedRows(0, 5, 60.5f, 550,
                                      stamp.getUnderContent());


    }


    stamp.alterContents();
    copy.addPage(page);

}
document.close();

When we change the jar from 5.1.1 to 5.5.3 the header is lost. May be a change is needed in the way we call the header for the new jar.

Any inputs will be well appreciated. Thanks.

Sandeep Singh
  • 15
  • 1
  • 6

1 Answers1

0

You have cells with default padding (i.e. 2) and height 10, and you try to insert text at height 7. But 2 (top margin) + 7 (text height) + 2 (bottom margin) = 11, i.e. more than fits into your cell height 10. Thus, the text does not fit and is not displayed.

You can fix this by either

  • using a smaller font, e.g. 6, or
  • using a heigher cell, e.g. 11, or
  • using a smaller padding, e.g. 1:

    headerTable.getDefaultCell().setPadding(1);
    

With any of these changes, your header shows.

I don't know in which way iText 5.1.1 handled this differently, but the behavior of current iText versions makes sense.

mkl
  • 90,588
  • 15
  • 125
  • 265
  • The above corrections were helpful. Thanks a lot! I still dont know how iText 5.1.1 ignored these but on correcting the values for cell padding and font value, I was able to display the header in pdf using iText 5.5.3 – Sandeep Singh May 08 '15 at 15:06