1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Word控件Spire.Doc 【页眉页脚】教程(1):用C#/VB.NET:在 Word 文档中添加页眉和页脚

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

时间:2023-10-14 22:41:50

相关推荐

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

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

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

Spire.Doc 最新下载/product/3368/download

安装适用于 .NET 的 Spire.Doc

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

PM> Install-Package Spire.Doc

添加页眉和页脚

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

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

创建文档类的实例。使用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 classDocument document = new Document();//Load a Word documentdocument.LoadFromFile("input.docx");//Get the first section of Word DocumentSection section = document.Sections[0];//Get header via HeadersFooters.Header propertyHeaderFooter header = section.HeadersFooters.Header;//Add a paragraph and set paragraph alignment styleParagraph 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 textHeaderFooter footer = section.HeadersFooters.Footer;Paragraph footerPara = footer.AddParagraph();footerPara.Format.HorizontalAlignment = HorizontalAlignment.Center;textrange = footerPara.AppendText("Copyright © All Rights Reserved.");textrange.CharacterFormat.Bold = false;textrange.CharacterFormat.FontSize = 11;//Save to filedocument.SaveToFile("output.docx", FileFormat.Docx);}}}

[]

Imports Spire.DocImports Spire.Doc.DocumentsImports System.DrawingImports Spire.Doc.FieldsNamespace AddHeaderAndFooterClass ProgramPrivate Shared Sub Main(args As String())'Create an instance of Document classDim document As New Document()'Load a Word documentdocument.LoadFromFile("input.docx")'Get the first section of Word DocumentDim section As Section = document.Sections(0)'Get header via HeadersFooters.Header propertyDim header As HeaderFooter = section.HeadersFooters.Header'Add a paragraph and set paragraph alignment styleDim 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 = 13textrange.CharacterFormat.TextColor = Color.DodgerBluetextrange.CharacterFormat.Bold = True'Get footer, add paragraph and append textDim footer As HeaderFooter = section.HeadersFooters.FooterDim footerPara As Paragraph = footer.AddParagraph()footerPara.Format.HorizontalAlignment = HorizontalAlignment.Centertextrange = footerPara.AppendText("Copyright © All Rights Reserved.")textrange.CharacterFormat.Bold = Falsetextrange.CharacterFormat.FontSize = 11'Save to filedocument.SaveToFile("output.docx", FileFormat.Docx)End SubEnd ClassEnd Namespace

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。