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

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales