2

Using iText 5.5.5. I have an open com.itextpdf.text.Document through a PdfWriter instance. At various points during construction of the document, I need to add in static PDF pages. The static pages are coming in the form of a byte[].

After following various examples on itextpdf.com, I am unable to mesh their examples with my use case. Here is the code:

Document trunk = new Document();
PdfWriter writer = PdfWriter.getInstance(trunk, getTrunkStream());
writer.setPageEvent(geTrunkPageEvent());
trunk.open();

....

PdfReader reader = new PdfReader(bytes);
// graft == my static content
Document graft = new Document(reader.getPageSizeWithRotation(1));
PdfCopy copy = new PdfCopy(graft, getTrunkStream());
graft.open();

int count = reader.getNumberOfPages();
for(int page = 0; page < count;) {
    copy.addPage(copy.getImportedPage(reader, ++page));
}

copy.freeReader(reader);
reader.close();

The code compiles and runs error free. But the graft pages fail to appear with the trunk pages.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
TheRed__
  • 164
  • 11
  • Did you download the free ebook [The best iText Questions on StackOverflow](http://pages.itextpdf.com/ebook-stackoverflow-questions.html)? It contains the answer to this question [Creating a PDF image in iText](http://stackoverflow.com/questions/23022471/creating-a-pdf-image-in-itext). In your code, you are using `getTrunkStream()` twice: once to create a document from scratch with `PdfWriter` and once to combine existing documents using `PdfCopy`. This goes against all logic. Didn't you read [chapter 6 of my book](http://manning.com/lowagie2/samplechapter6.pdf)? – Bruno Lowagie Apr 17 '15 at 07:20
  • Why patronize the question? – TheRed__ Apr 17 '15 at 13:53
  • Because I'm the patron of iText. Did you ever fly to Silicon Valley? Then you'd know that the SFO airport is located at a place called San Bruno :D – Bruno Lowagie Apr 17 '15 at 13:55
  • I'm flying to San Jose for the ISO meetings for PDF tomorrow. I'm always nervous before a flight. I'll be happy when I land in the city named after Saint Bruno. – Bruno Lowagie Apr 17 '15 at 14:08
  • Especially when your flying that distance. Godspeed. – TheRed__ Apr 17 '15 at 14:15

1 Answers1

3

Read the answer to this question Read BLOB (PDF Content) from database and edit and output PDF , for more detailed description

Use PdfContentByte to hold the PDF content to be added

 PdfContentByte cb = writer.getDirectContent(); 

Create a PdfImportedPage page object for each page you want to import from another document using getImportedPage() and add the page using to writer using addTemplate():

trunk.newPage();
page = writer.getImportedPage(pdfReader, pagenumber);
cb.addTemplate(page, 0, 0);

Make sure to close the document and the pdfReader.

Note: do not use this code snippet if you merely want to merge a bunch of files. The reason why you shouldn't do this, is explained in the answer to the question How to merge documents correctly?

Community
  • 1
  • 1
Sangeet Menon
  • 9,555
  • 8
  • 40
  • 58
  • 2
    I have updated your answer because it could easily be misinterpreted as a way to merge documents. When you import pages, it is important to understand the caveats explained in the answer to the question [How to merge documents correctly?](http://stackoverflow.com/questions/21731439/how-to-merge-documents-correctly) which is a question that was selected as one of [The Best iText Questions on StackOverflow](http://pages.itextpdf.com/ebook-stackoverflow-questions.html). With these changes in place, the answer deserves an upvote ;-) – Bruno Lowagie Apr 17 '15 at 07:31
  • 1
    @BrunoLowagie, I have never come across a situation with pages with different orientation, hence wasn't aware of this issue. An upvote more than deserved for answer that points it out correctly! – Sangeet Menon Apr 17 '15 at 07:41
  • 1
    This solution worked for me. Though I had to be sure and turn off my trunk's page event and invoke a new page before turning it back on as I wanted to preserve, exactly, the content retrieved. Thanks guys. – TheRed__ Apr 21 '15 at 18:43