Category / Section
How to perform WinForms PDF overlay using C# and VB.NET?
7 mins read
This article explains how to perform PDF Overlay using WinForms PDF viewer libraries.
Two PDF documents can be overlayed using Essential® PDF and Essential® PDF viewer libraries. The following steps are involved in overlaying PDF documents.
- Convert the PDF documents to images with the help of Essential® PDF viewer.
- Compare the images using open source image Magick library.
- Save the difference images as PDF document with the help of Essential® PDF.
Steps to overlay the PDF documents programmatically:
- Create a new Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms , Syncfusion.PdfViewer.Windows and Magick.NET as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the Form1.Designer.cs file.
using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Graphics; using Syncfusion.Windows.Forms.PdfViewer; using ImageMagick;
- Add a new button in Form1.Designer.cs to overlay two PDF documents as follows.
private Button button; private Label label; private void InitializeComponent() { label = new Label(); button = new Button(); //Label label.Location = new System.Drawing.Point(0, 40); label.Size = new System.Drawing.Size(426, 35); label.Text = "Click the button to view the overlayed PDF file generated by Essential PDF"; label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; //Button button.Location = new System.Drawing.Point(180, 110); button.Size = new System.Drawing.Size(85, 26); button.Text = "Overlay PDF"; button.Click += new EventHandler(OverlayPDF); //Create PDF ClientSize = new System.Drawing.Size(450, 150); Controls.Add(label); Controls.Add(button); Text = "Overlay PDF"; }
- Add the following code in OverlayPDF to compare the PDF files.
string sourceFile = "../../Data/groundfloor1.pdf"; //Load the PDF document PdfLoadedDocument loadedDocument = new PdfLoadedDocument(sourceFile); //Get the size of page SizeF pageSize=loadedDocument.Pages[0].Size; //Load the first PDF document to compare PdfViewerControl pdfViewer = new PdfViewerControl(); pdfViewer.RenderingEngine = PdfRenderingEngine.Pdfium; pdfViewer.ReferencePath = @"../../"; pdfViewer.Load(sourceFile); //Load the second PDF document to compare PdfViewerControl pdfViewer1 = new PdfViewerControl(); pdfViewer1.RenderingEngine = PdfRenderingEngine.Pdfium; pdfViewer1.ReferencePath = @"../../"; pdfViewer1.Load("../../Data/groundfloor2.pdf"); //Get the page count int pageCount = 0; if(pdfViewer.PageCount==pdfViewer1.PageCount) { pageCount = pdfViewer.PageCount; } for(int i=0;i<pageCount;i++) { //Export the PDF page to image Bitmap sImage = pdfViewer.ExportAsImage(i); Bitmap gImage = pdfViewer1.ExportAsImage(i); MagickImage image1 = new MagickImage(sImage); MagickImage compare = new MagickImage(gImage); //Generate the diff image MagickImage diffImage = new MagickImage(); double diffPixel = image1.Compare(compare, ErrorMetric.Absolute, diffImage); Bitmap differenceImage = diffImage.ToBitmap(); //Replace the page with diff image if (diffPixel>0) { loadedDocument.Pages.RemoveAt(i); loadedDocument.Pages.Insert(i, pageSize); PdfBitmap image = new PdfBitmap(differenceImage); loadedDocument.Pages[i].Graphics.DrawImage(image, 0, 0, loadedDocument.Pages[i].Graphics.ClientSize.Width, loadedDocument.Pages[i].Graphics.ClientSize.Height); } } //Save and close the document loadedDocument.Save("OverlayPDF.pdf"); loadedDocument.Close(true); //This will open the PDF file so, the result will be seen in default PDF viewer System.Diagnostics.Process.Start("OverlayPDF.pdf");
A complete working sample can be download from PDFOverlaySample.zip
By executing the program, you will get the PDF document as follows.
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.
Conclusion
I hope you enjoyed learning on how to perform PDF Overlay using C# and VB.NET.
You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms PDF example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!