ImageGear .NET - Updated May 30, 2018
Comparing Documents
User Guide > How to Work with... > Common Operations > Comparing Documents

ImageGear .NET 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);

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();

    // 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);
    }
}

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