在 Word 文档中,您可以在页眉或页脚中添加时间、日期、标题、参考信息、页码、内容描述和图像/徽标来丰富文档。本文将详细介绍如何使用 Spire.Doc for .NET 在 Word 文档中添加页眉和页脚。
安装 Spire.Doc for .NET
首先,您需要添加 Spire.Doc for .NET 包中包含的 DLL 文件作为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过 NuGet 安装。
PM> Install-Package Spire.Doc在 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#
- VB.NET
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("成都冰蓝科技" + "\n您的办公文档开发技术专家");
            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("版权所有 © 2022 ");
            textrange.CharacterFormat.Bold = false;
            textrange.CharacterFormat.FontSize = 11;
            //保存文件
            document.SaveToFile("结果文档.docx", FileFormat.Docx);
        }
    }
}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("成都冰蓝科技" & vbLf & "您的办公文档开发技术专家")
            textrange.CharacterFormat.FontName = "Arial"
            textrange.CharacterFormat.FontSize = 13
            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("版权所有 © 2022 ") textrange.CharacterFormat.Bold = False textrange.CharacterFormat.FontSize = 11 '保存文件
            document.SaveToFile("结果文档.docx", FileFormat.Docx)
        End Sub
    End Class
End Namespace
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
 



 
					



