Word处理控件Aspose.Words功能演示:使用 C++ 拆分 MS Word 文档

Aspose.Words 是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

Aspose.words 最新下载(qun:761297826)icon-default.png?t=N176https://www.evget.com/product/4116/download

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(int32_t PageIndex) 方法提取每个页面。
  • 使用Document->Save (System::String fileName)方法保存文档。

以下是按页拆分 Word 文档的示例代码。

// Sample Word document
System::String sampleFile = u"SourceDirectory\\Sample 3.docx";

// Load the Word document
System::SharedPtr<Aspose::Words::Document> document = System::MakeObject<Aspose::Words::Document>(sampleFile);

// Create and initialize document page splitter
DocumentPageSplitter splitter = DocumentPageSplitter(document);

// Output file path
System::String outputPath = u"OutputDirectory\\";

// Save each page as a separate document
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(int32_t pageIndex, int32_t pageIndex) 方法检索所需范围内的页面。
  • 最后,使用Document->Save (System::String fileName)方法保存 Word 文档。

以下是使用页面范围拆分 Word 文档的示例代码。

// Sample Word document
System::String sampleFile = u"SourceDirectory\\Sample 3.docx";

// Load the Word document
System::SharedPtr<Aspose::Words::Document> document = System::MakeObject<Aspose::Words::Document>(sampleFile);

// Create and initialize document page splitter
DocumentPageSplitter splitter = DocumentPageSplitter(document);

// Get the page range
auto pageDoc = splitter.GetDocumentOfPageRange(2, 3);

// Output file path
System::String outputPath = u"OutputDirectory\\";

// Save as DOCX file
pageDoc->Save(outputPath + u"SplitDocumentByPageRangeOut.docx");

四、使用 C++ 按部分拆分 Word 文档

Word 文件可以包含一个或多个部分,这些部分可能具有不同的格式,并且可能包含任意数量的页面。要在 Word 文件中添加新部分,请使用局>中断>分节选项。以下是使用 Aspose.Words for C++ API 按部分拆分 Word 文件的步骤。

  • 使用Document类加载 Word 文件 。
  • 使用Document->get_Sections()方法检索文档的各个部分并对其进行迭代。
  • 使用Document->get_Sections()->idx_get(int32_t index)->Clone()方法将该部分克隆到Section对象中。
  • 使用Document类创建一个新的 Word 文档。
  • 使用Document->get_Sections()->Add (System::SharedPtr Aspose::Words::Node node) 方法将克隆的部分添加到新创建的 Word 文档中。
  • 最后,使用Document->Save (System::String fileName)方法保存 Word 文件。

以下是按部分拆分 Word 文档的示例代码。

// Sample Word document
System::String sampleFile = u"SourceDirectory\\Sample 3.docx";

// Load the Word document
System::SharedPtr<Aspose::Words::Document> document = System::MakeObject<Aspose::Words::Document>(sampleFile);

// Output file path
System::String outputPath = u"OutputDirectory\\";

for (int i = 0; i < document->get_Sections()->get_Count(); i++)
{
// Split a document into smaller parts, in this instance split by section
auto section = document->get_Sections()->idx_get(i)->Clone();

// Create a new document
auto newDoc = System::MakeObject<Aspose::Words::Document>();
newDoc->get_Sections()->Clear();

//Add new section to the newly created document
auto newSection = System::StaticCast<Aspose::Words::Section>(newDoc->ImportNode(section, true));
newDoc->get_Sections()->Add(newSection);

// Save each section as a separate document
newDoc->Save(outputPath + u"SplitDocumentBySectionsOut_" + System::Convert::ToString(i) + u".docx");
}

以上便是使用 C++ 拆分 MS Word 文档详细步骤 ,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值