頁眉位於文檔中每個頁面的頂部區域,常用於顯示文檔的附加信息,可以插入時間、圖形、公司微標、文檔標題、文件名或作者姓名等;頁腳位於文檔中每個頁面的底部的區域,常用於顯示文檔的附加信息,可以在頁腳中插入文本或圖形。今天這篇文章就將為大家展示如何以編程的方式在在 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
效果圖
—本文完—