DOCX Compare - C++ 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#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

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales