Articles in this section
Category / Section

How are two PDF files compared in WinForms PDFViewer?

2 mins read

Two PDF documents can be compared using Essential PDF and Essential PDF viewer libraries. The following steps are involved in comparing PDF documents.

  1. Convert the PDF documents to images with the help of Essential PDF viewer.
  2. Compare the images using open source image Magick library.
  3. Save the difference images as PDF document with the help of Essential PDF.

Steps to compare PDF documents programmatically:

  1. Create a new Windows Forms application project. Create new console application
  2. Install Syncfusion.Pdf.Base, Syncfusion.PdfViewer.Windows and Magick.NET as reference to your .NET Framework application from Install pdf.base nuget package Install nuget package Install Magick package
  3. 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;


  1. Add a new button in Form1.Designer.cs to compare two PDF documents as follows.
    private Button button;
    private Label label;
    private void InitializeComponent()
        label = new Label();
        button = new Button();
        label.Location = new System.Drawing.Point(0, 40);
        label.Size = new System.Drawing.Size(426, 35);
        label.Text = "Click the button to view the compared PDF file generated by Essential PDF";
        label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        button.Location = new System.Drawing.Point(180, 110);
        button.Size = new System.Drawing.Size(85, 26);
        button.Text = "Compare PDF";
        button.Click += new EventHandler(ComparePDF);
        //Create PDF
        ClientSize = new System.Drawing.Size(450, 150);
        Text = "Create PDF";


  1. Add the following code in ComparePDF to compare the PDF files.
    string sourceFile = "../../Data/Source1.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 = @"../../";
    //Load the second PDF document to compare
    PdfViewerControl pdfViewer1 = new PdfViewerControl();
    pdfViewer1.RenderingEngine = PdfRenderingEngine.Pdfium;
    pdfViewer1.ReferencePath = @"../../";
    //Get the page count
    int pageCount = 0;
        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.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
    //This will open the PDF file so, the result will be seen in default PDF viewer 


A complete working sample can be download from

By executing the program, you will get the PDF document as follows. Screenshot of output PDF document


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.




I hope you enjoyed learning about how are are two PDF files compared.

You can refer to our WinForms PDFViewer 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 PDFViewer 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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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