DOCX Compare - PHP 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<?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

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales