PDFOne .NET
Powerful all-in-one PDF library for .NET
Compatibility
VS 2008 VS 2005 CLR 2.0

PDF Overlay - Stitching PDF Pages Together in .NET

“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 V. Subhash

This article is based on an older PDFOne Java article. The Java article has been the most popular article so far and I guessed .NET users would also be interested in a similar solution.

PDFOne .NET supports page overlays or page stitching where pages are placed side by side, one over another, or set against specified offsets.

PDFOne .NET uses the following overloaded methods of the PDFDocument class to perform PDF overlays.

public void Stitch( 
   int stitchToPageNo,
   int stitchFromPageNo
);

public void Stitch( 
   int stitchToPageNo,
   int stitchFromPageNo,
   float offsetX,
   float offsetY,
   PDFMeasurementUnit unit
);

The first method takes the content of the page specified by stitchFromPageNo and places it over the page specified by stitchToPageNo. The second method does the same but supports offsets from the top-left corner.

For an illustration, we will use the user requirement quoted above. Here is the document that contains the letterhead.

And, here is the document that contains the letters.

Here is the code that provides solution. As Stitch() method to lift the contents a page in the same document, the two documents are merged to form a single document. Next, the second page is lifted and placed on the first page.

using System;
using System.Collections.Generic;
using System.Text;
using Gnostice.PDFOne;

namespace PDF_Overlay {
 class Program  {
  static void Main(string[] args) {
   // Load the document containing the letterhead
   PDFDocument doc = new PDFDocument();
   doc.Load("LetterHead.pdf");
   // Copy page from the other document containing the letter
   doc.AppendPagesFrom("Letters.pdf", "1");
   // Save merged document to file
   doc.Save("MergedDoc.pdf");
   // Free I/O resources
   doc.Close();

   // Load the merged document
   doc.Load("MergedDoc.pdf");
   // Overlay page 2 on page 1
   doc.Stitch(1, 2);
   // Delete the unwanted page 2
   doc.DeletePages("2");
   // Save overlaid document to file
   doc.Save("OverlaidDoc.pdf");
   // Free I/O resources
   doc.Close();
  }
 }
}

Ta da!

Privacy | Legal | Feedback | Newsletter © 2002-2010 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 users are advised to use Microsoft ClearType Tuning for optimal experience. Linux and other users can enable font smoothing, as supported by their OS. Also, please use the latest version of a standards-compliant browser such as Opera, FireFox, Chrome or Safari.