Sunday, October 17, 2010

How to append text and images to an existing PDF document

Requirements:

Recently in one of my projects we had a requirement that, we had to append some legal text and an image (in BMP, PNG or JPEG format) to an existing PDF invoice.

Now PDF is not a text format.  We can't simply add text and an image to the pdf document, by appending their bytes to the PDF file.  It requires special api's for manipulation. 

How do we do it:

iText framework has excellent support for manipulating PDF documents.  It can create, read, edit and append PDF documents.  Its really powerful!

But today we are specifically interested in adding some text and image to an existing PDF document.

Here's the code that can do this:

The generated PDF looks like this:




That's it!  The end result is we have a shining new PDF called "invoice-appended.pdf", which holds the content of the existing PDF and new information (text and image) appended to it.
Have some Fun!