Stitch/overlay pages

<< Click to Display Table of Contents >>

Navigation:  Gnostice Document Studio .NET > Going Deeper > Document Engines > Working with PDF > Enhancement >

Stitch/overlay pages

In this section we present example code to stitch or overlay a PDF page on to another. This is useful when in situations such as when you need to add a letterhead template to an existing page in the PDF, or add a standard header/footer page template to all pages in a PDF file. Before proceeding further please make sure that the prerequisite steps as listed in the Document Engines parent topic are followed.

 

Given below is the sample code.

 

string templatePdfPath = @"Letterhead.pdf";
string letterPdfPath = @"Letter.pdf";
using (var pdfDoc = new PDF())
{
   // Load the document containing the letter
   pdfDoc.LoadDocument(letterPdfPath);
 
   // Copy page from the other document containing the template
   pdfDoc.AppendPagesFrom(templatePdfPath, pageRange: "1");
   
   // Save merged document to a stream
   using (var stream = new MemoryStream())
   {
      pdfDoc.Save(stream);
      pdfDoc.CloseDocument();
 
      // Reload the merged PDF
      stream.Position = 0;
      pdfDoc.LoadDocument(stream);
 
      // Overlay page 2 on page 1
      pdfDoc.Stitch(stitchToPageNo: 1, stitchFromPageNo: 2);
 
      // Delete the unwanted page 2
      pdfDoc.DeletePages(pageRange: "2");
 
      // Save overlaid document to file
      pdfDoc.Save("OverlaidDoc.pdf");
   }
}