DOCX Compare - Java Sample Code

Requirements

This is sample code for using Apryse SDK to compare two DOCX documents and produce an output DOCX document where the differences are recorded as tracked changes. The comparison runs entirely within the SDK with no external or system dependencies, producing identical results across Windows, Linux, macOS, and Android. Sample code is provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Python, Ruby, Go, Objective-C, and VB.

Implementation steps

To compare DOCX files with Apryse Server SDK:

Step 1: Follow get started with Server SDK in your preferred language or framework.
Step 2: Add the sample code provided in this guide.

Learn more about Apryse Server SDK.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2026 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import com.pdftron.common.PDFNetException;
7import com.pdftron.office.DOCXCompare;
8import com.pdftron.office.DOCXCompareOptions;
9import com.pdftron.office.DOCXCompareResult;
10import com.pdftron.pdf.PDFNet;
11
12//---------------------------------------------------------------------------------------
13// The following sample illustrates how to use the Office.DOCXCompare utility class
14// to compare two MS Word (DOCX) documents and produce a new DOCX document containing
15// the differences between them as tracked changes.
16//
17// This comparison is performed entirely within the PDFNet and has *no* external or
18// system dependencies -- Comparison results will be the same whether on Windows,
19// Linux or Android.
20//
21// Please contact us if you have any questions.
22//---------------------------------------------------------------------------------------
23public class DOCXCompareTest {
24
25 static String input_path = "../../TestFiles/";
26 static String output_path = "../../TestFiles/Output/";
27 // Provide your own original and revised versions of a DOCX document here.
28 static String original_filename = "SYH_Letter.docx";
29 static String revised_filename = "SYH_Letter_revision2.docx";
30 static String output_filename = "SYH_Letter_changes.docx";
31
32 public static void main(String[] args) {
33 PDFNet.initialize(PDFTronLicense.Key());
34 PDFNet.setResourcesPath("../../../Resources");
35
36 try {
37 DOCXCompareOptions options = new DOCXCompareOptions();
38
39 // Compare the two DOCX documents, writing the differences as tracked
40 // changes into the output DOCX document.
41 DOCXCompareResult result = DOCXCompare.compare(input_path + original_filename, input_path + revised_filename, output_path + output_filename, options);
42
43 if (result.differencesDetected()) {
44 System.out.println("Differences detected, saved to " + output_filename);
45 }
46 else {
47 System.out.println("No difference detected");
48 }
49 }
50 catch (PDFNetException e) {
51 e.printStackTrace();
52 System.out.println(e);
53 }
54
55 // And we're done!
56 System.out.println("Done.");
57
58 PDFNet.terminate();
59 }
60
61}
62
63

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales