admin管理员组文章数量:1627760
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 最新下载https://www.evget/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 文档中添加页眉和页脚,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。
版权声明:本文标题:Word控件Spire.Doc 【页眉页脚】教程(1):用C#VB.NET:在 Word 文档中添加页眉和页脚 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1728997947a1182359.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论