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.
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
6using System;
7
8using pdftron;
9using pdftron.Common;
10using pdftron.Office;
11
12//---------------------------------------------------------------------------------------
13// The following sample illustrates how to use the pdftron.Office.DOCXCompare utility
14// class to compare two MS Word (DOCX) documents and produce a new DOCX document
15// containing 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//---------------------------------------------------------------------------------------
23
24namespace DOCXCompareTestCS
25{
26 class Class1
27 {
28 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
29 static Class1() {}
30
31 static string input_path = "../../../../TestFiles/";
32 static string output_path = "../../../../TestFiles/Output/";
33
34 // Provide your own original and revised versions of a DOCX document here.
35 static string original_filename = "SYH_Letter.docx";
36 static string revised_filename = "SYH_Letter_revision2.docx";
37 static string output_filename = "SYH_Letter_changes.docx";
38
39 /// <summary>
40 /// The following sample illustrates how to compare two DOCX documents using 'pdftron.Office.DOCXCompare'.
41 /// </summary>
42 static void Main(string[] args)
43 {
44 PDFNet.Initialize(PDFTronLicense.Key);
45 PDFNet.SetResourcesPath("../../../../../Resources");
46
47 try
48 {
49 DOCXCompareOptions options = new DOCXCompareOptions();
50
51 // Compare the two DOCX documents, writing the differences as tracked
52 // changes into the output DOCX document.
53 DOCXCompareResult result = DOCXCompare.Compare(input_path + original_filename, input_path + revised_filename, output_path + output_filename, options);
54
55 // And we're done!
56 if (result.DifferencesDetected())
57 {
58 Console.WriteLine("Differences detected, saved to " + output_filename);
59 }
60 else
61 {
62 Console.WriteLine("No difference detected");
63 }
64 }
65 catch (PDFNetException e)
66 {
67 Console.WriteLine(e.Message);
68 }
69
70 Console.WriteLine("Done.");
71
72 PDFNet.Terminate();
73 }
74 }
75}
76
77
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2026 by Apryse Software Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 "fmt"
9 "testing"
10 "flag"
11 . "github.com/pdftron/pdftron-go/v2"
12)
13
14var licenseKey string
15var modulePath string
16
17func init() {
18 flag.StringVar(&licenseKey, "license", "", "License key for Apryse SDK")
19 flag.StringVar(&modulePath, "modulePath", "", "Module path for Apryse SDK")
20}
21
22//------------------------------------------------------------------------------
23// The following sample illustrates how to use the Office.DOCXCompare utility
24// class to compare two MS Word (DOCX) documents and produce a new DOCX document
25// containing the differences between them as tracked changes.
26//
27// This comparison is performed entirely within the PDFNet and has *no* external
28// or system dependencies -- Comparison results will be the same whether on
29// Windows, Linux or Android.
30//
31// Please contact us if you have any questions.
32//------------------------------------------------------------------------------
33
34// Relative path to the folder containing the test files.
35var inputPath = "../TestFiles/"
36var outputPath = "../TestFiles/Output/"
37
38// Provide your own original and revised versions of a DOCX document here.
39var originalFilename = "SYH_Letter.docx"
40var revisedFilename = "SYH_Letter_revision2.docx"
41var outputFilename = "SYH_Letter_changes.docx"
42
43func TestDOCXCompare(t *testing.T){
44 // The first step in every application using PDFNet is to initialize the
45 // library. The library is usually initialized only once, but calling
46 // Initialize() multiple times is also fine.
47 PDFNetInitialize(licenseKey)
48 PDFNetSetResourcesPath("../../Resources")
49
50 options := NewDOCXCompareOptions()
51
52 // Compare the two DOCX documents, writing the differences as tracked
53 // changes into the output DOCX document.
54 result := DOCXCompareCompare(inputPath + originalFilename, inputPath + revisedFilename, outputPath + outputFilename, options)
55
56 // And we're done!
57 if result.DifferencesDetected() {
58 fmt.Println("Differences detected, saved to " + outputFilename)
59 } else {
60 fmt.Println("No difference detected")
61 }
62
63 PDFNetTerminate()
64 fmt.Println("Done.")
65}
66
67
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
1//------------------------------------------------------------------------------
2// Copyright (c) 2001-2026 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//------------------------------------------------------------------------------
5
6#include <iostream>
7#include <PDF/PDFNet.h>
8#include <Office/DOCXCompare.h>
9#include "../../LicenseKey/CPP/LicenseKey.h"
10
11//------------------------------------------------------------------------------
12// The following sample illustrates how to use the Office::DOCXCompare utility
13// class to compare two MS Word (DOCX) documents and produce a new DOCX document
14// containing the differences between them as tracked changes.
15//
16// This comparison is performed entirely within the PDFNet and has *no*
17// external or system dependencies -- Comparison results will be
18// the same whether on Windows, Linux or Android.
19//
20// Please contact us if you have any questions.
21//------------------------------------------------------------------------------
22
23using namespace pdftron;
24using namespace Office;
25
26UString input_path = "../../TestFiles/";
27UString output_path = "../../TestFiles/Output/";
28
29int main(int argc, char *argv[])
30{
31 // The first step in every application using PDFNet is to initialize the
32 // library. The library is usually initialized only once, but calling
33 // Initialize() multiple times is also fine.
34 int ret = 0;
35
36 PDFNet::Initialize(LicenseKey);
37 PDFNet::SetResourcesPath("../../../Resources");
38
39 // Provide your own original and revised versions of a DOCX document here.
40 UString original_filename = "SYH_Letter.docx";
41 UString revised_filename = "SYH_Letter_revision2.docx";
42 UString output_filename = "SYH_Letter_changes.docx";
43
44 try
45 {
46 DOCXCompareOptions options;
47
48 // Compare the two DOCX documents, writing the differences as tracked
49 // changes into the output DOCX document.
50 DOCXCompareResult result = DOCXCompare::Compare(
51 input_path + original_filename,
52 input_path + revised_filename,
53 output_path + output_filename,
54 &options);
55
56 if (result.DifferencesDetected())
57 {
58 std::cout << "Differences detected, saved to " << output_filename << std::endl;
59 }
60 else
61 {
62 std::cout << "No difference detected" << std::endl;
63 }
64 }
65 catch (Common::Exception& e)
66 {
67 std::cout << e << std::endl;
68 ret = 1;
69 }
70 catch (...)
71 {
72 std::cout << "Unknown Exception" << std::endl;
73 ret = 1;
74 }
75
76 PDFNet::Terminate();
77 std::cout << "Done.\n";
78 return ret;
79}
80
81
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2026 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6//------------------------------------------------------------------------------
7// The following sample illustrates how to use the PDFNet.DOCXCompare utility
8// class to compare two MS Word (DOCX) documents and produce a new DOCX document
9// containing the differences between them as tracked changes.
10//
11// This comparison is performed entirely within the PDFNet and has *no*
12// external or system dependencies -- Comparison results will be
13// the same whether on Windows, Linux or Android.
14//
15// Please contact us if you have any questions.
16//------------------------------------------------------------------------------
17
18const { PDFNet } = require('../../lib/pdfnet.js');
19const PDFTronLicense = require('../../LicenseKey/NODEJS/LicenseKey');
20
21((exports) => {
22
23 exports.runDOCXCompareTest = () => {
24
25 const main = async() => {
26 // Relative paths to folders containing test files.
27 const inputPath = '../TestFiles/';
28 const outputPath = '../TestFiles/Output/';
29
30 // Provide your own original and revised versions of a DOCX document here.
31 const originalFilename = 'SYH_Letter.docx';
32 const revisedFilename = 'SYH_Letter_revision2.docx';
33 const outputFilename = 'SYH_Letter_changes.docx';
34
35 try {
36 const options = await PDFNet.DOCXCompare.createDOCXCompareOptions();
37
38 // Compare the two DOCX documents, writing the differences as tracked
39 // changes into the output DOCX document.
40 const result = await PDFNet.DOCXCompare.compare(
41 inputPath + originalFilename,
42 inputPath + revisedFilename,
43 outputPath + outputFilename,
44 options
45 );
46
47 // And we're done!
48 if (await result.differencesDetected()) {
49 console.log('Differences detected, saved to ' + outputFilename);
50 } else {
51 console.log('No difference detected');
52 }
53 } catch (err) {
54 console.log(err.stack);
55 }
56
57 console.log('Done.');
58 };
59 PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error){console.log('Error: ' + JSON.stringify(error));}).then(function(){return PDFNet.shutdown();});
60 };
61 exports.runDOCXCompareTest();
62})(exports);
63// eslint-disable-next-line spaced-comment
64//# sourceURL=DOCXCompareTest.js
65
66
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2026 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14# Relative path to the folder containing the test files.
15input_path = "../../TestFiles/"
16output_path = "../../TestFiles/Output/"
17
18# Provide your own original and revised versions of a DOCX document here.
19original_filename = "SYH_Letter.docx"
20revised_filename = "SYH_Letter_revision2.docx"
21output_filename = "SYH_Letter_changes.docx"
22
23#---------------------------------------------------------------------------------------
24# The following sample illustrates how to use the Office.DOCXCompare utility class to
25# compare two MS Word (DOCX) documents and produce a new DOCX document containing the
26# differences between them as tracked changes.
27#
28# This comparison is performed entirely within the PDFNet and has *no* external or
29# system dependencies -- Comparison results will be the same whether on Windows,
30# Linux or Android.
31#
32# Please contact us if you have any questions.
33#---------------------------------------------------------------------------------------
34
35def main():
36 # The first step in every application using PDFNet is to initialize the
37 # library. The library is usually initialized only once, but calling
38 # Initialize() multiple times is also fine.
39 PDFNet.Initialize(LicenseKey)
40 PDFNet.SetResourcesPath("../../../Resources")
41
42 try:
43 options = DOCXCompareOptions()
44
45 # Compare the two DOCX documents, writing the differences as tracked
46 # changes into the output DOCX document.
47 result = DOCXCompare.Compare(input_path + original_filename, input_path + revised_filename, output_path + output_filename, options)
48
49 # And we're done!
50 if result.DifferencesDetected():
51 print("Differences detected, saved to " + output_filename)
52 else:
53 print("No difference detected")
54 except Exception as e:
55 print("Unable to compare DOCX documents, error: " + str(e))
56
57 PDFNet.Terminate()
58 print("Done.")
59
60if __name__ == '__main__':
61 main()
62
63
1<?php
2//------------------------------------------------------------------------------
3// Copyright (c) 2001-2026 by Apryse Software Inc. All Rights Reserved.
4// Consult legal.txt regarding legal and license information.
5//------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10// Relative path to the folder containing the test files.
11$input_path = getcwd()."/../../TestFiles/";
12$output_path = $input_path."Output/";
13
14// Provide your own original and revised versions of a DOCX document here.
15$original_filename = "SYH_Letter.docx";
16$revised_filename = "SYH_Letter_revision2.docx";
17$output_filename = "SYH_Letter_changes.docx";
18
19//------------------------------------------------------------------------------
20// The following sample illustrates how to use the Office::DOCXCompare utility class
21// to compare two MS Word (DOCX) documents and produce a new DOCX document containing
22// the differences between them as tracked changes.
23//
24// This comparison is performed entirely within the PDFNet and has *no* external or
25// system dependencies -- Comparison results will be the same whether on Windows,
26// Linux or Android.
27//
28// Please contact us if you have any questions.
29//------------------------------------------------------------------------------
30
31function main()
32{
33 global $input_path, $output_path, $original_filename, $revised_filename, $output_filename;
34
35 // The first step in every application using PDFNet is to initialize the
36 // library. The library is usually initialized only once, but calling
37 // Initialize() multiple times is also fine.
38 global $LicenseKey;
39 PDFNet::Initialize($LicenseKey);
40 PDFNet::SetResourcesPath("../../../Resources");
41
42 try
43 {
44 $options = new DOCXCompareOptions();
45
46 // Compare the two DOCX documents, writing the differences as tracked
47 // changes into the output DOCX document.
48 $result = DOCXCompare::Compare($input_path.$original_filename, $input_path.$revised_filename, $output_path.$output_filename, $options);
49
50 // And we're done!
51 if($result->DifferencesDetected())
52 {
53 echo nl2br("Differences detected, saved to ".$output_filename."\n");
54 }
55 else
56 {
57 echo nl2br("No difference detected\n");
58 }
59 }
60 catch(Exception $e)
61 {
62 echo nl2br("Unable to compare DOCX documents, error: ".$e->getMessage()."\n");
63 }
64
65 PDFNet::Terminate();
66 echo(nl2br("Done.\n"));
67}
68
69main()
70
71?>
72
73
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2026 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6require '../../../PDFNetC/Lib/PDFNetRuby'
7include PDFNetRuby
8require '../../LicenseKey/RUBY/LicenseKey'
9
10$stdout.sync = true
11
12#---------------------------------------------------------------------------------------
13# The following sample illustrates how to use the Office.DOCXCompare utility class to
14# compare two MS Word (DOCX) documents and produce a new DOCX document containing the
15# 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#---------------------------------------------------------------------------------------
23
24# Relative path to the folder containing the test files.
25$inputPath = "../../TestFiles/"
26$outputPath = "../../TestFiles/Output/"
27
28# Provide your own original and revised versions of a DOCX document here.
29$original_filename = "SYH_Letter.docx"
30$revised_filename = "SYH_Letter_revision2.docx"
31$output_filename = "SYH_Letter_changes.docx"
32
33def main()
34 # The first step in every application using PDFNet is to initialize the
35 # library. The library is usually initialized only once, but calling
36 # Initialize() multiple times is also fine.
37 PDFNet.Initialize(PDFTronLicense.Key)
38 PDFNet.SetResourcesPath("../../../Resources")
39
40 begin
41 options = DOCXCompareOptions.new()
42
43 # Compare the two DOCX documents, writing the differences as tracked
44 # changes into the output DOCX document.
45 result = DOCXCompare.Compare($inputPath + $original_filename, $inputPath + $revised_filename, $outputPath + $output_filename, options)
46
47 # And we're done!
48 if result.DifferencesDetected()
49 puts "Differences detected, saved to " + $output_filename
50 else
51 puts "No difference detected"
52 end
53 rescue => error
54 puts "Unable to compare DOCX documents, error: " + error.message
55 end
56
57 PDFNet.Terminate
58 puts "Done."
59end
60
61main()
62
63
1'
2' Copyright (c) 2001-2026 by Apryse Software Inc. All Rights Reserved.
3'
4
5Imports System
6
7Imports pdftron
8Imports pdftron.Common
9Imports pdftron.Office
10
11Module DOCXCompareTestVB
12 Dim pdfNetLoader As PDFNetLoader
13 Sub New()
14 pdfNetLoader = pdftron.PDFNetLoader.Instance()
15 End Sub
16
17 ' The following sample illustrates how to use the pdftron.Office.DOCXCompare utility
18 ' class to compare two MS Word (DOCX) documents and produce a new DOCX document
19 ' containing the differences between them as tracked changes.
20 '
21 ' This comparison is performed entirely within the PDFNet and has *no* external or
22 ' system dependencies -- Comparison results will be the same whether on Windows,
23 ' Linux or Android.
24 '
25 ' Please contact us if you have any questions.
26 Sub Main()
27 PDFNet.Initialize(PDFTronLicense.Key)
28 PDFNet.SetResourcesPath("../../../../../Resources")
29
30 Dim input_path As String = "../../../../TestFiles/"
31 Dim output_path As String = "../../../../TestFiles/Output/"
32
33 ' Provide your own original and revised versions of a DOCX document here.
34 Dim original_filename As String = "SYH_Letter.docx"
35 Dim revised_filename As String = "SYH_Letter_revision2.docx"
36 Dim output_filename As String = "SYH_Letter_changes.docx"
37
38 Try
39 Dim options As New DOCXCompareOptions()
40
41 ' Compare the two DOCX documents, writing the differences as tracked
42 ' changes into the output DOCX document.
43 Dim result As DOCXCompareResult = DOCXCompare.Compare(input_path + original_filename, input_path + revised_filename, output_path + output_filename, options)
44
45 ' And we're done!
46 If result.DifferencesDetected() Then
47 Console.WriteLine("Differences detected, saved to " + output_filename)
48 Else
49 Console.WriteLine("No difference detected")
50 End If
51 Catch ex As PDFNetException
52 Console.WriteLine(ex.Message)
53 Catch ex As Exception
54 MsgBox(ex.Message)
55 End Try
56
57 Console.WriteLine("Done.")
58
59 PDFNet.Terminate()
60 End Sub
61
62End Module
63
64
65
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2026 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#import <OBJC/PDFNetOBJC.h>
7#import <Foundation/Foundation.h>
8
9//------------------------------------------------------------------------------
10// The following sample illustrates how to use the PTDOCXCompare utility class
11// to compare two MS Word (DOCX) documents and produce a new DOCX document
12// containing the differences between them as tracked changes.
13//
14// This comparison is performed entirely within the PDFNet and has *no*
15// external or system dependencies -- Comparison results will be
16// the same whether on Windows, Linux or Android.
17//
18// Please contact us if you have any questions.
19//------------------------------------------------------------------------------
20
21int main(int argc, char *argv[])
22{
23
24 @autoreleasepool {
25
26 [PTPDFNet Initialize: 0];
27
28 NSString *input_path = @"../../TestFiles/";
29 NSString *output_path = @"../../TestFiles/Output/";
30 // Provide your own original and revised versions of a DOCX document here.
31 NSString *original_filename = @"SYH_Letter.docx";
32 NSString *revised_filename = @"SYH_Letter_revision2.docx";
33 NSString *output_filename = @"SYH_Letter_changes.docx";
34
35 // Compare the two DOCX documents, writing the differences as tracked
36 // changes into the output DOCX document.
37 PTDOCXCompareOptions *options = [[PTDOCXCompareOptions alloc] init];
38 PTDOCXCompareResult *result = [PTDOCXCompare Compare: [NSString stringWithFormat:@"%@%@", input_path, original_filename]
39 revised_docx_path: [NSString stringWithFormat:@"%@%@", input_path, revised_filename]
40 output_path: [NSString stringWithFormat:@"%@%@", output_path, output_filename]
41 options: options];
42
43 if ([result DifferencesDetected]) {
44 NSLog(@"Differences detected, saved to %@\n", output_filename);
45 } else {
46 NSLog(@"No difference detected\n");
47 }
48 NSLog(@"Done.\n");
49
50 [PTPDFNet Terminate: 0];
51 return 0;
52 }
53}
54
55
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales