翻譯|使用教程|編輯:胡濤|2023-03-17 09:57:33.770|閱讀 112 次
概述:本文介紹用C#/VB.NET:在 Word 文檔中添加頁眉和頁腳
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
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 包中的 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 或其他文件格式的文件。 |
以下是添加頁眉和頁腳的步驟。
[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 文檔中添加頁眉和頁腳,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(767755948)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn