C#/VB.NET实现在Word文档中添加页眉和页脚

Karli ·
更新时间:2024-09-20
· 1383 次阅读

目录

程序环境

在 Word 文档中添加页眉和页脚

完整代码

效果图

页眉位于文档中每个页面的顶部区域,常用于显示文档的附加信息,可以插入时间、图形、公司微标、文档标题、文件名或作者姓名等;页脚位于文档中每个页面的底部的区域,常用于显示文档的附加信息,可以在页脚中插入文本或图形。今天这篇文章就将为大家展示如何以编程的方式在在 Word 文档中添加页眉和页脚。下面是我整理的思路及方法,并附上C#/VB.NET供大家参考。

程序环境

本次测试时,在程序中引入Free Spire.Doc for .NET。可通过以下方法引用 Free Spire.Doc.dll文件:

方法1:将 Free Spire.Doc for .NET下载到本地,解压,安装。安装完成后,找到安装路径下BIN文件夹中的 Spire.Doc.dll。然后在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径BIN文件夹下的dll文件添加引用至程序。

方法2:通过NuGet安装。可通过以下2种方法安装:

(1)可以在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理NuGet包”,然后搜索“Free Spire.Doc”,点击“安装”。等待程序安装完成。

(2)将以下内容复制到PM控制台安装。

Install-Package FreeSpire.Doc -Version 10.8.0

在 Word 文档中添加页眉和页脚

该表列出了操作中使用的主要类、属性和方法。

名称描述
Document类表示 Word 文档模型。
Document. LoadFromFile()方法加载 Word 文档。
Section 类表示 Word 文档中的一个节。
Document.Sections 属性获取文档的节。
HeaderFooter 类表示 Word 的页眉和页脚模型。
Section.HeadersFooters.Header属性获取当前节的页眉/页脚。
Paragraph 类表示文档中的段落。
HeaderFooter. AddParagraph() 方法在部分末尾添加段落。
TextRange 类表示文本范围。
Paragraph.AppendText()方法将文本附加到段落的末尾。
Document. SaveToFile()方法将文档保存为 Microsoft Word 或其他文件格式的文件。

添加页眉和页脚的详细步骤如下:

创建 Document 类的实例。

使用 Document.LoadFromFile(string fileName) 方法加载示例文档。

使用 Document.Sections 属性获取 Word 文档的指定节

添加页眉

通过HeadersFooters.Header 属性获取页眉。

使用HeaderFooter. AddParagraph()方法添加段落。并设置段落对齐方式。

使用 Paragraph.AppendText(string text) 方法追加文本并设置字体名称、大小、颜色等。

添加页脚

调用 HeadersFooter.Footer 属性获取页脚。

在页脚中添加段落和文本。

使用 Document. SaveToFile(string filename, FileFormat fileFormat) 方法保存 Word 文档。

完整代码

C#

using Spire.Doc; using Spire.Doc.Documents; using System.Drawing; using Spire.Doc.Fields; namespace AddHeaderAndFooter { class Program { static void Main(string[] args) { //创建 Document 类的实例 Document document = new Document(); //加载示例文档 document.LoadFromFile("测试文档.docx"); //获取 Word 文档的指定节 Section section = document.Sections[0]; //通过 HeadersFooters.Header 属性获取页眉 HeaderFooter header = section.HeadersFooters.Header; //添加段落并设置段落对齐样式 Paragraph headerPara = header.AddParagraph(); headerPara.Format.HorizontalAlignment = HorizontalAlignment.Left; //附加文本并设置字体名称、大小、颜色等。 TextRange textrange = headerPara.AppendText("《生死疲劳》" + "莫言"); textrange.CharacterFormat.FontName = "Arial"; textrange.CharacterFormat.FontSize = 13; textrange.CharacterFormat.TextColor = Color.DodgerBlue; textrange.CharacterFormat.Bold = true; //获取页脚、添加段落和附加文本 HeaderFooter footer = section.HeadersFooters.Footer; Paragraph footerPara = footer.AddParagraph(); footerPara.Format.HorizontalAlignment = HorizontalAlignment.Center; textrange = footerPara.AppendText("我不眷恋温暖的驴棚,我追求野性的自由。"); textrange.CharacterFormat.Bold = false; textrange.CharacterFormat.FontSize = 11; //保存文件 document.SaveToFile("结果文档.docx", FileFormat.Docx); } } }

VB.NET

Imports Spire.Doc Imports Spire.Doc.Documents Imports System.Drawing Imports Spire.Doc.Fields Namespace AddHeaderAndFooter Friend Class Program Private Shared Sub Main(ByVal args As String()) '创建 Document 类的实例 Dim document As Document = New Document() '加载示例文档 document.LoadFromFile("生死疲劳.docx") '获取 Word 文档的指定节 Dim section As Section = document.Sections(0) '通过 HeadersFooters.Header 属性获取页眉 Dim header As HeaderFooter = section.HeadersFooters.Header '添加段落并设置段落对齐样式 Dim headerPara As Paragraph = header.AddParagraph() headerPara.Format.HorizontalAlignment = HorizontalAlignment.Left '附加文本并设置字体名称、大小、颜色等。 Dim textrange As TextRange = headerPara.AppendText("《生死疲劳》" & "莫言") textrange.CharacterFormat.FontName = "宋体" textrange.CharacterFormat.FontSize = 12 textrange.CharacterFormat.TextColor = Color.DodgerBlue textrange.CharacterFormat.Bold = True '获取页脚、添加段落和附加文本 Dim footer As HeaderFooter = section.HeadersFooters.Footer Dim footerPara As Paragraph = footer.AddParagraph() footerPara.Format.HorizontalAlignment = HorizontalAlignment.Center textrange = footerPara.AppendText("我不眷恋温暖的驴棚,我追求野性的自由。") textrange.CharacterFormat.Bold = False textrange.CharacterFormat.FontSize = 11 '保存文件 document.SaveToFile("结果文档.docx", FileFormat.Docx) End Sub End Class End Namespace 效果图

以上就是C#/VB.NET实现在Word文档中添加页眉和页脚 的详细内容,更多关于C# Word添加页眉页脚 的资料请关注软件开发网其它相关文章!



页脚 C# vb 页眉和页脚 word net vb.net

需要 登录 后方可回复, 如果你还没有账号请 注册新账号