To add an image to an already existing PDF file is a relatively simple task, using iText.
On this example, we will add a smile image to the bottom of the PDF first page.
We start by loading the PDF we want to modify, and getting the reference for the first page:
PdfReader reader = new PdfReader(srcPdf); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destPdf)); PdfContentByte content = stamper.getOverContent(1);
srcPdf is a String with the full path to the existing PDF file, while destPdf is the full path where the modified PDF file will be created.
Then, we load the image (imagePath is the full path for the imagem file):
Image image = Image.getInstance(imagePath); // scale the image to 50px height image.scaleAbsoluteHeight(50); image.scaleAbsoluteWidth((image.getWidth() * 50) / image.getHeight());
Since the image dimensions are large, it's scaled to 50 pixels height before we add it to the PDF.
We then set the page coordinates where we want it. Be aware that the 0 value for the Y axis is the bottom of the page, not the top:
All we need to do now is add it to the page reference and close the stamper:
The sample project with all the code, example PDF and image is available at github, here