合併 Word 文檔

MS Word 是一個強大的工具,它允許您使用一組出色的功能自定義和格式化您的專業文檔。您可能會發現自己處於想要合併多個 Word 文檔的情況,例如將所有收據合併到一個文件中或合併不同的軟件需求文檔以便於管理。此外,合併文件簡化了共享它們的過程。在本文中,您將學習如何使用 C++ 合併多個 MS Word 文檔。

用於合併 Word 文檔的 C++ API

Aspose.Words for C++ 是一個本地 C++ 庫,允許您創建、更改和轉換 Microsoft Word 文檔。此外,它還支持將多個Word文檔合併為一個文件。您可以通過 NuGet 安裝 API 或直接從 下載 部分下載。

PM> Install-Package Aspose.Words.Cpp

使用 C++ 合併 Word 文檔

將 Word 文檔與 Aspose.Words for C++ API 合併是輕而易舉的事。您只需幾行代碼就可以加載和合併它們。下圖顯示了我們將在本文中組合的兩個示例 Word 文件。

要合併的Word文檔

以下是使用 C++ 合併 Word 文檔的步驟。

以下是合併 Word 文檔的示例代碼。

// 要合併的示例 Word 文檔
System::String sampleFile1 = u"SourceDirectory\\Sample 1.docx";
System::String sampleFile2 = u"SourceDirectory\\Sample 2.docx";

// 加載要合併的Word文檔
System::SharedPtr<Aspose::Words::Document> document1 = System::MakeObject<Aspose::Words::Document>(sampleFile1);
System::SharedPtr<Aspose::Words::Document> document2 = System::MakeObject<Aspose::Words::Document>(sampleFile2);

// 合併文檔保持源文件的格式
document1->AppendDocument(document2, Aspose::Words::ImportFormatMode::KeepSourceFormatting);

// 輸出文件路徑
System::String outputPath = u"OutputDirectory\\merged-doc-out.docx";

// 將合併後的文檔另存為 DOCX 文件
document1->Save(outputPath, Aspose::Words::SaveFormat::Docx);

下圖比較了使用 ImportFormatMode::KeepSourceFormatting 和 [ImportFormatMode](https://reference. aspose.com/words/cpp/namespace/aspose.words#aafaa52cbf0baa49c3225787c23a8c949::UseDestinationStyles 模式。

使用 ImportFormatOptions::KeepSourceFormatting 生成的合併 Word 文檔

使用 ImportFormatMode::KeepSourceFormatting 生成的輸出

使用 ImportFormatOptions::UseDestinationStyles 生成的合併 Word 文檔

使用 ImportFormatMode::UseDestinationStyles 生成的輸出

使用其他選項合併 Word 文檔

Aspose.Words for C++ API 提供了 ImportFormatOptions 類用於自定義合併 Word 文件。以下是 ImportFormatOptions 類提供的選項。

以下是使用附加選項合併多個 Word 文檔的步驟:

以下是使用附加選項合併 Word 文檔的示例代碼。

// 要合併的示例 Word 文檔
System::String sampleFile1 = u"SourceDirectory\\Sample 1.docx";
System::String sampleFile2 = u"SourceDirectory\\Sample 2.docx";

// 加載要合併的Word文檔
System::SharedPtr<Aspose::Words::Document> document1 = System::MakeObject<Aspose::Words::Document>(sampleFile1);
System::SharedPtr<Aspose::Words::Document> document2 = System::MakeObject<Aspose::Words::Document>(sampleFile2);

// 設置選項
auto options = MakeObject<Aspose::Words::ImportFormatOptions>();
options->set_IgnoreHeaderFooter(false);

// 合併文檔保持源文件的格式
document1->AppendDocument(document2, Aspose::Words::ImportFormatMode::KeepSourceFormatting, options);

// 輸出文件路徑
System::String outputPath = u"OutputDirectory\\merged-doc-out.docx";

// 將合併後的文檔另存為 DOCX 文件
document1->Save(outputPath, Aspose::Words::SaveFormat::Docx);

下圖比較了通過將 IgnoreHeaderFooter 選項設置為 true 和 false 生成的合併文檔。

通過將 IgnoreHeaderFooter 選項設置為 true 生成的合併 Word 文檔

通過將 IgnoreHeaderFooter 選項設置為 true 生成的輸出

通過將 IgnoreHeaderFooter 選項設置為 false 生成的合併 Word 文檔

通過將 IgnoreHeaderFooter 選項設置為 false 生成的輸出

獲得免費許可證

您可以通過申請 免費的臨時許可證 來試用沒有評估限制的 API。

結論

在本文中,您學習瞭如何使用 C++ 合併多個 Word 文檔。此外,您還學習瞭如何使用其他選項來自定義 Word 文件的合併。 Aspose.Words for C++ API 提供了一系列用於處理 Word 文件的功能。您可以使用 官方文檔 詳細探索 API。如果您有任何疑問,請隨時在論壇 上與我們聯繫。

也可以看看