Compare Documents - Document Comparison API
PDF4me Compare Documents enables you to compare two Word documents and track changes with comprehensive comparison options. This API service processes two Word files and generates a comparison document with detailed change tracking, customizable comparison settings, revision management, and professional comparison reports. The API receives both document contents through REST API calls, utilizing Base64 encoding for secure transmission. With full control over formatting, case sensitivity, comments, tables, fields, footnotes, textboxes, and headers/footers, this solution is ideal for document review, version control, and collaborative editing workflows.
Authenticating Your API Request
To access the PDF4me REST API, every request must include proper authentication credentials. Authentication ensures secure communication and validates your identity as an authorized user of the REST API.
Key Features
- Document Comparison: Compare two Word documents and identify all differences
- Track Changes: Generate documents with tracked changes showing modifications
- Customizable Options: Control what types of changes to track or ignore
- Revision Management: Clean up existing revisions before comparison
- Author Attribution: Assign changes to specific authors for accountability
- Professional Output: Generate clean comparison documents with revision tracking
- Multiple Format Support: Compare .docx and .doc files
- Advanced Filtering: Ignore formatting, case changes, comments, tables, and more
REST API Endpoint
The PDF4me REST API uses standard HTTP methods to interact with resources. All document comparison operations are performed through a single endpoint:
- Method: POST
- Endpoint:
office/ApiV2Word/CompareDocuments
REST API Parameters
Complete list of parameters for the Compare Documents REST API. Parameters are organized by category for better understanding and implementation.
Important: Parameters marked with an asterisk (*) are required and must be provided for the API to function correctly.
Required Parameters
| Parameter | Type | Description | Example |
|---|---|---|---|
| firstDocument* | Object | First (base) document reference. Must contain Name (string) — document name with extension (.docx, .doc). This document serves as the base for comparison | { "Name": "original.docx" } |
| firstDocContent* | Base64 | Base64-encoded content of the first document. Must be valid Word document (.docx, .doc formats) | UEsDBBQABgAIAAAA... |
| secondDocument* | Object | Second (comparison) document reference. Must contain Name (string) — document name with extension (.docx, .doc). This document is compared against the first | { "Name": "modified.docx" } |
| secondDocContent* | Base64 | Base64-encoded content of the second document. Must be valid Word document (.docx, .doc formats) | UEsDBBQABgAIAAAA... |
Optional Parameters (Comparison Options)
| Parameter | Type | Description | Example |
|---|---|---|---|
| comparisonOptions | Object | Comparison settings. Properties: ignoreFormatting (boolean), ignoreCaseChanges (boolean), ignoreComments (boolean), ignoreTables (boolean), ignoreFields (boolean), ignoreFootnotes (boolean), ignoreTextboxes (boolean), ignoreHeadersAndFooters (boolean), author (string). See Comparison Options below | { "ignoreFormatting": false, "author": "John Doe" } |
Comparison Options
The API provides fine-grained control over what changes to track:
Content Options:
- ignoreFormatting: Focus on text content, ignore styling changes
- ignoreCaseChanges: Case-insensitive comparison
- ignoreComments: Exclude comment changes from comparison
- ignoreTables: Exclude table structure and content changes
- ignoreFields: Exclude field updates (auto-updating content)
Document Element Options:
- ignoreFootnotes: Exclude footnote changes
- ignoreTextboxes: Exclude textbox changes
- ignoreHeadersAndFooters: Exclude header/footer changes
Attribution:
- author: Set the author name for tracked changes in the comparison document
Output
The PDF4me Compare Documents REST API returns different responses based on the processing mode. The API returns the Word document as a Base64-encoded string in JSON format, not as binary data.
- Success Response
- Asynchronous Processing
- Error Responses
- Response Format Details
Synchronous Processing (Default)
The API processes the request and returns:
Status Code: 200 OK
Content-Type: application/json
Response Body:
{
"document": "UEsDBBQABgAIAAAAIQDfpNJsWgEAACAFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAAC...",
"fileName": "comparison-result.docx",
"success": true,
"errorMessage": null
}
Response Fields:
- document (string): The Word document with tracked changes showing all differences, encoded as Base64 string
- fileName (string): The output Word file name with comparison results
- success (boolean): Indicates whether the request succeeded
- errorMessage (string or null): Error details when success is false
How to Use:
- Extract the
documentfield from the JSON response (Base64) - Decode the Base64 string to get the binary Word document data
- Open the document in Word to review tracked changes
- Accept or reject changes as needed
Example (JavaScript):
const response = await fetch(url, options);
const data = await response.json();
const wordBytes = atob(data.document); // Decode Base64
// Save or process wordBytes - document contains tracked changes
Asynchronous Processing
Asynchronous behavior (202 Accepted with polling) is controlled by server configuration, not by a request body parameter. When enabled, the API may return a 202 status with a polling URL in the Location header. Poll the URL with GET requests until you receive 200 OK with the same response shape (document, fileName, success, errorMessage).
Error Responses
The API returns standard HTTP error codes with error details:
- Invalid request parameters
- Missing required fields (
firstDocumentwithName,firstDocContent,secondDocumentwithName,secondDocContent) - First document is empty or null
- Second document is empty or null
- Invalid Base64 encoding in document content
- Invalid or corrupted Word documents
- Invalid or missing API key
- API key not properly Base64 encoded in Authorization header
- Missing
Authorization: Basicheader
- Server-side processing error
- Word document processing failure
- Document comparison failure
Error Response Format:
{
"error": "Error message describing what went wrong"
}
Response Format Details
Important: The API always returns JSON, never binary Word data directly.
Response Structure:
{
"document": "string", // Base64-encoded Word document with tracked changes
"fileName": "string", // Output Word filename with comparison results
"success": true,
"errorMessage": "string or null"
}
Content-Type Header:
- Success:
application/json - The Word document is embedded as a Base64 string within the JSON response
Why Base64?
- JSON-safe encoding for binary data
- Easy to transmit over HTTP
- Compatible with all programming languages
- Can be directly embedded in JSON without escaping issues
Decoding Base64 to Word Document:
JavaScript/Node.js:
const base64 = response.document;
const binary = atob(base64); // Browser
// OR
const binary = Buffer.from(base64, 'base64'); // Node.js
Python:
import base64
word_bytes = base64.b64decode(response['document'])
with open('comparison.docx', 'wb') as f:
f.write(word_bytes)
C#:
byte[] wordBytes = Convert.FromBase64String(response.document);
File.WriteAllBytes("comparison.docx", wordBytes);
Request Example
Header
Content-Type: application/json
Authorization: Basic YOUR_BASE64_ENCODED_API_KEY
Note:
- Get your API key from the PDF4me Dashboard
- The API key must be Base64 encoded and prefixed with "Basic " in the Authorization header
- Example: If your API key is
abc123, encode it to Base64 and useAuthorization: Basic YWJjMTIz
Payload
Basic Example (Required Fields Only):
{
"firstDocument": { "Name": "original.docx" },
"firstDocContent": "UEsDBBQABgAIAAAAIQDfpNJsWgEAACAFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAAC...",
"secondDocument": { "Name": "modified.docx" },
"secondDocContent": "UEsDBBQABgAIAAAAIQDfpNJsWgEAACAFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAAC..."
}
Advanced Example (With Comparison Options):
{
"firstDocument": { "Name": "original.docx" },
"firstDocContent": "UEsDBBQABgAIAAAAIQDfpNJsWgEAACAFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAAC...",
"secondDocument": { "Name": "modified.docx" },
"secondDocContent": "UEsDBBQABgAIAAAAIQDfpNJsWgEAACAFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAAC...",
"comparisonOptions": {
"ignoreFormatting": false,
"ignoreCaseChanges": false,
"ignoreComments": true,
"ignoreTables": false,
"ignoreFields": true,
"ignoreFootnotes": false,
"ignoreTextboxes": false,
"ignoreHeadersAndFooters": false,
"author": "Legal Review System"
}
}
Content-Only Comparison Example:
{
"firstDocument": { "Name": "original.docx" },
"firstDocContent": "UEsDBBQABgAIAAAAIQDfpNJsWgEAACAFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAAC...",
"secondDocument": { "Name": "modified.docx" },
"secondDocContent": "UEsDBBQABgAIAAAAIQDfpNJsWgEAACAFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAAC...",
"comparisonOptions": {
"ignoreFormatting": true,
"ignoreCaseChanges": true,
"ignoreComments": true,
"ignoreFields": true,
"author": "Content Review"
}
}
Code Samples
The PDF4me Compare Documents REST API provides code samples in multiple programming languages. Choose the language that best fits your development environment:
- C#
- Java
- JavaScript
- Python
- Salesforce
- n8n
- Google Script
- AWS Lambda
Google Script Sample
Google Apps Script implementation for Google Workspace integration:
Industry Use Cases & Applications
- Legal & Professional Services
- Business & Enterprise
- Education & Research
- Finance & Banking
Legal & Professional Services Use Cases
- Contract Review: Compare contract versions to identify changes and modifications
- Legal Document Updates: Track changes in legal briefs, agreements, and court documents
- Compliance Monitoring: Compare policy documents to ensure regulatory compliance
- Client Document Review: Track changes in client-facing legal documents
Business & Enterprise Use Cases
- Proposal Management: Compare proposal versions to track client requirements
- Marketing Material Updates: Track changes in marketing collateral and campaigns
- Pricing Document Changes: Compare pricing sheets and rate cards
- Client Presentation Updates: Track modifications in client presentation materials
Education & Research Use Cases
- Medical Protocol Updates: Compare medical procedure and protocol documents
- Patient Care Guidelines: Track changes in patient care documentation
- Research Documentation: Compare research protocols and study documents
- Compliance Documentation: Track changes in healthcare compliance materials
Finance & Banking Use Cases
- Financial Report Updates: Compare quarterly and annual financial reports
- Budget Document Changes: Track modifications in budget proposals and reports
- Audit Documentation: Compare audit reports across different periods
- Regulatory Filings: Track changes in regulatory submission documents