PDFOne (for Java™)
Powerful all-in-one PDF library for Java
Compatibility
J2SE J2EE

PDF Overlay - Stitching PDF Pages Together

“I have two pdf documents - one contains my company's letterhead template and the other has letters addressed to our customers. I would like to know how I can use the letterhead in each of those letters?”
By Santhanam L.

Having everything in one place is a rare virtue. Page overlays seem to bring that luxury to PDF documents.

PDF page overlays are also called page stitching, as pages can be placed side by side, one over another, or set against specified offsets. In PDFOne Java, page stitching is performed by method stitch(int stitchToPageNo, int stitchFromPageNo, double offsetX, double offsetY) method in class PDFDocument.

stitch() overlays content from a page in a document over another page in the same document. The offsets specify x-y coordinates of the position on the original page where the top-left corner of the overlay page is placed. In this article, we will use this method to provide a solution for the given scenario (mentioned along with the title).

To start with, we have a document LetterHead.pdf (Screenshot 1) containing a template and another document Letters.pdf (Screenshot 2) containing sample letter communications. Pages from the second document will be used as overlays on the template from the first document.

Screenshot 1: PDF page with letterhead template
Screenshot 2: PDF page with overlay content

Using overlays, we should be able to obtain a document as shown below.

Screenshot 3: Overlay page placed on template

In our code, we first load the second document and append the letterhead page from the first document. The resulting document is saved as MergedForStitching.pdf. This is an intermediate file, which will then be used by stitch() to perform the page overlay operation.

// Load document containing overlays
PdfReader r = PdfReader.fileReader("Letters.pdf");
r.setOutFilePath("MergedForStitching.pdf");
PdfDocument d = new PdfDocument(r);

// Append "template" page
d.appendPagesFrom("LetterHead.pdf", "1");

Now, the MergedForStitching.pdf document has been created with both the overlay pages as well as the template.

Going further, we load MergedForStitching.pdf and specify that output of the overlay operation should be saved as StitchDocument.pdf.

r = PdfReader.fileReader("MergedForStitching.pdf");
r.setOutFilePath("StitchDocument.pdf");
d = new PdfDocument(r);

Rest of the implementation is as follows: Process the overlay pages in a loop. Inside each iteration, clone the template page and append it to the document. Now, call stitch() to overlay content from the current overlay page on newly appended cloned template page. After completing loop, delete the original overlay pages and the template page. This will give you the final document with just pages from the overlay operation, as shown in Screenshot 3.

// Store number of the template page
int templatePageNo = d.getPageCount();
// Store number of last overlay page
int lastOverlayPageNo = templatePageNo - 1;

PdfPage page = null;

// Loop through the overlay pages
for (int i = 1; i <= lastOverlayPageNo; i++) {
 // Clone and append a copy of template page for 
 // current overlay page
 page = (PdfPage) d.getPage(templatePageNo).clone();
 d.add(page);

 // Place content from current overlay page 
 // on cloned template page
 d.stitch(
     templatePageNo + i,  
         // int stitchToPageNo (cloned template page number)
     i,  // int stitchFromPageNo (current overlay page number)
     0,  // double offsetX
     0); // double offsetY
}

// Delete overlay pages and template page
d.deletePages("1-" + templatePageNo);

d.setOpenAfterSave(true);
d.write();
r.dispose();
---o0O0o---

Downloads:

---o0O0o---

Our Developer Tools
eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

XtremePDFConverter VCL

A Delphi/C++Builder component to intelligently convert PDF to user-friendly Word RTF documents.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

XtremeDocumentStudio .NET

Multi-format document-processing component suite for .NET developers

PDFOne (for Java™)

A Java™ PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java™ applications.

XtremeFontEngine (for Java)

Java font engine to render glyphs from Type 1, Type 2 (CFF), and TrueType fonts

Our Office Productivity Applications
Free PDF Reader

A free, fast, and portable application for viewing, printing and converting PDF documents.

Privacy | Legal | Feedback | Newsletter | Resellers © 2002-2013 Gnostice Information Technologies Private Limited. All rights reserved.

This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows XP users are advised to use Microsoft ClearType Tuning for optimal experience. Also, please use the latest version of a standards-compliant browser such as Firefox, Opera, or Dragon (Chromium).