Forums Register Login

Adding Header and Footer to an existing PDF

+Pie Number of slices to send: Send
Hi All,

I am stuck with adding a Header and Footer to an existing PDF using iText.

Any clues are appreciated. I have already checked the iText Documentation and
did not find anything matching.

My code goes like below -


If I comment the line

document.add( new Chunk("testing"));

I get an exception

Exception in thread "main" ExceptionConverter: java.io.IOException: The document
has no pages.

Without commenting there are no exceptions but it doesnt add the Header and
Footer. Any clues are highly appreciated.

Or if you could suggest how can I add events to an existing PDF.
I also tried using PDFCopy and adding events to it but seems like you can attach
events only to a writer.

Is there anyways we can add header and footer to an existing PDF?

Regards,
Tina
+Pie Number of slices to send: Send
I'm not sure if it's possible, but the way to alter existing documents is by way of the com.lowagie.text.pdf.PdfStamper class.
+Pie Number of slices to send: Send
Thanks Lester.

I already tried that and it works.

But the problem is I have a multiple line header and footer. Adding Multiple line header and footer doesnt seem possible with PdfStampler.

I don’t mind writing temp files, so I tried using PdfCopy to create a new PDF File but the problem is you cant attach events to PdfCopy.

Regards,
Tina
+Pie Number of slices to send: Send
 

Tina Tibrewal wrote:Adding Multiple line header and footer doesnt seem possible with PdfStamper.



That's not correct.
There are different ways to add a multiple line header.
Please read chapter 3 of the book to find out how to use ColumnText to add multiple lines at absolute positions.
You can also use a PdfPTable.
As a matter of fact, there's an example in the chapter that is available for free on the book page at Manning
that uses a PdfPTable for the header; see listing 6.14. As you probably know a PdfPCell can consist of multiple lines.
+Pie Number of slices to send: Send
Thanks Bruno.

I got it working. But now the problem I am facing is - It is not writing the header properly. Though I do a getOverContent and getUnderContent and then add tables but somehow the header is not being written. Any suggestions there?

My code looks like -



Regards,
Tina
+Pie Number of slices to send: Send
 

Tina Tibrewal wrote:I got it working.


The book sample works.

Tina Tibrewal wrote:But now the problem I am facing is - It is not writing the header properly.


You adapted the book sample, and you don't see the header, but I'm not sitting at your desk, I can't see what's wrong.
I can only tell that 1000pt seems like a very large table to me (does it fit on the page, or is it written outside the visible area of the page?) and that I really don't like these lines:

Tina Tibrewal wrote:Document document = underContent.getPdfDocument();
Document overDocument = overContent.getPdfDocument();


You didn't get these from one of the book examples, did you?
Bras cause cancer. And tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 14299 times.
Similar Threads
Trouble in generating pdf reports with Jasper and iText
Headers and Footers are not appearing in saved PDF
PdfBox, do you have to save the .pdf to a file?
PDF with jsf
How jsp and a pdf document displayed in the same browser?
iText: Page content overlapping with footer and also not generating new page.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:02:06.