Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created October 23, 2023 06:26
Show Gist options
  • Save aspose-com-gists/3d2eecfab1e32d3246357a264a6467cf to your computer and use it in GitHub Desktop.
Save aspose-com-gists/3d2eecfab1e32d3246357a264a6467cf to your computer and use it in GitHub Desktop.
Compare Documents in Java
// Load PDF files
Document PDF1 = new Document("first.pdf");
Document PDF2 = new Document("second.pdf");
// Convert PDF files to editable Word format
PDF1.save("first.docx", SaveFormat.DOCX);
PDF2.save("second.docx", SaveFormat.DOCX);
// Load converted Word documents
Document DOC1 = new Document("first.docx");
Document DOC2 = new Document("second.docx");
// Set comparison options
CompareOptions options = new CompareOptions();
options.setIgnoreFormatting(true);
options.setIgnoreHeadersAndFooters(true);
options.setIgnoreCaseChanges(true);
options.setIgnoreTables(true);
options.setIgnoreFields(true);
options.setIgnoreComments(true);
options.setIgnoreTextboxes (true);
options.setIgnoreFootnotes(true);
// DOC1 will contain changes as revisions after comparison
DOC1.compare(DOC2, "user", new Date(), options);
if (DOC1.getRevisions().getCount() > 0)
// Save resultant file as PDF
DOC1.save("compared.pdf", SaveFormat.PDF);
else
System.out.println("Documents are equal");
// Load presentations
Presentation presentation1 = new Presentation("first.pptx");
try {
Presentation presentation2 = new Presentation("second.pptx");
try {
// Loop through slides
for (int i = 0; i < presentation1.getMasters().size(); i++)
{
for (int j = 0; j < presentation2.getMasters().size(); j++)
{
// Compare slides
if (presentation1.getMasters().get_Item(i).equals(presentation2.getMasters().get_Item(j)))
System.out.println(String.format("SomePresentation1 MasterSlide#%d is equal to SomePresentation2 MasterSlide#%d", i, j));
}
}
} finally {
presentation2.dispose();
}
} finally {
presentation1.dispose();
}
// Load Word documents
Document DOC1 = new Document("first.docx");
Document DOC2 = new Document("second.docx");
// Set comparison options
CompareOptions options = new CompareOptions();
options.setIgnoreFormatting(true);
options.setIgnoreHeadersAndFooters(true);
options.setIgnoreCaseChanges(true);
options.setIgnoreTables(true);
options.setIgnoreFields(true);
options.setIgnoreComments(true);
options.setIgnoreTextboxes (true);
options.setIgnoreFootnotes(true);
// DOC1 will contain changes as revisions after comparison
DOC1.compare(DOC2, "user", new Date(), options);
if (DOC1.getRevisions().getCount() > 0)
// Save with comparison results
DOC1.save("compared.docx", SaveFormat.DOCX);
else
System.out.println("Documents are equal");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment