views:

379

answers:

2

Let's say I have two PDF templates created with Adobe Acrobat, which are both single-page, 8.5x11 documents. The first template (A.pdf) has content for the top half of the page. The second template (B.pdf) has content for the bottom half of the page. (It just so happens the content in both templates does not "overlap" each other.)

I would like to use iText to take these two templates and create a single, "merged" template from it (C.pdf) that is only a single page (with A.pdf's content on the top half and B.pdf's content on the bottom half).

(I do not want to "merge" these two files into a 2-page document. I need the final product to be a single page.)

I will be running iText in a servlet environment (Tomcat 6) but I don't think that makes a difference to the answer.

Is this possible?

A: 

Perhaps this code sample helps http://kickjava.com/src/com/lowagie/tools/handout_pdf.java.htm

leonbloy
This seems speculative. Can you describe what the code does and how it applies to my specific problem? It's written in a rather opaque manner.
Shaggy Frog
The program opens a PDF file and generates a new PDF file placing in each new page (say) two pages from the original, with rotation and/or scaling. See around line 130.I once write a program similar based on this, for reformatting pdf files, for viewing or printing. It seemed to me somewhat related to your need.
leonbloy
The code looks like it's adding data in page-size chunks. I want to just add portions of a page.
Shaggy Frog
The code merges two pages of the original into one. If I understand right, you want something similar, only with no (or different) transformation (rotation/scaling/transalation) - and with the two pages coming from different documents, insted of being consecutive pages from the same one.
leonbloy
A: 

I got help from Mark Storer on the iText mailing list. The solution is to get PdfTemplate objects for each file, and then use the addTemplate() method to add them together, e.g.:

PdfTemplate topOfPage = writer.getImportedPage( reader, 1 );
PdfTemplate bottomOfPage = writer.getImportedPage( reader, 2 );

PdfContentByte content = writer.getDirectContent();

// in PDF, "0, 0" is the lower left corner.
content.addTemplate( bottomOfPage );
content.addTemplate( topOfPage, 0, bottomOfPage.getHeight() );
Shaggy Frog