Articles in this section
Category / Section

How to superimpose PDF pages using C# and VB.NET?

2 mins read

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can superimpose PDF pages using C# and VB.NET.

Steps to superimpose PDF pages programmatically:

  1. Create a new C# console application project. Create a console application project
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. NuGet package reference
  3. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
using System.Drawing;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Parsing
Imports System.Drawing

 

  1. Use the following code snippet to superimpose the PDF pages.

C#

//Load the PDF documents
PdfLoadedDocument ldDoc1 = new PdfLoadedDocument("BorderTemplate.pdf");
PdfLoadedDocument ldDoc2 = new PdfLoadedDocument("SourceTemplate.pdf");
//Create the new PDF document
PdfDocument doc = new PdfDocument();
for (int i = 0, count = ldDoc2.Pages.Count; i < count; ++i)
{
    //Add a new page to the document
    PdfPage page = doc.Pages.Add();
    //Create the graphics
    PdfGraphics g = page.Graphics;
    //Load the page from loaded document
    PdfPageBase lpage = ldDoc2.Pages[i];
    //Create template
    PdfTemplate template = lpage.CreateTemplate();
    g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize());
    //Load the page from loaded document
    lpage = ldDoc1.Pages[0];
    //Create template
    template = lpage.CreateTemplate();
    //Draw template 
    g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize());
}
//Save the document
doc.Save("SuperimposePDF.pdf");
//Close the documents
ldDoc1.Close(true);
ldDoc2.Close(true);
doc.Close(true);
//This will open the PDF file so, the result will be seen in default PDF viewer
Process.Start("SuperimposePDF.pdf");

 

VB.NET

'Load the PDF documents
Dim ldDoc1 As PdfLoadedDocument = New PdfLoadedDocument("BorderTemplate.pdf")
Dim ldDoc2 As PdfLoadedDocument = New PdfLoadedDocument("SourceTemplate.pdf")
'Create the new PDF document
Dim doc As PdfDocument = New PdfDocument()
Dim i As Integer = 0, count As Integer = ldDoc2.Pages.Count
 
While i < count
    'Add a new page to the document
    Dim page As PdfPage = doc.Pages.Add()
    'Create the graphics
    Dim g As PdfGraphics = page.Graphics
    'Load the page from loaded document
    Dim lpage As PdfPageBase = ldDoc2.Pages(i)
    'Create template
    Dim template As PdfTemplate = lpage.CreateTemplate()
    g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize())
    'Load the page from loaded document
    lpage = ldDoc1.Pages(0)
    'Create template
    template = lpage.CreateTemplate()
    'Draw template
    g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize())
    i += 1
End While
 
'Save the document
doc.Save("SuperimposePDF.pdf")
'Save the documents
doc.Close(True)
ldDoc1.Close(True)
ldDoc2.Close(True)
'This will open the PDF file so, the result will be seen in default PDF viewer
Process.Start("SuperimposePDF.pdf")

 

A complete working sample can be downloaded from SuperimposePDFSample.zip.

Take a moment to peruse the documentation, where you can find other options like rearranging pages from an existing PDF document, rotating a PDF page, inserting pages in a document, changing the page numbers in a PDF document with code examples.

Refer here to explore the rich set of Syncfusion Essential PDF features.

An online sample link to overlay two different documents into a single PDF document.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied