Reputation: 944
I have a problem adding already existed pdf into current pdf I just generated. I saw PdfCopy but my situation is a little different. I have a pdf on the fly which is generated right now and before sending it out to the client I need to add an existing pdf into it.
Upvotes: 2
Views: 1128
Reputation: 77576
If you don't mind losing all interactivity that might be present in the existing PDF, you can use PdfReader
and PdfImportedPage
. Please start by reading the documentation, more specifically section 6.2.1 entitled "Importing pages". You can also download the example ImportingPages1 or take a look at this code snippet:
PdfReader reader = new PdfReader(existing_pdf);
PdfImportedPage page = writer.getImportedPage(reader, pagenumber);
document.setPageSize(reader.getPageSize(pagenumber));
document.newPage();
PdfContentByte canvas = writer.getDirectContent();
canvas.addTemplate(page, 0, 0);
This snippet is written from memory. You may need to tweak it here and there if it doesn't work immediately.
Upvotes: 4