PDFOne (for Java)
Create, edit, view, print & enhance PDF documents and forms in Java SE/EE
Compatibility
J2SE J2EE Windows Linux Mac (OS X)

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.

try {
  // Load document containing letter content
  PdfDocument doc1 = new PdfDocument();      
  doc1.load("Letters.pdf");
  
  // Copy letterhead page from another document
  doc1.appendPagesFrom("LetterHead.pdf", "1");
  
  // Save merged document to file
  doc1.save("MergedForStitching.pdf");
  doc1.close();
} catch (IOException | PdfException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

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.

PdfDocument doc2 = new PdfDocument();
try {
  // Load merged document
  doc2.load("MergedForStitching.pdf");

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.

  // Obtain number of the letterhead page 
  int letterHeadPageNumber = doc2.getPageCount();
  // Obtain number of the last page with letter content
  int lastContentPageNumber = letterHeadPageNumber - 1;
   
  PdfPage tempPage;       
  // Loop through the overlay pages
  for (int i = 1; i <= lastContentPageNumber; i++) {
    // Clone and append a copy of letterhead page
    tempPage = (PdfPage) doc2.getPage(letterHeadPageNumber).clone();
    doc2.add(tempPage);
     
    // Copy content of letterhead page over cloned page
    doc2.stitch(
      letterHeadPageNumber + i, // to           
      i,    // from
      0,    // horizontal offset
      0);   // vertical offset
  }

  // Delete content and letterhead pages
  doc2.deletePages("1-" + letterHeadPageNumber);
  
  // Save document to file
  doc2.save("StitchDocument.pdf");
  doc2.close();
} catch (IOException | PdfException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}
---o0O0o---

Downloads:

---o0O0o---

Our .NET Developer Tools
Gnostice Document Studio .NET

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

PDFOne .NET

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

Our Delphi/C++Builder developer tools
Gnostice Document Studio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

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.

Our Java developer tools
Gnostice Document Studio Java

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

PDFOne (for Java)

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

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

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