1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Word处理控件Aspose.Words功能演示:使用 C++ 拆分 MS Word 文档

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

时间:2023-04-17 10:47:11

相关推荐

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

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

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

Aspose.words 最新下载(qun:761297826)/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 documentSystem::String sampleFile = u"SourceDirectory\\Sample 3.docx";// Load the Word documentSystem::SharedPtr<Aspose::Words::Document> document = System::MakeObject<Aspose::Words::Document>(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::SharedPtr<Aspose::Words::Document> document = System::MakeObject<Aspose::Words::Document>(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 文件中添加新部分,请使用布局>中断>分节选项。以下是使用 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::SharedPtrAspose::Words::Nodenode) 方法将克隆的部分添加到新创建的 Word 文档中。最后,使用Document->Save (System::String fileName)方法保存 Word 文件。

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

// Sample Word documentSystem::String sampleFile = u"SourceDirectory\\Sample 3.docx";// Load the Word documentSystem::SharedPtr<Aspose::Words::Document> document = System::MakeObject<Aspose::Words::Document>(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 sectionauto section = document->get_Sections()->idx_get(i)->Clone();// Create a new documentauto newDoc = System::MakeObject<Aspose::Words::Document>();newDoc->get_Sections()->Clear();//Add new section to the newly created documentauto newSection = System::StaticCast<Aspose::Words::Section>(newDoc->ImportNode(section, true));newDoc->get_Sections()->Add(newSection);// Save each section as a separate documentnewDoc->Save(outputPath + u"SplitDocumentBySectionsOut_" + System::Convert::ToString(i) + u".docx");}

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。