页眉和页脚可以在Word里显示一些额外信息,如公司标识、公司信息、文章的作者等信息,可以是文本或者图片的形式,这篇文章主要就是介绍怎么使用
Spire.Doc for .NET Word文档处理控件对Word文件插入带图片的页眉页脚。
Spire.Doc for .NET专门为页眉页脚提供了一个HeaderFooter类,可以帮助开发人员生成新的页眉页脚,在生成页眉页脚时,首先需要创建一个HeaderFooter的实例,然后利用该类的AddParagraph()方法为页眉添加一个段落,然后使用Paragraph.AppendPicture(Image image) 方法为段落插入一个图片,同样地也可以利用Paragraph.AppendText(string text)方法为页眉设置文本,并且可以很好地设置文本和图片的一些布局位置,字体大小,颜色等信息,具体完整的代码可以参考下面:
using System.Drawing;
namespace ImageHeaderFooter
{
class Program
{
static void Main(string[] args)
{
//Load Document
Document document = new Document();
document.LoadFromFile(@"E:\Work\Documents\
Spire.Doc for .NET.docx");
//Initialize a Header Instance
HeaderFooter header = document.Sections[0].HeadersFooters.Header;
//Add Header Paragraph and Format
Paragraph paragraph = header.AddParagraph();
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Right;
//Append Picture for Header Paragraph and Format
DocPicture headerimage = paragraph.AppendPicture(Image.FromFile(@"E:\Logo\doclog.png"));
headerimage.VerticalAlignment = ShapeVerticalAlignment.Bottom;
//Initialize a Footer Instance
HeaderFooter footer = document.Sections[0].HeadersFooters.Footer;
//Add Footer Paragraph and Format
Paragraph paragraph2 = footer.AddParagraph();
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Left;
//Append Picture and Text for Footer Paragraph
DocPicture footerimage = paragraph2.AppendPicture(Image.FromFile(@"E:\Logo\logo.jpeg"));
TextRange TR = paragraph2.AppendText("Copyright © 2013 e-iceblue. All Rights Reserved.");
TR.CharacterFormat.FontName = "Arial";
TR.CharacterFormat.FontSize = 10;
TR.CharacterFormat.TextColor = Color.Black;
//Save and Launch
document.SaveToFile("ImageHeaderFooter.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("ImageHeaderFooter.docx");
}
}
}