亲宝软件园·资讯

展开

C# 在Word中插入横线(附VB.NET代码)

TO BE 人气:0

文本介绍如何通过C#程序代码在Word文档中添加横线。另附VB.NET代码供参考。

程序环境:

● Visual Studio

● .net framework 4.8

● Word测试文档:.docx 2013

● Word类库工具:spire.doc.dll 9.6.1

在VS中的“解决方案资源管理器”中引用spire.doc.dll文件,如下图:

C#

using Spire.Doc;

using Spire.Doc.Documents;

 

namespace AddHorizontalLine

{

    class Program

    {

        static void Main(string[] args)

        {

            //加载Word测试文档

            Document doc = new Document();

            doc.LoadFromFile("test.docx");

 

            //获取节和段落

            Section section = doc.Sections[0];

            Paragraph paragraph = section.Paragraphs[4];

 

            //添加横线

            paragraph.AppendHorizonalLine();

 

            //保存文档

            doc.SaveToFile("output.docx");

            System.Diagnostics.Process.Start("output.docx");

        }

    }

}

 

VB.NET

Imports Spire.Doc

Imports Spire.Doc.Documents

 

Namespace AddHorizontalLine

         Class Program

                   Private Shared Sub Main(args As String())

                            '加载Word测试文档

                            Dim doc As New Document()

                            doc.LoadFromFile("test.docx")

 

                            '获取节和段落

                            Dim section As Section = doc.Sections(0)

                            Dim paragraph As Paragraph = section.Paragraphs(4)

 

                            '添加横线

                            paragraph.AppendHorizonalLine()

 

                            '保存文档

                            doc.SaveToFile("output.docx")

                            System.Diagnostics.Process.Start("output.docx")

 

                   End Sub

         End Class

End Namespace

 

横线添加效果:

—End—

加载全部内容

相关教程
猜你喜欢
用户评论