File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Open Source Projects and the fly likes Adding Header and Footer to an existing PDF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Adding Header and Footer to an existing PDF" Watch "Adding Header and Footer to an existing PDF" New topic
Author

Adding Header and Footer to an existing PDF

Tina Tibrewal
Ranch Hand

Joined: Apr 19, 2007
Posts: 40
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
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
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.
Tina Tibrewal
Ranch Hand

Joined: Apr 19, 2007
Posts: 40
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
Bruno Lowagie
author
Ranch Hand

Joined: Oct 23, 2006
Posts: 88
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.
Tina Tibrewal
Ranch Hand

Joined: Apr 19, 2007
Posts: 40
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
Bruno Lowagie
author
Ranch Hand

Joined: Oct 23, 2006
Posts: 88
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding Header and Footer to an existing PDF