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


    MS Word文档被广泛用于保存和共享信息。在某些情况下,可能需要从可能位于不同部分或页面的Word文档中拆分数据。另外,还可能需要将单个文档的页面拆分为多个文档。

    根据这种情况,在本文中,将展示如何使用C#以编程方式拆分MS Word文档。让我们继续学习以下用例:

    • 使用C#按部分拆分Word文档
    • 使用C#逐页拆分Word文档
    • 使用页面范围使用C#拆分Word文档

    >>Aspose.Words for .NET已经更新至最新版,此常规的每月版本中有81项改进和修复,主要特点包括实现了Markdown的“内嵌图片”功能、为字体名称处理添加了新的字体替换规则等三大新功能。

    Aspose.Words for .NET最新版下载(技术交流Q群:761297826)icon-default.png?t=M85Bhttps://www.evget.com/product/564/download

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

    部分是指文档中可以应用不同格式的部分。一个部分可以由一个页面,一个页面范围或整个文档组成。分节符用于将文档拆分为多个部分。以下是使用Aspose.Words for .NET根据文档的部分拆分Word文档的步骤。

    • 使用Document类加载Word文档。
    • 使用Document.Sections属性循环浏览页面部分。
    • 将节克隆到新的Section对象中。
    • 创建一个新的Document对象。
    • 使用Document.Sections.Add(Section)方法将该部分添加到新的Document中。
    • 使用Document.Save(String)方法保存文档。

    下面的代码示例演示如何使用C#按部分拆分MS Word文档。

    // Open a Word document
    Document doc = new Document("document.docx"); 
    
    for (int i = 0; i < doc.Sections.Count; i++) { // Split a document into smaller parts, in this instance split by section Section section = doc.Sections[i].Clone(); // Create a new document Document newDoc = new Document(); newDoc.Sections.Clear(); Section newSection = (Section)newDoc.ImportNode(section, true); newDoc.Sections.Add(newSection); // Save each section as a separate document newDoc.Save($"splitted_{i}.docx"); }

    使用C#逐页拆分Word文档

    在某些情况下,Word文档在每页上都包含类似类型的信息,例如发票或收据。在这种情况下,可以拆分文档的页面,以便将每个发票另存为单独的文档。若要逐页拆分文档,可以使用 基于Aspose.Words for .NET 的帮助程序类DocumentPageSplitter。可以按照以下步骤简单地在项目中复制该类并逐页拆分Word文档。

    • 使用Document类加载Word文档。
    • 创建一个DocumentPageSplitter类的对象,并使用Document对象对其进行初始化。
    • 遍历文档页面。
    • 使用DocumentPageSplitter.GetDocumentOfPage(int PageIndex)方法将每个页面提取到一个新的Document对象中。
    • 使用Document.Save(String)方法保存每个文档。

    下面的代码示例演示如何使用C#按页面拆分Word文档。

    // Open a Word document
    Document doc = new Document("Document.docx");
    
    // Create and initialize the document page splitter
    DocumentPageSplitter splitter = new DocumentPageSplitter(doc);
    
    // Save each page as a separate document
    for (int page = 1; page <= doc.PageCount; page++) { Document pageDoc = splitter.GetDocumentOfPage(page); pageDoc.Save($"spliteed_{page}.docx"); }

    使用C#按页面范围拆分Word文档

    还可以使用DocumentPageSplitter 类指定页面范围以将其与原始文档分开。例如,如果需要将页面从2拆分为4,只需在DocumentPageSplitter.GetDocumentOfPageRange(int StartIndex,int EndIndex)方法中指定起始页和结束页的索引即可。

    下面的代码示例演示如何使用C#从Word文档中拆分页面范围。

    // Open a Word document
    Document doc = new Document("document.docx");
    
    // Create and initialize document page splitter
    DocumentPageSplitter splitter = new DocumentPageSplitter(doc);
    
    // Get the page range
    Document pageDoc = splitter.GetDocumentOfPageRange(3, 6);
    pageDoc.Save("splitted.docx");
  • 相关阅读:
    雪崩问题以及sentinel的使用
    Java 基础语法
    【PowerQuery】Excel 一分钟以内刷新PowerQuery数据
    Mybatis入门
    基于SpringBoot大学生心理健康咨询管理系统的分析与设计
    Android Span进阶之路——ClickableSpan
    JUC并发编程——Stream流式计算(基于狂神说的学习笔记)
    基于springboot实现音乐网站与分享平台项目【项目源码+论文说明】
    java计算机毕业设计华北地区阔叶林木叶部病虫害图像管理系统MyBatis+系统+LW文档+源码+调试部署
    【CBAM 解读】混合注意力机制:Convolutional Block Attention Module
  • 原文地址:https://blog.csdn.net/m0_67129275/article/details/126847898