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

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales