Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Please note that all comparing tools are available in Aspose.PDF.Drawing library.
When working with PDF documents, there are times when you need to compare the content of two documents to identify differences. The Aspose.PDF for .NET library provides a powerful toolset for this purpose. In this article, we’ll explore how to compare PDF documents using a couple of simple code snippets.
The comparison functionality in Aspose.PDF allows you to compare two PDF documents page by page. You can choose to compare either specific pages or entire documents. The resulting comparison document highlights differences, making it easier to identify changes between the two files.
Here is a list of possible ways to compare PDF documents using Aspose.PDF for .NET library:
Comparing Specific Pages - Compare the first pages of two PDF documents.
Comparing Entire Documents - Compare the entire content of two PDF documents.
Compare PDF documents graphically:
Compare PDF with GetDifference method - individual images where changes are marked.
Compare PDF with CompareDocumentsToPdf method - PDF document with images where changes are marked.
The first code snippet demonstrates how to compare the first pages of two PDF documents.
Steps:
Document Initialization. The code starts by initializing two PDF documents using their respective file paths (documentPath1 and documentPath2). The paths are specified as empty strings for now, but in practice, you would replace these with the actual file paths.
Comparison Process.
‘AdditionalChangeMarks = true’- this option ensures that additional change markers are displayed. These markers highlight differences that might be present on other pages, even if they are not on the current page being compared.
‘ComparisonMode = ComparisonMode.IgnoreSpaces’ - this mode tells the comparer to ignore spaces in the text, focusing only on changes within words.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ComparingSpecificPages()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentCompare();
// Open PDF documents
using (var document1 = new Aspose.Pdf.Document(dataDir + "ComparingSpecificPages1.pdf"))
{
using (var document2 = new Aspose.Pdf.Document(dataDir + "ComparingSpecificPages2.pdf"))
{
// Compare
Aspose.Pdf.Comparison.SideBySidePdfComparer.Compare(document1.Pages[1], document2.Pages[1], dataDir + "ComparingSpecificPages_out.pdf", new Aspose.Pdf.Comparison.SideBySideComparisonOptions
{
AdditionalChangeMarks = true,
ComparisonMode = Aspose.Pdf.Comparison.ComparisonMode.IgnoreSpaces
});
}
}
}
The second code snippet expands the scope to compare the entire content of two PDF documents.
Steps:
Document Initialization. Just like in the first example, two PDF documents are initialized with their file paths.
Comparison Process.
Entire Document Comparison - unlike the first snippet, this code compares the entire content of the two documents.
Comparison Options - the options are the same as in the first snippet, ensuring that spaces are ignored, and additional change markers are displayed.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ComparingEntireDocuments()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentCompare();
// Open PDF documents
using (var document1 = new Aspose.Pdf.Document(dataDir + "ComparingEntireDocuments1.pdf"))
{
using (var document2 = new Aspose.Pdf.Document(dataDir + "ComparingEntireDocuments2.pdf"))
{
// Compare
Aspose.Pdf.Comparison.SideBySidePdfComparer.Compare(
document1,
document2,
dataDir + "ComparingEntireDocuments_out.pdf",
new Aspose.Pdf.Comparison.SideBySideComparisonOptions
{
AdditionalChangeMarks = true,
ComparisonMode = Aspose.Pdf.Comparison.ComparisonMode.IgnoreSpaces
});
}
}
}
The comparison results generated by these snippets are PDF documents that you can open in a viewer like Adobe Acrobat. If you use the Two-page view in Adobe Acrobat, you’ll see the changes side by side:
By setting ‘AdditionalChangeMarks’ to ’true’, you can also see markers for changes that may occur on other pages, even if those changes aren’t on the current page being viewed.
Aspose.PDF for .NET provides robust tools for comparing PDF documents, whether you need to compare specific pages or entire documents. By using options like ‘AdditionalChangeMarks’ and different ‘ComparisonMode settings’, you can tailor the comparison process to your specific needs. The resulting document provides a clear, side-by-side view of changes, making it easier to track revisions and ensure document accuracy.
When collaborating on documents, especially in professional environments, you often end up with multiple versions of the same file.
You can use the GraphicalPdfComparer class to compare PDF documents and pages. The class is suitable for comparing changes in a page’s graphic content.
With Aspose.PDF for .NET, it’s possible to compare documents and pages and output the comparison result to a PDF document or image file.
You can set the following class properties:
The class has a method that allows you to get page image differences in a form suitable for further processing: ImagesDifference GetDifference(Page page1, Page page2).
This method returns an object of the ImagesDifference class, which contains an image of the first page being compared and an array of differences. The array of differences and the original image has the RGB24bpp pixel format.
ImagesDifference allows you to generate a different image and get an image of the second page being compared by adding an array of differences to the original image. To do this, use the ImagesDifference.GetDestinationImage and ImagesDifference.DifferenceToImage methods.
The provided code defines a method GetDifference that compares two PDF documents and generates visual representations of the differences between them.
This method compares the first pages of two PDF files and generates two PNG images:
This process can be useful for visually comparing changes or differences between two versions of a document.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ComparePDFWithGetDifferenceMethod()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentCompare();
// Open PDF documents
using (var document1 = new Aspose.Pdf.Document(dataDir + "ComparePDFWithGetDifferenceMethod1.pdf"))
{
using (var document2 = new Aspose.Pdf.Document(dataDir + "ComparePDFWithGetDifferenceMethod2.pdf"))
{
// Create comparer
var comparer = new Aspose.Pdf.Comparison.GraphicalPdfComparer();
// Compare
using (var imagesDifference = comparer.GetDifference(document1.Pages[1], document2.Pages[1]))
{
using (var diffImg = imagesDifference.DifferenceToImage(Aspose.Pdf.Color.Red, Aspose.Pdf.Color.White))
{
diffImg.Save(dataDir + "ComparePDFWithGetDifferenceMethodDiffPngFilePath_out.png");
}
using (var destImg = imagesDifference.GetDestinationImage())
{
destImg.Save(dataDir + "ComparePDFWithGetDifferenceMethodDestPngFilePath_out.png");
}
}
}
}
}
The provided code snippet used the CompareDocumentsToPdf method, which compares two documents and generates a PDF report of the comparison results.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ComparePDFWithCompareDocumentsToPdfMethod()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentCompare();
// Open PDF documents
using (var document1 = new Aspose.Pdf.Document(dataDir + "ComparePDFWithCompareDocumentsToPdfMethod1.pdf"))
{
using (var document2 = new Aspose.Pdf.Document(dataDir + "ComparePDFWithCompareDocumentsToPdfMethod2.pdf"))
{
// Create comparer
var comparer = new Aspose.Pdf.Comparison.GraphicalPdfComparer()
{
Threshold = 3.0,
Color = Aspose.Pdf.Color.Blue,
Resolution = new Aspose.Pdf.Devices.Resolution(300)
};
// Compare
comparer.CompareDocumentsToPdf(document1, document2, dataDir + "compareDocumentsToPdf_out.pdf");
}
}
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.