Could someone kindly show a working code example for PDF page imposition using iText? It seems I've exhausted my Google options: there's no code samples for that out there.
Thanks,
InnerOrchestra
ps: By imposition, a printing technical term, I mean, for example, having an 11x17 sheet of paper hold two 8.5x11 pages. For business cards, this would be the same page (3.75x2.25) and for a booklet it would not since the sheet would be folded and the page placement would vary depending on booklet settings.
You could have saved yourself plenty of time by reading Chapter 6 of my book or by simply taking a look at the examples on the iText site. Take for instance the NUpTool example. As you're working in the printing sector, you should be familiar with the term "N-upping". It's when you take a document and then create a new one with 2 pages on one (2-upping), 4 pages on one (4-upping), etc...
Your request is very similar, but easier to achieve, because when we take a document, let's say text_on_stationery.pdf and we 2-up it using the example from my book, you have to scale down the pages, resulting for instance in the document result2up.pdf.
In your case, it's not that hard because you don't need to scale anything. You just need to create a Document
object with twice the size of the original document, create PdfImportedPage
objects to import the pages, and use addTemplate()
with the correct offset to add them side by side on the new document.
There are quite some examples that demonstrate the use of PdfImportedPage
: http://itextpdf.com/themes/keyword.php?id=236
It is strange that Google didn't show you the SuperImposing
example when looking for "imposing". In this example, we add four different layers on top of each other:
PdfReader reader = new PdfReader(SOURCE);
// step 1
Document document = new Document(PageSize.POSTCARD);
// step 2
PdfWriter writer
= PdfWriter.getInstance(document, new FileOutputStream(RESULT));
// step 3
document.open();
// step 4
PdfContentByte canvas = writer.getDirectContent();
PdfImportedPage page;
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
page = writer.getImportedPage(reader, i);
canvas.addTemplate(page, 1f, 0, 0, 1, 0, 0);
}
// step 5
document.close();
reader.close();
In other words, a 4-page document, is now a 1-page document where all the pages are rendered on top of each other. What you now need to do, is to change step 1, so that the dimension of the new pages are different, and to adapt step 4, so two pages are added next to each other and a new page is added after each two pages:
page = writer.getImportedPage(reader, i);
canvas.addTemplate(page, 1f, 0, 0, 1, 0, 0);
i++;
if (i <= reader.getNumberOfPages())
page = writer.getImportedPage(reader, i);
canvas.addTemplate(page, 1f, 0, 0, 1, width / 2, 0);
document.newPage();
In this example, I assume that the height of the original document is equal to the height of the new document and that the width
of the new document is twice the width of the original document. It goes without saying that you can also choose to create a new document with the same width and a double height. In that case, you need:
page = writer.getImportedPage(reader, i);
canvas.addTemplate(page, 1f, 0, 0, 1, 0, height / 2);
i++;
if (i <= reader.getNumberOfPages())
page = writer.getImportedPage(reader, i);
canvas.addTemplate(page, 1f, 0, 0, 1, 0, 0);
document.newPage();