Compare two documents Comparing documents is often used to track changes made to important documents. Additionally, it is used in editing to compare the modified document version with the original.
Aspose.Words REST API is a powerful document manipulation tool that allows developers to compare two documents and retrieve the differences between them.
As a result of the comparison, a list of differences between the two Word documents is returned. Each difference will contain information about the type of difference (e.g. deleted text, added text, formatting change), the location of the difference in the document, and the text that was added or deleted.
Compare two documents REST API
Server
Method
Endpoint
https://api.aspose.cloud/v4.0
PUT
/words/online/put/compareDocument
You can use the following parameters in a REST request:
Parameter Name
Data Type
Required/Optional
Description
loadEncoding
string
Optional
Encoding that will be used to load an HTML (or TXT) document if the encoding is not specified in HTML.
password
string
Optional
Password of protected Word document. Use the parameter to pass a password via SDK. SDK encrypts it automatically. We don’t recommend to use the parameter to pass a plain password for direct call of API.
encryptedPassword
string
Optional
Password of protected Word document. Use the parameter to pass an encrypted password for direct calls of API. See SDK code for encyption details.
destFileName
string
Optional
Result path of the document after the operation. If this parameter is omitted then result of the operation will be saved as the source document.
encryptedPassword2
string
Optional
encrypted password for the second document.
Use $multipart/form-data
request to combine one or more properties into a single body:
Property Name
Data Type
Required/Optional
Description
document
string(binary)
Required
The document.
compareData
CompareData
Required
Compare data.
comparingDocument
string(binary)
Optional
The comparing document.
Note : to access this REST API, you need to register and get personal credentials. Use the ‘
Quick Start ’ guide to go through the procedure in a couple of minutes.
Compare two documents usage examples
Let’s look at practical examples of using the web service. You can do this both with cURL and Postman utilities, and from your code in various programming languages: Python, Java, JavaScript, C#, PHP, C++, Go, Ruby, Swift, Dart.
How to compare twodocuments with cURL or Postman
One of the easiest and fastest ways to call a REST API is to use cURL or Postman:
cURL Request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl -v "https://api.aspose.cloud/v4.0/words/online/put/compareDocument?destFileName=CompareDocumentOut.doc" \
-X PUT \
-H "Content-Type: multipart/form-data" \
-H "Authorization: Bearer ####################" \
-F Document="@compareTestDoc1.doc" \
-F CompareData="{\"Author\":\"author\",\"ComparingWithDocument\":\"TestCompareDocument2.doc\",\"DateTime\":\"2015-10-26T00:00:00Z\"}" \
-F ComparingDocument="@compareTestDoc2.doc"
To get a JWT token use these instructions
Postman Request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{
"info": {
"name": "CompareDocumentOnline",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"auth": {
"type": "oauth2",
"oauth2": [
{
"key": "clientSecret",
"value": "{{CLIENT_SECRET}}",
"type": "string"
},
{
"key": "clientId",
"value": "{{CLIENT_ID}}",
"type": "string"
},
{
"key": "addTokenTo",
"value": "header",
"type": "string"
},
{
"key": "client_authentication",
"value": "body",
"type": "string"
},
{
"key": "accessTokenUrl",
"value": "https://api.aspose.cloud/connect/token",
"type": "string"
},
{
"key": "grant_type",
"value": "client_credentials",
"type": "string"
},
{
"key": "tokenName",
"value": "Access Token",
"type": "string"
}
]
},
"item": [
{
"name": "compareRequest",
"protocolProfileBehavior": {
"disableBodyPruning": true
},
"request": {
"method": "PUT",
"header": [
],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "Document",
"type": "file",
"src": "compareTestDoc1.doc"
},
{
"key": "CompareData",
"type": "text",
"value": "{\"Author\":\"author\",\"ComparingWithDocument\":\"TestCompareDocument2.doc\",\"DateTime\":\"2015-10-26T00:00:00Z\"}"
},
{
"key": "ComparingDocument",
"type": "file",
"src": "compareTestDoc2.doc"
}
]
},
"url": {
"raw": "https://api.aspose.cloud/v4.0/words/online/put/compareDocument?destFileName=CompareDocumentOut.doc",
"protocol": "https",
"host": [
"api",
"aspose",
"cloud"
],
"path": [
"v4.0",
"words",
"online",
"put",
"compareDocument"
],
"query": [
{
"key": "DestFileName",
"value": "CompareDocumentOut.doc",
"description": ""
}
]
}
},
"response": []
}
],
"variable": [
]
}
To get a JWT token use these instructions
How to compare two documents in Python, Java, C#, C++, JavaScript and other programming languages
Using SDK is the quickest way to speed up the development. Please take a look at the provided code examples to quickly call this web service from your favourite programming language:
Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os
import asposewordscloud
import asposewordscloud.models.requests
from asposewordscloud.rest import ApiException
from shutil import copyfile
words_api = WordsApi(client_id = '####-####-####-####-####', client_secret = '##################')
request_document = open('compareTestDoc1.doc', 'rb')
request_compare_data = asposewordscloud.CompareData(author='author', comparing_with_document='TestCompareDocument2.doc', date_time=dateutil.parser.isoparse('2015-10-26T00:00:00.0000000Z'))
request_comparing_document = open('compareTestDoc2.doc', 'rb')
compare_request = asposewordscloud.models.requests.CompareDocumentOnlineRequest(document=request_document, compare_data=request_compare_data, comparing_document=request_comparing_document, dest_file_name='CompareDocumentOut.doc')
words_api.compare_document_online(compare_request)
Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.aspose.words.cloud.*;
import com.aspose.words.cloud.api.*;
import com.aspose.words.cloud.model.*;
import com.aspose.words.cloud.model.requests.*;
import com.aspose.words.cloud.model.responses.*;
import java.nio.file.Files;
import java.nio.file.Paths;
ApiClient apiClient = new ApiClient(/*clientId*/ "####-####-####-####-####", /*clientSecret*/ "##################", null);
WordsApi wordsApi = new WordsApi(apiClient);
byte[] requestDocument = Files.readAllBytes(Paths.get("compareTestDoc1.doc").toAbsolutePath());
CompareData requestCompareData = new CompareData();
requestCompareData.setAuthor("author");
requestCompareData.setComparingWithDocument("TestCompareDocument2.doc");
requestCompareData.setDateTime(OffsetDateTime.of(2015, 10, 26, 0, 0, 0, 0, ZoneOffset.UTC));
byte[] requestComparingDocument = Files.readAllBytes(Paths.get("compareTestDoc2.doc").toAbsolutePath());
CompareDocumentOnlineRequest compareRequest = new CompareDocumentOnlineRequest(requestDocument, requestCompareData, requestComparingDocument, null, null, null, "CompareDocumentOut.doc", null);
wordsApi.compareDocumentOnline(compareRequest);
Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as fs from "fs";
const clientId = "####-####-####-####-####";
const secret = "##################";
const wordsApi = new WordsApi(clientId, secret);
const requestDocument = fs.createReadStream("compareTestDoc1.doc");
const requestCompareData = new model.CompareData({
author: "author",
comparingWithDocument: "TestCompareDocument2.doc",
dateTime: new Date('2015-10-26T00:00:00Z')
})
const requestComparingDocument = fs.createReadStream("compareTestDoc2.doc");
const compareRequest = new model.CompareDocumentOnlineRequest({
document: requestDocument,
compareData: requestCompareData,
comparingDocument: requestComparingDocument,
destFileName: "CompareDocumentOut.doc"
});
wordsApi.compareDocumentOnline(compareRequest)
.then((compareRequestResult) => {
// tslint:disable-next-line:no-console
console.log("Result of compareRequest: ", compareRequestResult);
});
C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Aspose.Words.Cloud.Sdk;
using Aspose.Words.Cloud.Sdk.Model;
using Aspose.Words.Cloud.Sdk.Model.Requests;
var config = new Configuration { ClientId = "####-####-####-####-####", ClientSecret = "##################" };
var wordsApi = new WordsApi(config);
using var requestDocument = File.OpenRead("compareTestDoc1.doc");
var requestCompareData = new CompareData()
{
Author = "author",
ComparingWithDocument = "TestCompareDocument2.doc",
DateTime = new System.DateTime(2015, 10, 26, 0, 0, 0)
};
using var requestComparingDocument = File.OpenRead("compareTestDoc2.doc");
var compareRequest = new CompareDocumentOnlineRequest(requestDocument, requestCompareData, comparingDocument: requestComparingDocument, destFileName: "CompareDocumentOut.doc");
await wordsApi.CompareDocumentOnline(compareRequest);
PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php
use Aspose\Words\WordsApi;
use Aspose\Words\Model\Requests\{CompareDocumentOnlineRequest};
use Aspose\Words\Model\{CompareData};
$clientId = '####-####-####-####-####';
$secret = '##################';
$wordsApi = new WordsApi($clientId, $secret);
$requestDocument = "compareTestDoc1.doc";
$requestCompareData = new CompareData(array(
"author" => "author",
"comparing_with_document" => "TestCompareDocument2.doc",
"date_time" => new \DateTime("2015-10-26T00:00:00.0000000Z"),));
$requestComparingDocument = "compareTestDoc2.doc";
$compareRequest = new CompareDocumentOnlineRequest(
$requestDocument, $requestCompareData, $requestComparingDocument, NULL, NULL, NULL, "CompareDocumentOut.doc", NULL);
$wordsApi->compareDocumentOnline($compareRequest);
C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "aspose_words_cloud.h"
using namespace aspose::words::cloud;
auto config = std::make_shared<ApiConfiguration>(/*clientId*/ L"####-####-####-####-####", /*clientSecret*/ L"##################");
auto wordsApi = std::make_shared<WordsApi>(config);
auto requestDocument = std::shared_ptr<std::istream>(new std::ifstream(std::filesystem::path(L"compareTestDoc1.doc"), std::istream::binary));
auto requestCompareData = std::make_shared<aspose::words::cloud::models::CompareData>();
requestCompareData->setAuthor(std::make_shared<std::wstring>(L"author"));
requestCompareData->setComparingWithDocument(std::make_shared<std::wstring>(L"TestCompareDocument2.doc"));
requestCompareData->setDateTime(std::make_shared<std::wstring>(L"2015-10-26T00:00:00.0000000Z"));
auto requestComparingDocument = std::shared_ptr<std::istream>(new std::ifstream(std::filesystem::path(L"compareTestDoc2.doc"), std::istream::binary));
std::shared_ptr<requests::CompareDocumentOnlineRequest> compareRequest(
new requests::CompareDocumentOnlineRequest(
requestDocument, requestCompareData, requestComparingDocument, std::make_shared<std::wstring>(L"CompareDocumentOut.doc")));
wordsApi->compareDocumentOnline(compareRequest);
Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{
"ClientId": "####-####-####-####-####",
"ClientSecret": "##################",
"BaseUrl": "https://api.aspose.cloud"
}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import (
"os"
"github.com/aspose-words-cloud/aspose-words-cloud-go/dev/api/models")
config, _ := models.NewConfiguration("config.json")
wordsApi, ctx, _ := api.CreateWordsApi(config)
requestDocument, _ := os.Open("compareTestDoc1.doc")
requestCompareData := models.CompareData{
Author: ToStringPointer("author"),
ComparingWithDocument: ToStringPointer("TestCompareDocument2.doc"),
DateTime: ToTimePointer(CreateTime(2015, 10, 26, 0, 0, 0)),
}
requestComparingDocument, _ := os.Open("compareTestDoc2.doc")
compareRequestOptions := map[string]interface{}{"comparingDocument": requestComparingDocument,
"destFileName": "CompareDocumentOut.doc",}
compareRequest := &models.CompareDocumentOnlineRequest{
Document: requestDocument,
CompareData: &requestCompareData,
Optionals: compareRequestOptions,
}
_, _, _ = wordsApi.CompareDocumentOnline(ctx, compareRequest)
Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'aspose_words_cloud'
AsposeWordsCloud.configure do |config|
config.client_data['ClientId'] = '####-####-####-####-####'
config.client_data['ClientSecret'] = '##################'
end
@words_api = WordsAPI.new
request_document = File.open('compareTestDoc1.doc')
request_compare_data = CompareData.new({:Author => 'author', :ComparingWithDocument => 'TestCompareDocument2.doc', :DateTime => Date.iso8601('2015-10-26T00:00:00.0000000Z')})
request_comparing_document = File.open('compareTestDoc2.doc')
compare_request = CompareDocumentOnlineRequest.new(document: request_document, compare_data: request_compare_data, comparing_document: request_comparing_document, dest_file_name: 'CompareDocumentOut.doc')
@words_api.compare_document_online(compare_request)
Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AsposeWordsCloud
let config = Configuration(clientId: "####-####-####-####-####", clientSecret: "##################");
let api = try WordsAPI(configuration: config);
let requestDocument = InputStream(url: URL(string: "compareTestDoc1.doc"))!;
let requestCompareData = CompareData()
.setAuthor(author: "author")
.setComparingWithDocument(comparingWithDocument: "TestCompareDocument2.doc")
.setDateTime(dateTime: ObjectSerializer.customIso8601.date(from: "2015-10-26T00:00:00Z")!);
let requestComparingDocument = InputStream(url: URL(string: "compareTestDoc2.doc"))!;
let compareRequest = CompareDocumentOnlineRequest(document: requestDocument, compareData: requestCompareData, comparingDocument: requestComparingDocument, destFileName: "CompareDocumentOut.doc");
_ = try api.compareDocumentOnline(request: compareRequest);
Dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io';
import 'package:aspose_words_cloud/aspose_words_cloud.dart';
final clientId = "####-####-####-####-####";
final clientSecret = "##################";
final config = Configuration(clientId, clientSecret);
final wordsApi = WordsApi(config);
final requestDocument = (await File('compareTestDoc1.doc').readAsBytes()).buffer.asByteData();
final requestCompareData = CompareData();
requestCompareData.author = 'author';
requestCompareData.comparingWithDocument = 'TestCompareDocument2.doc';
requestCompareData.dateTime = DateTime(2015, 10, 26, 0, 0, 0);
final requestComparingDocument = (await File('compareTestDoc2.doc').readAsBytes()).buffer.asByteData();
final compareRequest = CompareDocumentOnlineRequest(requestDocument, requestCompareData, comparingDocument: requestComparingDocument, destFileName: 'CompareDocumentOut.doc');
await wordsApi.compareDocumentOnline(compareRequest);
See Also
GitHub repository — explore Aspose.Words Cloud SDK Family. These software libraries take care of all low-level document-processing details.