C#/VB.NET實現在Word文檔中添加頁眉和頁腳
頁眉位于文檔中每個頁面的頂部區(qū)域,常用于顯示文檔的附加信息,可以插入時間、圖形、公司微標、文檔標題、文件名或作者姓名等;頁腳位于文檔中每個頁面的底部的區(qū)域,常用于顯示文檔的附加信息,可以在頁腳中插入文本或圖形。今天這篇文章就將為大家展示如何以編程的方式在在 Word 文檔中添加頁眉和頁腳。下面是我整理的思路及方法,并附上C#/VB.NET供大家參考。
程序環(huán)境
本次測試時,在程序中引入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 文檔中的一個節(jié)。 |
| Document.Sections 屬性 | 獲取文檔的節(jié)。 |
| HeaderFooter 類 | 表示 Word 的頁眉和頁腳模型。 |
| Section.HeadersFooters.Header屬性 | 獲取當前節(jié)的頁眉/頁腳。 |
| Paragraph 類 | 表示文檔中的段落。 |
| HeaderFooter. AddParagraph() 方法 | 在部分末尾添加段落。 |
| TextRange 類 | 表示文本范圍。 |
| Paragraph.AppendText()方法 | 將文本附加到段落的末尾。 |
| Document. SaveToFile()方法 | 將文檔保存為 Microsoft Word 或其他文件格式的文件。 |
添加頁眉和頁腳的詳細步驟如下:
- 創(chuàng)建 Document 類的實例。
- 使用 Document.LoadFromFile(string fileName) 方法加載示例文檔。
- 使用 Document.Sections 屬性獲取 Word 文檔的指定節(jié)
添加頁眉
- 通過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)
{
//創(chuàng)建 Document 類的實例
Document document = new Document();
//加載示例文檔
document.LoadFromFile("測試文檔.docx");
//獲取 Word 文檔的指定節(jié)
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())
'創(chuàng)建 Document 類的實例
Dim document As Document = New Document()
'加載示例文檔
document.LoadFromFile("生死疲勞.docx")
'獲取 Word 文檔的指定節(jié)
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# TabControl控件中TabPage選項卡切換時的觸發(fā)事件問題
這篇文章主要介紹了C# TabControl控件中TabPage選項卡切換時的觸發(fā)事件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04

