C#:在 Word 文档中插入段落_c# word怎么继续编号

C#:在 Word 文档中插入段落_c# word怎么继续编号

编码文章call10242025-10-13 17:03:183A+A-

在Word文档中插入段落是构建结构清晰、条理分明文档的基础操作。段落能够将大段文本进行合理划分,使读者更易于理解行文思路并快速定位所需信息。在Word中,您可以通过添加新段落来呈现新的观点或补充相关信息。本文将演示如何使用Spire.Doc for .NET库,通过C#代码实现在Word文档中插入新段落的操作方法。

安装 Spire.Doc for .NET

首先,您需要将 Spire.Doc for .NET 软件包中的 DLL 文件添加为项目的引用。这些 DLL 文件可以通过此链接下载,或通过 NuGet 安装。

 PM> Install-Package Spire.Doc

使用C#在Word文档末尾添加段落

若要在文档末尾添加新段落,您需要通过Document.LastSection属性获取文档的最后一个节,然后通过Section.AddParagraph()方法在该节末尾添加段落。具体代码示例如下:

using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;

namespace AddParagraph
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建Document实例
            Document doc = new Document();

            //加载Word文档
            doc.LoadFromFile("Test.docx");

            //获取文档的最后一个节
            Section section = doc.LastSection;

            //在节末尾添加段落和对应的文字内容
            Paragraph para = section.AddParagraph();
            para.AppendText("Add a paragraph to the end of the document.");

            //设置段落文本的字体名称、大小和样式
            ParagraphStyle style = new ParagraphStyle(doc);
            style.Name = "Style1";
            style.CharacterFormat.FontName = "Times New Roman";
            style.CharacterFormat.FontSize = 12;
            style.CharacterFormat.TextColor = Color.Blue;
            style.CharacterFormat.Bold = true;
            doc.Styles.Add(style);
            para.ApplyStyle("Style1");
            para.Format.BeforeSpacing = 10;

            //保存文件
            doc.SaveToFile("AddParagraph.docx", FileFormat.Docx2016);
        }
    }
}

在C#中向Word文档指定位置插入段落

您还可以通过Section.Paragraphs.Insert(int index, IParagraph paragraph)方法添加段落并将其插入指定位置。

具体代码示例如下:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace InsertParagraph
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建Document实例
            Document doc = new Document();

            //加载Word文档
            doc.LoadFromFile("Test.docx");
        

            //定位到文档第一节
            Section section = doc.Sections[0];

            //添加段落以及对应的文本内容
            Paragraph para = section.AddParagraph();
            TextRange textRange = para.AppendText("Insert a paragraph at a specified location in the Word document.");

            //设置段落文本字体,颜色,大小
            textRange.CharacterFormat.TextColor = Color.Blue;
            textRange.CharacterFormat.FontName = "Times New Roman";
            textRange.CharacterFormat.FontSize = 14;
            textRange.CharacterFormat.UnderlineStyle = UnderlineStyle.Single;

            //将文本插入为第三段
            section.Paragraphs.Insert(2, para);

            //在段落后设置空行
            para.Format.AfterSpacing = 10;

            //保存文件
            doc.SaveToFile("InsertParagraph.docx", FileFormat.Docx2016);
        }
    }
}

申请临时许可

如果您需要Spire.PDF for .NET的完整权限并希望解锁该产品的更多功能,可联系官方工作人员申请免费的30天临时许可证。

点击这里复制本文地址 以上内容由文彬编程网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

文彬编程网 © All Rights Reserved.  蜀ICP备2024111239号-4