Word开发工具Aspose.Words功能演示:使用C ++分割MS Word文档

  • 使用C ++按页面拆分Word文档
  • 使用页面范围分割Word文档
  • 使用C ++按部分拆分Word文档

Aspose.Words for C ++是本机C ++库,允许您生成,更改和转换Microsoft Word文档。此外,它还支持将Word文档拆分为多个文件。

如果你还没有用过C ++版Aspose.Words可以点击这里下载最新版测试。

整合所有格式的Aspose.Total永久授权正在火热促销中,联系客服立马1分钟了解全部咨询!


使用C ++按页面拆分Word文档

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

  • 使用Document类加载Word文档 。
  • 创建DocumentPageSplitter 类的实例, 并使用上一步中创建的Document对象对其进行初始化。
  • 循环浏览文档的页面。
  • 使用DocumentPageSplitter.GetDocumentOfPage(int32_t PageIndex)方法提取每个页面。
  • 使用Document-> Save(System :: String fileName)方法保存文档。

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

// Sample Word documentSystem::String sampleFile = u"SourceDirectory\Sample 3.docx";// Load the Word documentSystem::SharedPtrdocument = System::MakeObject(sampleFile);// Create and initialize document page splitterDocumentPageSplitter splitter = DocumentPageSplitter(document);// Output file pathSystem::String outputPath = u"OutputDirectory\";// Save each page as a separate documentfor (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 documentSystem::String sampleFile = u"SourceDirectory\Sample 3.docx";// Load the Word documentSystem::SharedPtrdocument = System::MakeObject(sampleFile);// Create and initialize document page splitterDocumentPageSplitter splitter = DocumentPageSplitter(document);// Get the page rangeauto pageDoc = splitter.GetDocumentOfPageRange(2, 3);// Output file pathSystem::String outputPath = u"OutputDirectory\";// Save as DOCX filepageDoc->Save(outputPath + u"SplitDocumentByPageRangeOut.docx");

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

Word文件可以包含一个或多个部分,这些部分可能具有不同的格式,并且可以由任意数量的页面组成。要在Word文件中添加新部分,请使用Layout> Breaks> Section Breaks选项。以下是使用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(系统:: SharedPtr节点)方法将克隆的部分添加到新创建的Word文档中。
  • 最后,使用Document-> Save(System :: String fileName)方法保存Word文件。

下面是示例代码,用于按部分拆分Word文档。

// Sample Word documentSystem::String sampleFile = u"SourceDirectory\Sample 3.docx";// Load the Word documentSystem::SharedPtrdocument = System::MakeObject(sampleFile);// Output file pathSystem::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();    newDoc->get_Sections()->Clear();    //Add new section to the newly created document    auto newSection = System::StaticCast(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");}


还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年1月2日
下一篇 2021年1月2日

相关推荐

发表回复

登录后才能评论