码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Word控件Spire.Doc 【表单域】教程(六):C#在word文档中插入和删除TOC


     

    我们已经演示了如何在创建 word 文档时添加一个全新的 TOC 。本文将向您展示如何将 TOC 插入到现有的带有样式的 Word 文档中,并从 Word 文档中删除 TOC。

    Spire.Doc for.NET 最新下载

    首先,查看带有 Title、Heading1 和 Heading 2 样式的示例文档:

    下面的代码片段显示了如何将目录 (TOC) 插入到文档中。

    using Spire.Doc;
    using Spire.Doc.Documents;
    using Spire.Doc.Fields;
    using System.Text.RegularExpressions;
    namespace InsertTOC
    {
    class Program
    {
    static void Main(string[] args)
    {
    //Create a new instance of Document and load the document from file.
    Document doc = new Document();
    doc.LoadFromFile("Sample.docx", FileFormat.Docx2010);
    
    //Add the TOC to the document
    TableOfContent toc = new TableOfContent(doc, "{\\o \"1-3\" \\h \\z \\u}");
    Paragraph p = doc.LastSection.Paragraphs[0];
    p.Items.Add(toc);
    p.AppendFieldMark(FieldMarkType.FieldSeparator);
    p.AppendText("TOC");
    p.AppendFieldMark(FieldMarkType.FieldEnd);
    doc.TOC = toc;
    
    //Update the table of contents
    doc.UpdateTableOfContents();
    
    //Save the document to file
    doc.SaveToFile("Result.docx", FileFormat.Docx);
    
    }
    }
    }
    

    从文档中删除目录

    using Spire.Doc;
    using System.Text.RegularExpressions;
    namespace RemoveTOC
    {
    class Program
    {
    static void Main(string[] args)
    {
    //load the document from file with TOC
    Document doc = new Document();
    doc.LoadFromFile("Result.docx", FileFormat.Docx2010);
    
    //get the first body from the first section
    Body body = doc.Sections[0].Body;
    
    //remove TOC from first body
    Regex regex = new Regex("TOC\\w+");
    for (int i = 0; i < body.Paragraphs.Count; i++)
    {
    if (regex.IsMatch(body.Paragraphs[i].StyleName))
    {
    body.Paragraphs.RemoveAt(i);
    i--;
    }
    }
    //save the document to file
    doc.SaveToFile("RemoveTOC.docx", FileFormat.Docx2010);
    
    }
    }
    }
    

    欢迎下载|体验更多E-iceblue产品 技术交流Q群(767755948)  

  • 相关阅读:
    Worthington丨Worthington 蛋白酶K(Proteinase K)说明书
    Go 项目配置文件的定义和读取
    linux操作系统虚拟机的环境配置
    训练模型时,wandb关闭问题。你以为加了一句 wandb = None 就能关闭了吗?
    Java ArrayList扩容底层原理深挖
    分布式消息队列RocketMQ详细下载安装教程
    非递归方式实现二叉树四种遍历
    每日一练 | 华为认证真题练习Day130
    软考高级软件架构师学习笔记一
    带你了解如何防御DDoS攻击
  • 原文地址:https://blog.csdn.net/m0_67129275/article/details/126135557
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号