拆分 Word 文檔 C++

MS Word 是一種用於存儲和共享信息的流行格式。在某些情況下,您可能希望將 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 文檔

以下是使用 C++ 拆分 Word 文件的步驟。

  • 使用 Document 類加載 Word 文檔。
  • 創建 DocumentPageSplitter 類的實例,並使用上一步中創建的 Document 對像對其進行初始化。
  • 循環瀏覽 Document 的頁面。
  • 使用 DocumentPageSplitter.GetDocumentOfPage(int32t PageIndex) 方法提取每個頁面。
  • 使用 Document->Save (System::String fileName) 方法保存文檔。

以下是按頁拆分 Word 文檔的示例代碼。

// 示例 Word 文檔
System::String sampleFile = u"SourceDirectory\\Sample 3.docx";

// 載入Word文檔
System::SharedPtr<Aspose::Words::Document> document = System::MakeObject<Aspose::Words::Document>(sampleFile);

// 創建並初始化文檔分頁器
DocumentPageSplitter splitter = DocumentPageSplitter(document);

// 輸出文件路徑
System::String outputPath = u"OutputDirectory\\";

// 將每一頁另存為單獨的文檔
for (int page = 1; page <= document->get_PageCount(); page++)
{
    auto pageDoc = splitter.GetDocumentOfPage(page);
    pageDoc->Save(outputPath + u"SplitDocumentPageByPageOut_" + System::Convert::ToString(page) + u".docx");
}

使用頁面範圍拆分 Word 文檔

您還可以通過提供頁面範圍來拆分 Word 文檔,而不是分隔每一頁。以下是使用頁面範圍拆分 Word 文檔的步驟。

  • 首先,使用 Document 類加載 Word 文檔。
  • 創建 DocumentPageSplitter 類的實例並使用上一步的 Document 對像對其進行初始化。
  • 使用 DocumentPageSplitter.GetDocumentOfPageRange(int32t pageIndex, int32t pageIndex) 方法檢索所需範圍內的頁面。
  • 最後,使用 Document->Save (System::String fileName) 方法保存 Word 文檔。

以下是使用頁面範圍拆分 Word 文檔的示例代碼。

// 示例 Word 文檔
System::String sampleFile = u"SourceDirectory\\Sample 3.docx";

// 載入Word文檔
System::SharedPtr<Aspose::Words::Document> document = System::MakeObject<Aspose::Words::Document>(sampleFile);

// 創建並初始化文檔分頁器
DocumentPageSplitter splitter = DocumentPageSplitter(document);

// 獲取頁面範圍
auto pageDoc = splitter.GetDocumentOfPageRange(2, 3);

// 輸出文件路徑
System::String outputPath = u"OutputDirectory\\";

// 另存為 DOCX 文件
pageDoc->Save(outputPath + u"SplitDocumentByPageRangeOut.docx");

使用 C++ 按部分拆分 Word 文檔

Word 文件可以包含一個或多個部分,這些部分可能具有不同的格式並且可能包含任意數量的頁面。要在 Word 文件中添加新節,請使用“佈局”>“分隔符”>“分節符”選項。以下是使用 Aspose.Words for C++ API 按部分拆分 Word 文件的步驟。

以下是按部分拆分 Word 文檔的示例代碼。

// 示例 Word 文檔
System::String sampleFile = u"SourceDirectory\\Sample 3.docx";

// 載入Word文檔
System::SharedPtr<Aspose::Words::Document> document = System::MakeObject<Aspose::Words::Document>(sampleFile);

// 輸出文件路徑
System::String outputPath = u"OutputDirectory\\";

for (int i = 0; i < document->get_Sections()->get_Count(); i++)
{
    // 將文檔拆分為更小的部分,在本例中按部分拆分
    auto section = document->get_Sections()->idx_get(i)->Clone();

    // 創建一個新文檔
    auto newDoc = System::MakeObject<Aspose::Words::Document>();
    newDoc->get_Sections()->Clear();

    //向新創建的文檔添加新部分
    auto newSection = System::StaticCast<Aspose::Words::Section>(newDoc->ImportNode(section, true));
    newDoc->get_Sections()->Add(newSection);

    // 將每個部分保存為單獨的文檔
    newDoc->Save(outputPath + u"SplitDocumentBySectionsOut_" + System::Convert::ToString(i) + u".docx");
}

獲得免費許可證

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

結論

在本文中,您學習瞭如何使用 C++ 將 Word 文檔拆分為多個文件。您已經學習瞭如何使用 Aspose.Words for C++ API 將 Word 文檔按頁面、頁面範圍和部分拆分。您可以使用 官方文檔 詳細探索 API。如果您有任何疑問,請隨時在論壇 上與我們聯繫。

也可以看看