使用C++ SDK比較Word或PDF文件

強大的C++庫,用於比較PDF, Word, HTML, TXT, MD和其他格式的文件

使用我們的C++文件差異檢查器,比較兩個文檔,並在輸出中清楚地查看它們之間的差異。您不再需要手動比較文檔。另請注意,比較結果可以導出為DOCX, PDF, DOC等。

看程式碼片段

比較 C++ 中的兩個文件

我們的 C++ 解決方案旨在為您在使用複雜的比較算法時提供最佳結果。因此,您不再需要手動查找文檔中的差異 - 請隨意使用我們的 API 來比較兩個文件。

我們的比較引擎尋找字符或整個單詞級別的差異。如果只更改了一個字符,則整個單詞將突出顯示為已更改。所有發現的更改都可以在輸出中清楚地查看,並以方便的格式導出。

最流行的比較類型是比較 PDF 和 Word 文件。因此,我們將使用這些示例來學習我們的 C++ diff工具及其比較功能。

在 C++ 中比較 PDF

有時您需要確保您的PDF文檔沒有被修改,並將其與原始版本進行比較。或者您已經知道您的 PDF 確實發生了變化,並且您需要查看位置和方式。然後您可以比較兩個版本的 PDF 文件並查看差異。為了避免手動執行此操作,只需使用我們的 C++ diff 檢查器 API。

通過我們的 C++ 解決方案,您甚至會看到在手動比較中很容易錯過的微小變化。

使用 C++ 比較兩個 Word 文檔

Word 文檔相當容易更改,因此比較 Word 文件以確保部分或整個文檔沒有更改非常重要。

要比較 Word 文件,請使用我們的 C++ 解決方案,就像處理 PDF 一樣。您可以在下面的示例中執行此操作。

比較兩個文檔

要了解我們的 C++ 文檔比較的工作原理,請加載要比較的兩個文件並選擇導出文件格式。

比較完成後,將按照您選擇的格式自動下載包含差異的文檔。

重要的是,被比較的文檔在比較之前不能有任何修訂,因此我們需要在比較開始之前接受所有修訂。別擔心,我們已經在我們的示例中解決了這個問題:

上傳比較文件
運行代碼
上傳第二個文件進行比較
從列表中選擇目標格式
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";

// 將文檔上傳到雲存儲。
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);

// 比較雲中的文檔。
auto compareDataOptions = std::make_shared<aspose::words::cloud::models::CompareData>();
compareDataOptions->setAuthor(std::make_shared<std::wstring>(L"author"));
compareDataOptions->setComparingWithDocument(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);

// 從雲存儲下載結果文檔。
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();
}
運行代碼
  
將代碼C++複製到剪貼板

如何比較文件的兩個版本

  1. Aspose.Words Cloud for C++
  2. 將庫引用(導入庫)添加到您的C++項目
  3. 加載兩個文檔進行比較
  4. 調用compareDocument()方法比較兩個文檔
  5. 從雲存儲下載結果文檔

C++庫使用比較

從 GitHub 克隆 Aspose.Words Cloud SDK for C++ 源代碼。您可以在 "How to use the SDK" 部分找到有關構建和配置 SDK 的詳細信息。

要快速獲取必要的安全憑證並訪問我們的 REST API,請按照文檔中的這些 Instructions 進行操作。

系統要求

請參閱 Repository Documentation 以查看詳細信息。

最流行的比較操作格式

5%

訂閱 Aspose 產品更新

獲取直接發送到您的郵箱的每月通訊和優惠。

© Aspose Pty Ltd 2001-2024. 版權所有。