We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to compare two PDF documents

Platform: WinForms |
Control: PDF |
Published Date: August 31, 2018 |
Last Revised Date: May 3, 2019

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 NuGet.org. 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
        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
        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);
        Controls.Add(label);
        Controls.Add(button);
        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 = @"../../";
    pdfViewer.Load(sourceFile);
     
    //Load the second PDF document to compare
    PdfViewerControl pdfViewer1 = new PdfViewerControl();
    pdfViewer1.RenderingEngine = PdfRenderingEngine.Pdfium;
    pdfViewer1.ReferencePath = @"../../";
    pdfViewer1.Load("../../Data/Source2.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("ComparedPDF.pdf");
    loadedDocument.Close(true);
     
    //This will open the PDF file so, the result will be seen in default PDF viewer 
    System.Diagnostics.Process.Start("ComparedPDF.pdf");
    

 

A complete working sample can be download from PDFComparisonSample.zip

By executing the program, you will get the PDF document as follows. Screenshot of output 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.

 

 

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon