Compare PDF Documents in Java
Contents
[
Hide
]
Aspose.PDF for Java provides both side-by-side and graphical comparison APIs for detecting differences between PDF files.
Compare pages and export difference images
Use this example when you need image-based difference output for a specific pair of PDF pages.
- Open both source PDF Document objects. «««< HEAD
- Use GraphicalPdfComparer to get the page-level ImagesDifference. =======
- Use ‘GraphicalPdfComparer’ to get the page-level ‘ImagesDifference’.
fix-java-docs
- Export the generated difference images and dispose the comparison result.
public static void comparePdfWithGetDifferenceMethod(
Path inputFile1, Path inputFile2, Path diffOutputFile, Path destinationOutputFile) throws Exception {
try (Document document1 = new Document(inputFile1.toString());
Document document2 = new Document(inputFile2.toString())) {
GraphicalPdfComparer comparer = new GraphicalPdfComparer();
ImagesDifference imagesDifference = comparer.getDifference(document1.getPages().get_Item(1),
document2.getPages().get_Item(1));
ImageIO.write(imagesDifference.differenceToImage(Color.getRed(), Color.getWhite()),
"png", diffOutputFile.toFile());
ImageIO.write(imagesDifference.getDestinationImage(), "png", destinationOutputFile.toFile());
imagesDifference.dispose();
}
System.out.println("Difference images saved to " + diffOutputFile + " and " + destinationOutputFile);
}
Compare specific pages side by side
Use this example when only selected pages should be compared and saved as a side-by-side PDF result.
- Open both source PDF Document objects. «««< HEAD
- Configure SideBySideComparisonOptions for the required comparison mode. =======
- Configure ‘SideBySideComparisonOptions’ for the required comparison mode.
fix-java-docs
- Compare the selected pages and save the output PDF.
public static void comparingSpecificPages(Path inputFile1, Path inputFile2, Path outputFile) {
try (Document document1 = new Document(inputFile1.toString());
Document document2 = new Document(inputFile2.toString())) {
SideBySideComparisonOptions options = new SideBySideComparisonOptions();
options.setAdditionalChangeMarks(true);
options.setComparisonMode(ComparisonMode.IgnoreSpaces);
SideBySidePdfComparer.compare(document1.getPages().get_Item(1), document2.getPages().get_Item(1),
outputFile.toString(), options);
}
System.out.println("Specific pages comparison saved to " + outputFile);
}
Compare full PDF documents graphically
This example generates a graphical PDF report that highlights visual differences across the entire documents.
- Open both source PDF Document objects.
- Configure the GraphicalPdfComparer threshold, color, and resolution.
- Compare the full documents and save the graphical output PDF.
public static void comparePdfWithCompareDocumentsToPdfMethod(Path inputFile1, Path inputFile2, Path outputFile) {
try (Document document1 = new Document(inputFile1.toString());
Document document2 = new Document(inputFile2.toString())) {
GraphicalPdfComparer pdfComparer = new GraphicalPdfComparer();
pdfComparer.setThreshold(3.0);
pdfComparer.setColor(Color.getBlue());
pdfComparer.setResolution(new Resolution(300));
pdfComparer.compareDocumentsToPdf(document1, document2, outputFile.toString());
}
System.out.println("Graphical comparison saved to " + outputFile);
}
Compare entire documents side by side
Use this example when the whole documents should be compared page by page in a side-by-side PDF output.
- Open both source PDF Document objects.
- Configure SideBySideComparisonOptions for the desired comparison behavior.
- Compare the full documents and save the result as a PDF.
public static void comparingEntireDocuments(Path inputFile1, Path inputFile2, Path outputFile) {
try (Document document1 = new Document(inputFile1.toString());
Document document2 = new Document(inputFile2.toString())) {
SideBySideComparisonOptions options = new SideBySideComparisonOptions();
options.setAdditionalChangeMarks(true);
options.setComparisonMode(ComparisonMode.IgnoreSpaces);
SideBySidePdfComparer.compare(document1, document2, outputFile.toString(), options);
}
System.out.println("Entire document comparison saved to " + outputFile);
}