ImageGear PDF v25.2 - Updated
Comparing Documents
Developer Guide > How to Work with... > Common Operations > Comparing Documents

ImageGear gives you the capability to compare text and image contents of documents by means of the API in the ImageGear.Comparison namespace.

Before you can compare PDF documents, you need to enable and initialize the PDF format. See Getting Started with ImageGear PDF for details.

Notes:

The static class ImGearComparison provides functions to check whether the text and/or image content of two documents are different, by means of the following methods:

C#
Copy Code
public static bool HasDifferences(ImGearDocument oldDocument, ImGearDocument newDocument);
public static bool HasTextDifferences(ImGearDocument oldDocument, ImGearDocument newDocument);
public static bool HasImageDifferences(ImGearDocument oldDocument, ImGearDocument newDocument);
VB.NET
Copy Code
Public Shared Function HasDifferences(ByVal oldDocument As ImGearDocument, ByVal newDocument As ImGearDocument) As Boolean
Public Shared Function HasTextDifferences(ByVal oldDocument As ImGearDocument, ByVal newDocument As ImGearDocument) As Boolean
Public Shared Function HasImageDifferences(ByVal oldDocument As ImGearDocument, ByVal newDocument As ImGearDocument) As Boolean

It also allows for the generation of a PDF document where any changes are annotated. The ImGearComparison.Compare method returns an IComparisonResult object which contains the GeneratePDFDocument method that generates the PDF document.

Below, an example is given how to compare two documents and save the document with the changes highlighted as markup.

C#
Copy Code
string fileName1 = "original_document.pdf";
string fileName2 = "modified_document.pdf";

ImGearDocument document1;
ImGearDocument document2;

using (Stream stream = new FileStream(fileName1, FileMode.Open, FileAccess.Read))
{
    document1 = ImGearFileFormats.LoadDocument(stream);
}

using (Stream stream = new FileStream(fileName2, FileMode.Open, FileAccess.Read))
{
    document2 = ImGearFileFormats.LoadDocument(stream);
}

// Compare the two documents.
IComparisonResult comparisonResult = ImGearComparison.Compare(document1, document2);

if (comparisonResult.TotalChangeCount > 0)
{
    // Generate a document with marked up changes.
    ImGearPDFDocument reportDocument = comparisonResult.GeneratePDFDocument(new OutputOptions());

    // Write the report to file.
    using (Stream stream = new FileStream("report.pdf", FileMode.Create, FileAccess.ReadWrite))
    {
        ImGearFileFormats.SaveDocument(reportDocument, stream, 0, ImGearSavingModes.OVERWRITE, ImGearSavingFormats.PDF, null);
    }
}
VB.NET
Copy Code
Dim fileName1 As String = "original_document.pdf"
Dim fileName2 As String = "modified_document.pdf"
Dim document1 As ImGearDocument
Dim document2 As ImGearDocument

Using stream As Stream = New FileStream(fileName1, FileMode.Open, FileAccess.Read)
    document1 = ImGearFileFormats.LoadDocument(stream)
End Using

Using stream As Stream = New FileStream(fileName2, FileMode.Open, FileAccess.Read)
    document2 = ImGearFileFormats.LoadDocument(stream)
End Using

' Compare the two documents.
Dim comparisonResult As IComparisonResult = ImGearComparison.Compare(document1, document2)

If (comparisonResult.TotalChangeCount > 0) Then
    ' Generate a document with marked up changes.
    Dim reportDocument As ImGearPDFDocument = comparisonResult.GeneratePDFDocument(New OutputOptions)
    ' Write the report to file.
    Using stream As Stream = New FileStream("report.pdf", FileMode.Create, FileAccess.ReadWrite)
        ImGearFileFormats.SaveDocument(reportDocument, stream, 0, ImGearSavingModes.OVERWRITE, ImGearSavingFormats.PDF, Nothing)
    End Using
End If

For a detailed description of the comparison API, see ImageGear.Comparison.

 

Is this page helpful?
Yes No
Thanks for your feedback.