当前位置: 首页 > news >正文

C# 使用FreeSpire.doc 生成带有页码的目录

1.使用nuget获取dll。

2.全部代码如下。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Pdf;

namespace test01
{
    public partial class Form1 : Form
    {

        private string OpenFilePath;//存储选择到的文件的完整路径

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //免费版本,在加载或操作Word文档时,要求Word文档不超过500个段落和25个表格

            // 创建Document类的对象
            Document doc = new Document();

            string path = OpenFilePath;
            // 载入Word文档
            doc.LoadFromFile(path);
            // 定义占位符文本,目录后
            string placeholderText = "目录";

            // 获取文档中的所有节
            Spire.Doc.Collections.SectionCollection sections = doc.Sections;
            Section section = sections[0];
     
            // 遍历节中的每个段落
            for (int j = 0 ; j < section.Paragraphs.Count ; j++)
            {
                
                Paragraph paragraph = section.Paragraphs[j];
                Console.WriteLine(paragraph.Text);

                if (paragraph.Text.Contains(placeholderText)) //需要手动或者代码插入 目录 两个字居中显示,还要换行
                {
                    // 在找到占位符的段落后插入目录
                    // 在新段落中创建一个展示1到3级标题的目录
                    paragraph.AppendTOC(1, 3);
                    //paragraph.getFormat().setPageBreakAfter(false);

                    //paragraph.Format.setPageBreakBefore(true);
                    break;
                }
            }

            // 更新目录
            doc.UpdateTableOfContents();

            //更新页码
            //doc.UpdateTOCPageNumbers();

            //doc.TOC
            doc.SaveToFile(path);

            /*
            //另存为pdf,免费版本的只可以三页
            ToPdfParameterList toPdf = new ToPdfParameterList();
            toPdf.PdfConformanceLevel = Spire.Doc.PdfConformanceLevel.Pdf_A1B;
            doc.SaveToFile("result.Pdf", toPdf);
            */

            doc.Dispose();
            //todo 如果需要处理空行,则需要全部完成后重新额外在更新一次文档目录 需要重新打开word
            MessageBox.Show("成功了!!!");

        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "浏览";//设置对话框标题栏的内容
            ofd.Filter = "文本文件|*docx;*.doc";  //这是设置对话框内显示的指定后缀类型文件(可设置多个)
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                OpenFilePath = ofd.FileName;
                this.textBox1.Text = OpenFilePath;
            }
            else
            {
                return;
            }
        }
    }
}

相关文章:

  • 力扣 438.找到字符串中所有字母异位词
  • osgearth视点坐标及鼠标交点坐标的信息显示(七)
  • 仿叮咚买菜鸿蒙原生APP
  • rabbitmq五种模式的实现——springboot
  • 线性代数中的正交和标准正交向量
  • 数据结构:顺序表
  • flutter 专题四十八 Google发布Flutter 2.0正式版,支持全平台程序构建
  • 如何在不依赖函数调用功能的情况下结合工具与大型语言模型
  • ranges::set_intersection set_union set_difference set_symmetric_difference
  • ollama本地部署 deepseek离线模型安装 一套从安装到UI运行
  • 【系列专栏】银行IT的云原生架构-存储架构-数据库部署 10
  • python+halcon 解读labelme标注生成marksimage
  • 【ISO 14229-1:2023 UDS诊断全量测试用例清单系列:第十九节】
  • Jetpack Compose系列教程之(10)——State及remeber
  • MySQL的SQL执行流程
  • 机器学习(四)
  • 智能车摄像头开源—8 元素处理
  • 【FastAPI】8.FastAPI结合Jinja2模板完成一个待办列表
  • 【数据结构-并查集】力扣721. 账户合并
  • Pycharm 2024在解释器提供的python控制台中运行py文件
  • 如何反击右翼思潮、弥合社会分裂:加拿大大选镜鉴
  • 央行召开落实金融“五篇大文章”总体统计制度动员部署会议
  • 大学男生被捉奸后将女生推下高楼?桂林理工大学辟谣
  • 企业取消“大小周”引热议,半月谈:不能将显性加班变为隐性加班
  • 鄂湘赣“中三角”,能否走向文旅C位?
  • 伊朗港口爆炸已致46人死亡