IText Flying Saucer PDF Headers and Ignore HTML

We use xhtml in pdf with good success, but there is a new requirement that the headings and the number of pages are displayed on each page. We use the Flying Saucer news release.

I followed the example here: http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html#page-specific-features

... but that will not work. The title will be on the top left of the first page.

If I use the r7 version, the headers and page numbers work fine, but none of the ones passed in html are rendered, while in r8 the header / page numbers are ignored, but the html is rendered perfectly. xHTML used for tests is copied from the url above.

I know that I need to skip something very simple, if someone has any ideas / comments, I would be very grateful to hear.

+2
source share
2 answers

I think they changed this functionality in r8 .... try this method:

https://gist.github.com/626264

+2
source

We use the same method and everything works fine, however I decided not to use the flying saucer built in the headers and use PdfStamper to add them after creating the PDF file, this works very well, here is an example.

  public void modifyPdf(PdfStamper stamper) { this.reader = stamper.getReader(); PdfContentByte under = null; PdfPTable header = null; PdfPTable footer = null; final int total = this.reader.getNumberOfPages(); for (int page = 1; page <= total; page++) { under = stamper.getUnderContent(page); final PdfDocument doc = under.getPdfDocument(); final Rectangle rect = this.reader.getPageSizeWithRotation(page); header = ... //build your header footer = ... // build your footer final float x = 0; //write header to PDF if (header != null) { float y = (rect.getTop() - 0); header.writeSelectedRows(0, -1, x, y, under); } //write footer to PDF if (footer != null) { float y = (rect.getBottom() + 20); footer.writeSelectedRows(0, -1, x, y, under); } } } 

You can create your template this way:

  final PdfReader reader = new PdfReader(/*your pdf file*/); final PdfStamper stamper = new PdfStamper(reader, /* output */); 

Hope you find this helpful.

+1
source

All Articles