How to reuse a page from one PDF document into another PDF document?

Tags: Import pagePdfReaderiText 5

I'm trying to add an image that I have on the file system as a PDF into a PDF that I'm creating on the fly. I tried to use the Image class, but it seems that it does not work with PDFs (only JPEG, PNG or GIF). How can I create an element from an existing PDF, so that I can place it in my new PDF?

Posted on StackOverflow on Apr 11, 2014 by Andres Olarte

The most basic solution is to create a PdfReader instance and to import the page into the PdfWriter instance, from which point on you can use the PdfImportedPage instance, either directly, or wrapped inside an Image object:

PdfReader reader = new PdfReader(existingPdf);
PdfImportedPage page = writer.getImportedPage(reader, i);
PdfImage img = Image.getInstance(page);