Compare PDF documents in C++

Aspose.PDF for C++ provides APIs to compare selected PDF pages or entire documents. Use SideBySidePdfComparer for marked page pairs and GraphicalPdfComparer for image-based differences and graphical PDF reports.

The following examples accept input and output file paths. Configure your project to include and link Aspose.PDF for C++ and create output directories before running the functions.

Compare pages and export difference images

Export the graphical differences between the first page of each input PDF as a PNG image. The example also saves a reconstructed destination image.

  1. Open both input PDFs as Document objects.
  2. Create a GraphicalPdfComparer and select page 1 from each document.
  3. Call GetDifference() to obtain an ImagesDifference object.
  4. Generate a difference image with DifferenceToImage(), using red for changes and white for the background.
  5. Save the difference image as PNG.
  6. Retrieve the destination image with GetDestinationImage() and save it as PNG.
  7. Release the comparison result with Dispose().

#include <iostream>
#include <drawing/bitmap.h>
#include <drawing/imaging/image_format.h>
#include <Aspose.PDF.Cpp/Color.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Comparison/GraphicalComparison/GraphicalPdfComparer.h>
#include <Aspose.PDF.Cpp/Comparison/GraphicalComparison/ImagesDifference.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Comparison;

void ComparePdfWithGetDifferenceMethod(System::String inputFile1, System::String inputFile2,
    System::String diffOutputFile, System::String destinationOutputFile)
{
    auto document1 = MakeObject<Document>(inputFile1);
    auto document2 = MakeObject<Document>(inputFile2);
    auto comparer = MakeObject<GraphicalPdfComparer>();
    auto imagesDifference = comparer->GetDifference(document1->get_Pages()->idx_get(1),
        document2->get_Pages()->idx_get(1));

    auto differenceImage = imagesDifference->DifferenceToImage(Color::get_Red(), Color::get_White());
    differenceImage->Save(diffOutputFile, System::Drawing::Imaging::ImageFormat::get_Png());
    auto destinationImage = imagesDifference->GetDestinationImage();
    destinationImage->Save(destinationOutputFile, System::Drawing::Imaging::ImageFormat::get_Png());
    imagesDifference->Dispose();

    std::cout << "Difference images saved to " << diffOutputFile << " and " << destinationOutputFile << std::endl;
}

Compare specific pages side by side

Compare the first page of each PDF and save the marked pages in an output PDF. Both input documents must contain at least one page.

Page collection indices start at 1. Change the selected indices to compare another pair of pages. Open the output in two-page view to inspect the pair side by side.

  1. Open both input PDFs as Document objects.
  2. Create SideBySideComparisonOptions.
  3. Enable additional change marks and set ComparisonMode to IgnoreSpaces.
  4. Select page 1 from each document.
  5. Pass the selected pages, output path, and options to SideBySidePdfComparer::Compare().

#include <iostream>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Comparison/SideBySideComparison/SideBySideComparisonOptions.h>
#include <Aspose.PDF.Cpp/Comparison/SideBySideComparison/SideBySidePdfComparer.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Comparison;

void ComparingSpecificPages(System::String inputFile1, System::String inputFile2, System::String outputFile)
{
    auto document1 = MakeObject<Document>(inputFile1);
    auto document2 = MakeObject<Document>(inputFile2);
    auto options = MakeObject<SideBySideComparisonOptions>();
    options->set_AdditionalChangeMarks(true);
    options->set_ComparisonMode(ComparisonMode::IgnoreSpaces);

    SideBySidePdfComparer::Compare(document1->get_Pages()->idx_get(1),
        document2->get_Pages()->idx_get(1), outputFile, options);

    std::cout << "Specific pages comparison saved to " << outputFile << std::endl;
}

Compare full PDF documents graphically

Generate a PDF report that highlights graphical differences between two documents. Configure the comparison sensitivity, highlight color, and rendering resolution before creating the report.

  1. Open both input PDFs as Document objects.
  2. Create a GraphicalPdfComparer.
  3. Set the comparison threshold to 3.0 percent and the highlight color to blue.
  4. Set a resolution of 300 dpi with Resolution.
  5. Call CompareDocumentsToPdf() with both documents and the output file path.

#include <iostream>
#include <Aspose.PDF.Cpp/Color.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Devices/Resolution.h>
#include <Aspose.PDF.Cpp/Comparison/GraphicalComparison/GraphicalPdfComparer.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Comparison;

void ComparePdfWithCompareDocumentsToPdfMethod(System::String inputFile1, System::String inputFile2,
    System::String outputFile)
{
    auto document1 = MakeObject<Document>(inputFile1);
    auto document2 = MakeObject<Document>(inputFile2);
    auto comparer = MakeObject<GraphicalPdfComparer>();
    comparer->set_Threshold(3.0);
    comparer->set_Color(Color::get_Blue());
    comparer->set_Resolution(MakeObject<Devices::Resolution>(300));
    comparer->CompareDocumentsToPdf(document1, document2, outputFile);

    std::cout << "Graphical comparison saved to " << outputFile << std::endl;
}

Compare entire documents side by side

Compare two PDF files page by page and save the marked results in a single PDF. The output alternates pages from the first and second document.

  1. Open both input PDFs as Document objects.
  2. Create SideBySideComparisonOptions.
  3. Enable additional change marks and set ComparisonMode to IgnoreSpaces.
  4. Pass both documents, the output path, and the options to SideBySidePdfComparer::Compare().
  5. Open the resulting PDF in two-page view to review corresponding pages side by side.

#include <iostream>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Comparison/SideBySideComparison/SideBySideComparisonOptions.h>
#include <Aspose.PDF.Cpp/Comparison/SideBySideComparison/SideBySidePdfComparer.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Comparison;

void ComparingEntireDocuments(System::String inputFile1, System::String inputFile2, System::String outputFile)
{
    auto document1 = MakeObject<Document>(inputFile1);
    auto document2 = MakeObject<Document>(inputFile2);
    auto options = MakeObject<SideBySideComparisonOptions>();
    options->set_AdditionalChangeMarks(true);
    options->set_ComparisonMode(ComparisonMode::IgnoreSpaces);

    SideBySidePdfComparer::Compare(document1, document2, outputFile, options);

    std::cout << "Entire document comparison saved to " << outputFile << std::endl;
}