Set Graphic Overlay in PDF File in C#/VB.NET

This section is designed to provide developers a solution to set graphic overlay in PDF file with C#, VB.NET via a .NET PDF library Spire.PDF for .NET.

Spire.PDF for .NET enables you to set graphic overlay for PDF pages in an easy way. In order to see content in both PDF files, we can set the transparency for the overlay. Now, let us see the code detail step by step.

First, we need to load two PDF documents from system which are used for creating overlay PDF document. Then, we can create the page template as one page of the first PDF file. In my project, I set the first page in the first PDF document as the template. Finally draw this template page into every page of the second PDF file by this method: PdfPageBase.Canvas.DrawTemplate(PdfTemplate template, PointF location) and set the transparency for the page: PdfPageBase.Canvas.SetTransparency(float alphaPen, float alphaBrush, PdfBlendMode blendMode).

Here you can know more about Spire.PDF for .NET. Spire.PDF for .NET is a PDF component that is applied to manipulate PDF files in .NET applications. You can download Spire.PDF for .NET. After installing it on system, you can start your project by C# or VB.NET in either Console Application or Windows Forms Application. Since we will use Spire.PDF, you need add Spire.Pdf.dll in the Bin folder, the default path is: "..\Spire.Pdf\Bin\NET4.0\Spire.Pdf.dll". Below is the whole code of the task:

[C#]
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;

namespace pdf_graphic_overlay
{
    class Program
    {
        static void Main(string[] args)
        {
            //load two documents
            PdfDocument doc1 = new PdfDocument();
            doc1.LoadFromFile(@"..\ Sample1.pdf");
            PdfDocument doc2 = new PdfDocument();
            doc2.LoadFromFile(@"..\Sample3.pdf");
            //Create page template
            PdfTemplate template = doc1.Pages[0].CreateTemplate();
            //set PDF overlay effect and set transparency mode
            foreach (PdfPageBase page in doc2.Pages)
            {
                //set transparency 
                page.Canvas.SetTransparency(0.25f, 0.25f, PdfBlendMode.Overlay);
                //add the first page of doc1 to every page of doc2
                page.Canvas.DrawTemplate(template, PointF.Empty);
            }
            //Save pdf file.
            doc2.SaveToFile("Overlay.pdf");
            doc1.Close();
            doc2.Close();
            //Launching the Pdf file.
            System.Diagnostics.Process.Start("Overlay.pdf");
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics

Namespace pdf_graphic_overlay
     Class Program
	    Private Shared Sub Main(args As String())
		'load two documents
		Dim doc1 As New PdfDocument()
		doc1.LoadFromFile("..\Sample1.pdf")
		Dim doc2 As New PdfDocument()
		doc2.LoadFromFile("..\Sample3.pdf")
		'Create page template
		Dim template As PdfTemplate = doc1.Pages(0).CreateTemplate()
		'set PDF overlay effect and set transparency mode
		For Each page As PdfPageBase In doc2.Pages
			'set transparency 
			page.Canvas.SetTransparency(0.25F, 0.25F, PdfBlendMode.Overlay)
			'add the first page of doc1 to every page of doc2
			page.Canvas.DrawTemplate(template, PointF.Empty)
		Next
		'Save pdf file.
		doc2.SaveToFile("Overlay.pdf")
		doc1.Close()
		doc2.Close()
		'Launching the Pdf file.
		System.Diagnostics.Process.Start("Overlay.pdf")
	    End Sub
     End Class
End Namespace

After executing above code, we can get the output file as below:

Draw Overlay Images

I have introduced one solution to set graphic overlay for PDF document, I wish it can give you some insights. If you have problem, feedback and suggestions, please do not hesitate to put them on E-iceblue Forum, we will give prompt reply.

Spire.PDF for .NET is a PDF application which is designed to perform processing tasks on managing PDF files. It is completely written in C# but also support VB.NET.