DOCX Compare - Javascript 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
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

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales