Read the complete article on how to compare documents in Java: https://blog.aspose.com/total/document-comparison-in-java/
Created
October 23, 2023 06:26
-
-
Save aspose-com-gists/3d2eecfab1e32d3246357a264a6467cf to your computer and use it in GitHub Desktop.
Compare Documents in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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