Articles in this section
Category / Section

How to compare two PDF Documents in ASP.NET Core

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 Essential PDF Viewer.
  2. Compare the images using the open source ImageMagick library.
  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 Core Web Application project.

    Create ASP.NET Core application

  2. Install the Syncfusion.EJ2.PdfViewer.AspNet.Core.Windows and Magick.NET NuGet package as reference to your ASP.NET Core applications from NuGet.org.

    Syncfusion_PDF_Nuget_installation.png

    ImageMagick_NuGet_package.png

  3. A default controller with the name HomeController.cs gets added to the creation of the ASP.NET Core project. Include the following namespaces in that HomeController.cs file.

using Syncfusion.EJ2.PdfViewer;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
using SkiaSharp;
using Syncfusion.Drawing;
using ImageMagick;
  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 IActionResult ComparePDFDocuments()
{
   //Get the input documents path. 
   string sourceFile1 = _hostingEnvironment.WebRootPath + "/Data/File1.pdf";
   string sourceFile2 = _hostingEnvironment.WebRootPath + "/Data/File2.pdf";
   //Load an existing PDF document.
   FileStream docStream = new FileStream(sourceFile1, FileMode.Open, FileAccess.Read);
   PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
   //Get the size of the page.
   SizeF pageSize = loadedDocument.Pages[0].Size;

   //Use the Syncfusion.EJ2.PdfViewer assembly.
   PdfRenderer pdfExportImage1 = new PdfRenderer();
   //Load a PDF document.
   pdfExportImage1.Load(sourceFile1);

   //Use the Syncfusion.EJ2.PdfViewer assembly.
   PdfRenderer pdfExportImage2 = new PdfRenderer();
   //Load a PDF document.
   pdfExportImage2.Load(sourceFile2);

   //Get the page count.
   int pageCount = 0;
   if (pdfExportImage1.PageCount == pdfExportImage2.PageCount)
   {
       pageCount = pdfExportImage1.PageCount;
   }

   for (int i = 0; i < pageCount; i++)
   {
       //Export a PDF page to the image. 
       SkiaSharp.SKBitmap sKBitmap1 = pdfExportImage1.ExportAsImage(i);
       SkiaSharp.SKBitmap sKBitmap2 = pdfExportImage2.ExportAsImage(i);

       //Convert the SKBitmap to the MagickImage.
       SkiaSharp.SKData encodeData1 = sKBitmap1.Encode(SkiaSharp.SKEncodedImageFormat.Jpeg, 100);
       Stream imageStream1 = encodeData1.AsStream();
       MagickImage image1 = new MagickImage(imageStream1);

       SkiaSharp.SKData encodeData2 = sKBitmap2.Encode(SkiaSharp.SKEncodedImageFormat.Jpeg, 100);
       Stream imageStream2 = encodeData2.AsStream();
       MagickImage compare = new MagickImage(imageStream2);

       //Generate the diff image.
       MagickImage diffImage = new MagickImage();
       double diffPixel = image1.Compare(compare, ErrorMetric.Absolute, diffImage);
       byte[] differenceImage = diffImage.ToByteArray();
       MemoryStream diffImageStream = new MemoryStream(differenceImage);

       //Replace the page with the diff image.
       if (diffPixel > 0)
       {
           //Remove the existing page from a PDF document. 
           loadedDocument.Pages.RemoveAt(i);
           //Insert the diff image as a new page to a PDF document.
           loadedDocument.Pages.Insert(i, pageSize);
           PdfBitmap image = new PdfBitmap(diffImageStream);
           //Draw the diff image 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 by using the file contents, content type, and file name.
   return File(stream, contentType, fileName);
}

A complete working sample can be download from Compare-PDF-documents.zip.
By executing the program, you will get a PDF document as follows.

Output_screenshot.png

Take a moment to peruse the documentation, where you can find the 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 to here to explore the rich set of Syncfusion Essential PDF features.

An online sample link to generate a simple PDF document.

Note: Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or NuGet feed, include a license key in your projects. Refer to this link to learn about generating and registering the Syncfusion license key in your application to use the components without a trail message.

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