Articles in this section
Category / Section

How to Compare two PDF Documents in ASP.NET Platform

5 mins read

The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. With the integration of Essential PDF and Essential PDF Viewer libraries, a streamlined approach to comparing text within two PDF documents becomes feasible. This process of a PDF document comparison entails the following sequential steps:

  1. Convert PDF documents to images with the help of PDF to Image converter.
  2. Compare the images and generate a different image.
  3. Save different images as PDF documents with the help of the Essential PDF library.

Steps to compare PDF documents programmatically

  1. Create a new C# ASP.NET Web Application project.
    Screenshot (1288).png
  2. Install the Syncfusion.PdfToImageConverter.AspNet.Mvc5 and Syncfusion.Pdf.AspNet.Mvc5 NuGet package as reference to your ASP.NET applications from NuGet.org.
    Screenshot (1293).png
    Screenshot (1294).png
  3. A default controller with the name HomeController.cs gets added to the creation of the ASP.NET project. Include the following namespaces in that HomeController.cs file.
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
using Syncfusion.PdfToImageConverter;
using System;
using System.Drawing;
using System.IO;
using System.Web.Mvc;
  1. A default action method named Index will be present in HomeController.cs. Right-click on the Index method and select “Go To View”, where you will be directed to its associated view page Index.cshtml. Add a new button in the Index.cshtml as follows.
@{Html.BeginForm("ComparePDFDocuments", "Home", FormMethod.Get);
   {
       <div>
           <input type="submit" value="Compare PDF documents" style="width:200px;height:27px" />
       </div>
   }
   Html.EndForm();
} 
  1. Add a new action method named ComparePDFDocuments in the HomeController.cs file and include the following code example to compare the PDF documents.
public ActionResult ComparePDFDocuments()
{
   //Get the input documents path. 
   string sourceFile1 = Server.MapPath("~/App_Data/Input1.pdf");
   string sourceFile2 = Server.MapPath("~/App_Data/Input2.pdf");
   //Load an existing PDF document.
   FileStream docStream1 = new FileStream(sourceFile1, FileMode.Open, FileAccess.Read);
   FileStream docStream2 = new FileStream(sourceFile2, FileMode.Open, FileAccess.Read);
   PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream1);

   //Initialize PDF to Image converter.
   PdfToImageConverter pdfExportImage1 = new PdfToImageConverter();
   //Load a PDF document.
   pdfExportImage1.Load(docStream1);

   //Initialize PDF to Image converter.
   PdfToImageConverter pdfExportImage2 = new PdfToImageConverter();
   //Load a PDF document.
   pdfExportImage2.Load(docStream2);

   //Get the page count.
   int pageCount = 0;
   if (pdfExportImage1.PageCount == pdfExportImage2.PageCount)
   {
       pageCount = pdfExportImage1.PageCount;
   }
   else
   {
       string ErrorMessage = "The document is not same";
       return View(ErrorMessage);
   }
   for (int i = 0; i < pageCount; i++)
   {
       //Convert PDF to Image.
       Stream imageStream1 = pdfExportImage1.Convert(i, false, false);
       Bitmap image1 = new Bitmap(imageStream1);

       //Convert PDF to Image.
       Stream imageStream2 = pdfExportImage2.Convert(i, false, false);
       Bitmap image2 = new Bitmap(imageStream2);

       // Compare the images and generate a different image.
       Bitmap diffImage = GenerateDifferenceImage(image1, image2);

       //Convert bitmap to a memory stream.
       MemoryStream diffImageStream = new MemoryStream();
       diffImage.Save(diffImageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
       diffImageStream.Position = 0;

       //Get the current page size.
       SizeF pageSize = loadedDocument.Pages[i].Size;

       //Remove the existing page from a PDF document. 
       loadedDocument.Pages.RemoveAt(i);

       //Insert the different images as a new page to a PDF document.
       loadedDocument.Pages.Insert(i, pageSize);
       PdfBitmap image = new PdfBitmap(diffImageStream);
       //Draw the different images on a PDF page.
       loadedDocument.Pages[i].Graphics.DrawImage(image, 0, 0, loadedDocument.Pages[i].Graphics.ClientSize.Width, loadedDocument.Pages[i].Graphics.ClientSize.Height);

   }

   //Create a memory stream.
   MemoryStream stream = new MemoryStream();
   //Save a PDF document to a stream.
   loadedDocument.Save(stream);
   //If the position is not set to '0', then a PDF will be empty.
   stream.Position = 0;
   //Close the document.
   loadedDocument.Close(true);
   //Define the content type for the PDF file.
   string contentType = "application/pdf";
   //Define the file name.
   string fileName = "ComparedPDF.pdf";
   //Create a FileContentResult object using the file contents, content type, and file name.
   return File(stream, contentType, fileName);
}
static Bitmap GenerateDifferenceImage(Bitmap image1, Bitmap image2)
{
   int width = Math.Min(image1.Width, image2.Width);
   int height = Math.Min(image1.Height, image2.Height);

   Bitmap diffImage = new Bitmap(width, height);

   // Loop through each pixel of the images.
   for (int y = 0; y < height; y++)
   {
       for (int x = 0; x < width; x++)
       {
           // Get the color of the pixel in each image.
           Color color1 = image1.GetPixel(x, y);
           Color color2 = image2.GetPixel(x, y);

           // Compare the colors of the pixels.
           if (color1 != color2)
           {
               // If colors are different, mark the pixel in the different images.
               diffImage.SetPixel(x, y, Color.Red);
           }
           else
           {
               // If colors are the same, keep the pixel unchanged.
               diffImage.SetPixel(x, y, Color.Transparent);
           }
       }
   }

   return diffImage;
}

A complete working sample can be downloaded from Compare-PDF-documents.zip.

By executing the program, you will get a PDF document as follows.Screenshot (1292).png

Take a moment to peruse the documentation, where you can find features like PDF form filling, extract text from PDF document, Extract images from PDF document, and protect PDF documents, and more with code examples.

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

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