Compare Word or PDF files using C++ SDK

Powerful C++ library to compare files in PDF, Word, HTML, TXT, MD and other formats

Use our C++ file difference checker, compare two documents and see the differences between them clearly shown in the output. You no longer need to compare your documents manually. Also note that the comparison result can be exported to a DOCX, PDF, DOC and others.

View code snippet

Comparing two files in C++

Our C++ solution is designed to give you the best possible results when using a complex comparison algorithm. So, you no longer need to manually look up differences in documents – feel free to use our API to diff two files.

Our comparison engine looks for differences at the level of characters or whole words. And if only one character was changed, the whole word will be highlighted as changed. All found changes can be clearly viewed in the output and exported in a convenient format.

The most popular types of comparison are comparing PDF and Word files. Therefore, we will learn our C++ diff tool and its comparison function using these examples.

Compare PDF in C++

Sometimes you need to make sure that your PDF document has not been modified and compare it with the original version. Or you already know that your PDF has really changed, and you need to look where and how. Then you can compare two versions of PDF files and see the differences. And to avoid doing it manually, just use our C++ diff checker API.

With our C++ solution, you will see even small changes that you could easily miss in a manual comparison.

Compare two Word documents using C++

Word documents are fairly easy to change, so it is important to compare Word files to make sure parts or the entire document have not changed.

To compare Word files, use our C++ solution, just like with PDF. You can do this in the example below.

Сompare two documents

To see how our C++ document compare works, load two files you want to compare and choose an export file format.

After the comparison is completed, the document with the diff will be automatically downloaded in the format you selected.

It is important that the documents being compared must not have any revisions before the comparison, so we need to accept all revisions before the compare starts. Do not worry, we have taken care of that in our example:

Upload the compared file
Run code
Upload the second file to compare
Select the target format from the list
using namespace aspose::words::cloud;

auto config =
    std::make_shared<ApiConfiguration>(L"####-####-####-####-####", L"##################");
auto wordsApi = std::make_shared<WordsApi>(config);

std::wstring fileName1 = L"Input1.docx";
std::wstring fileName2 = L"Input2.docx";
std::wstring fileResult = L"Output.docx";

//  Upload documents to cloud storage.
auto firstDocumentContent =
    std::shared_ptr<std::istream>(
	new std::ifstream(std::filesystem::path(fileName1), std::istream::binary));
std::shared_ptr<requests::UploadFileRequest> uploadFirstFileRequest(
    new requests::UploadFileRequest(
        firstDocumentContent, std::make_shared<std::wstring>(fileName1)
    )
);
wordsApi->uploadFile(uploadFirstFileRequest);

auto secondDocumentContent =
    std::shared_ptr<std::istream>(
	new std::ifstream(std::filesystem::path(fileName2), std::istream::binary));
std::shared_ptr<requests::UploadFileRequest> uploadSecondFileRequest(
    new requests::UploadFileRequest(
        secondDocumentContent, std::make_shared<std::wstring>(fileName2)
    )
);
wordsApi->uploadFile(uploadSecondFileRequest);

//  Compare documents in cloud.
auto compareDataOptions = std::make_shared<aspose::words::cloud::models::CompareData>();
compareDataOptions->setAuthor(std::make_shared<std::wstring>(L"author"));
compareDataOptions->setFileReference(
   std::make_shared<aspose::words::cloud::models::FileReference>(
      std::make_shared<std::wstring>(fileName2));
std::shared_ptr<requests::CompareDocumentRequest> compareDocumentRequest(
    new requests::CompareDocumentRequest(
        std::make_shared<std::wstring>(fileName1),
		compareDataOptions, std::make_shared<std::wstring>(fileResult)
    )
);
wordsApi->compareDocument(compareDocumentRequest);

//  Download result document from cloud storage.
std::shared_ptr<requests::DownloadFileRequest> downloadFileRequest(
    new requests::DownloadFileRequest(
        std::make_shared<std::wstring>(fileResult)
    )
);
auto downloadFileResult = wordsApi->downloadFile(downloadFileRequest);
{
    char buffer[1024];
    std::ofstream fileWriter(fileResult, std::ofstream::binary);
    const auto& fileData = *downloadFileResult->begin();
    while(fileData.second->read(buffer, sizeof(buffer))) {
        fileWriter.write(buffer, sizeof(buffer));
    }
    fileWriter.write(buffer, fileData.second->gcount());
    fileWriter.close();
}
Run code
  
Copy C++ code to the clipboard

How to Compare two versions of a file

  1. Install Aspose.Words Cloud for C++
  2. Add a library reference (import the library) to your C++ project
  3. Load two documents to compare
  4. Call the compareDocument() method to compare two docs
  5. Download the result document from the cloud storage

C++ library to use compare

Clone Aspose.Words Cloud SDK for C++ source code from GitHub. You can find detailed information on building and configuring the SDK in the "How to use the SDK" section.

To quickly get the necessary security credentials and access our REST API, please follow these Instructions in the documentation.

System Requirements

Refer to the Repository Documentation to see the details.

Most popular formats for compare operations

5%

Subscribe to Aspose Product Updates

Get monthly newsletters and offers directly delivered to your mailbox.

© Aspose Pty Ltd 2001-2024. All Rights Reserved.