This sample shows how to check if two PDF documents are equal (i.e. have the same contents).
A PDF file consists of PDF objects. Some PDF objects contain unique data like content hash, creation time, etc. So you can not compare PDF files as byte streams because PDF documents with the same contents may have been saved with different compression, formatting and different unique data.
The PdfDocument.DocumentsAreEqual method compares two PDF documents as collections of PDF objects. It reports that documents are equal if all corresponding PDF objects are equal except some known fields (like CreationDate, or file IDs) that are really file properties and not contents properties.
using System.Diagnostics; using System.Text; using System.Windows.Forms; namespace BitMiracle.Docotic.Pdf.Samples { public static class CompareDocuments { public static void Main() { // NOTE: // When used in trial mode, the library imposes some restrictions. // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx // for more information. const string originalFile = @"Sample Data\Link.pdf"; PdfDocument first = new PdfDocument(originalFile); first.Save("first.pdf"); first.Dispose(); PdfDocument second = new PdfDocument(originalFile); second.Save("second.pdf"); second.Dispose(); PdfDocument third = new PdfDocument(originalFile); third.Pages[0].Canvas.DrawString("Hello"); third.Save("third.pdf"); third.Dispose(); StringBuilder message = new StringBuilder(); bool equals = PdfDocument.DocumentsAreEqual("first.pdf", "second.pdf", ""); message.AppendLine("first.pdf equals to second.pdf?\r\n" + (equals ? "Yes" : "No")); message.AppendLine(); equals = PdfDocument.DocumentsAreEqual("first.pdf", "third.pdf", ""); message.AppendLine("first.pdf equals to third.pdf?\r\n" + (equals ? "Yes" : "No")); MessageBox.Show(message.ToString()); } } }
Imports System.Diagnostics Imports System.Text Imports System.Windows.Forms Imports Microsoft.VisualBasic Imports BitMiracle.Docotic.Pdf Namespace BitMiracle.Docotic.Pdf.Samples Public NotInheritable Class CompareDocuments Public Shared Sub Main() ' NOTE: ' When used in trial mode, the library imposes some restrictions. ' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx ' for more information. Const originalFile As String = "Sample Data\Link.pdf" Dim first As New PdfDocument(originalFile) first.Save("first.pdf") first.Dispose() Dim second As New PdfDocument(originalFile) second.Save("second.pdf") second.Dispose() Dim third As New PdfDocument(originalFile) third.Pages(0).Canvas.DrawString("Hello") third.Save("third.pdf") third.Dispose() Dim message As New StringBuilder() message.AppendLine("first.pdf equals to second.pdf?") If PdfDocument.DocumentsAreEqual("first.pdf", "second.pdf", "") Then message.AppendLine("Yes") Else message.AppendLine("No") End If message.AppendLine() message.AppendLine("first.pdf equals to third.pdf?") If PdfDocument.DocumentsAreEqual("first.pdf", "third.pdf", "") Then message.AppendLine("Yes") Else message.AppendLine("No") End If MessageBox.Show(message.ToString()) End Sub End Class End Namespace
