Word控件Spire.Doc 【页眉页脚】教程(1):用C#/VB.NET:在 Word 文档中添加页眉和页脚

Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。 

E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式

Spire.Doc for.NET 最新下载icon-default.png?t=N176https://www.evget.com/product/3368/download

安装适用于 .NET 的 Spire.Doc

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

PM> Install-Package Spire.Doc

添加页眉和页脚

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

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

以下是添加页眉和页脚的步骤。

  • 创建文档类的实例。
  • 使用Document.LoadFromFile(string fileName)方法加载示例文档。
  • 使用Document.Sections属性获取 Word 文档的指定部分
  • 添加标题
    • 使用HeadersFooters.Header属性获取标头。
    • 使用 HeaderFooter 添加段落。AddParagraph()方法并设置段落对齐方式。
    • 使用Paragraph.AppendText(string text)方法追加文本并设置字体名称、大小、颜色等。
  • 添加页脚
    • 使用HeadersFooters.Footer属性获取页脚。
    • 在页脚中添加段落和文本。
  • 使用文档保存 Word 文档。SaveToFile(string filename, FileFormat fileFormat )方法。

[C#]

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

namespace AddHeaderAndFooter
{
class Program
{
static void Main(string[] args)
{
//Create an instance of Document class
Document document = new Document();

//Load a Word document
document.LoadFromFile("input.docx");

//Get the first section of Word Document
Section section = document.Sections[0];

//Get header via HeadersFooters.Header property
HeaderFooter header = section.HeadersFooters.Header;

//Add a paragraph and set paragraph alignment style
Paragraph headerPara = header.AddParagraph();
headerPara.Format.HorizontalAlignment = HorizontalAlignment.Left;

//Append text and set font name, size, color,etc.
TextRange textrange = headerPara.AppendText("E-iceblue Co. Ltd." + "\n Your Office Development Master");
textrange.CharacterFormat.FontName = "Arial";
textrange.CharacterFormat.FontSize = 13;
textrange.CharacterFormat.TextColor = Color.DodgerBlue;
textrange.CharacterFormat.Bold = true;

//Get footer, add paragraph and append text
HeaderFooter footer = section.HeadersFooters.Footer;
Paragraph footerPara = footer.AddParagraph();
footerPara.Format.HorizontalAlignment = HorizontalAlignment.Center;
textrange = footerPara.AppendText("Copyright © 2021 All Rights Reserved.");
textrange.CharacterFormat.Bold = false;
textrange.CharacterFormat.FontSize = 11;

//Save to file
document.SaveToFile("output.docx", FileFormat.Docx);
}
}
}

[VB.NET]

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Drawing
Imports Spire.Doc.Fields

Namespace AddHeaderAndFooter
Class Program
Private Shared Sub Main(args As String())
'Create an instance of Document class
Dim document As New Document()

'Load a Word document
document.LoadFromFile("input.docx")

'Get the first section of Word Document
Dim section As Section = document.Sections(0)

'Get header via HeadersFooters.Header property
Dim header As HeaderFooter = section.HeadersFooters.Header

'Add a paragraph and set paragraph alignment style
Dim headerPara As Paragraph = header.AddParagraph()
headerPara.Format.HorizontalAlignment = HorizontalAlignment.Left

'Append text and set font name, size, color ,etc.
Dim textrange As TextRange = headerPara.AppendText("E-iceblue Co. Ltd." + vbLf & " Your Office Development Master")
textrange.CharacterFormat.FontName = "Arial"
textrange.CharacterFormat.FontSize = 13
textrange.CharacterFormat.TextColor = Color.DodgerBlue
textrange.CharacterFormat.Bold = True

'Get footer, add paragraph and append text
Dim footer As HeaderFooter = section.HeadersFooters.Footer
Dim footerPara As Paragraph = footer.AddParagraph()
footerPara.Format.HorizontalAlignment = HorizontalAlignment.Center
textrange = footerPara.AppendText("Copyright © 2021 All Rights Reserved.")
textrange.CharacterFormat.Bold = False
textrange.CharacterFormat.FontSize = 11

'Save to file
document.SaveToFile("output.docx", FileFormat.Docx)
End Sub
End Class
End Namespace

以上便是用C#/VB.NET:在 Word 文档中添加页眉和页脚,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值