一、下载用于拆分 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::SharedPtr Aspose::Words::Node node) 方法将克隆的部分添加到新创建的 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 文档详细步骤,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。
欢迎下载|体验更多Aspose产品
点此获取更多Aspose产品信息 或 加入Aspose技术交流群()
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!