本文将介绍通过Spire.Doc for .NET来设置Word中首页、奇偶页的页眉页脚不同的方法。
设置首页页眉页脚不同
C#
//创建Document类的对象,并加载测试文档
Document document = new Document();
document.LoadFromFile("test.docx");
//获取指定节,并设置页眉页脚首页不同属性为true
Section section = document.Sections[0];
section.PageSetup.DifferentFirstPageHeaderFooter = true;
//加载图片添加到首页页眉
Paragraph paragraph1 = section.HeadersFooters.FirstPageHeader.AddParagraph();
paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Left;
DocPicture headerimage = paragraph1.AppendPicture(Image.FromFile("2.png"));
//添加文字到首页页脚
Paragraph paragraph2 = section.HeadersFooters.FirstPageFooter.AddParagraph();
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Center;
TextRange FF = paragraph2.AppendText("首页页脚");
FF.CharacterFormat.FontSize = 12;
//添加页眉页脚到其他页面
Paragraph paragraph3 = section.HeadersFooters.Header.AddParagraph();
paragraph3.Format.HorizontalAlignment = HorizontalAlignment.Center;
TextRange NH = paragraph3.AppendText("非首页页眉");
NH.CharacterFormat.FontSize = 12;
Paragraph paragraph4 = section.HeadersFooters.Footer.AddParagraph();
paragraph4.Format.HorizontalAlignment = HorizontalAlignment.Center;
TextRange NF = paragraph4.AppendText("非首页页脚");
NF.CharacterFormat.FontSize = 12;
//保存文档
document.SaveToFile("result.docx", FileFormat.Docx2010);
VB.NET
'创建Document类的对象,并加载测试文档
Dim document As Document = New Document
document.LoadFromFile("test.docx")
'获取指定节,并设置页眉页脚首页不同属性为true
Dim section As Section = document.Sections(0)
section.PageSetup.DifferentFirstPageHeaderFooter = true
'加载图片添加到首页页眉
Dim paragraph1 As Paragraph = section.HeadersFooters.FirstPageHeader.AddParagraph
paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Left
Dim headerimage As DocPicture = paragraph1.AppendPicture(Image.FromFile("2.png"))
'添加文字到首页页脚
Dim paragraph2 As Paragraph = section.HeadersFooters.FirstPageFooter.AddParagraph
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Center
Dim FF As TextRange = paragraph2.AppendText("首页页脚")
FF.CharacterFormat.FontSize = 12
'添加页眉页脚到其他页面
Dim paragraph3 As Paragraph = section.HeadersFooters.Header.AddParagraph
paragraph3.Format.HorizontalAlignment = HorizontalAlignment.Center
Dim NH As TextRange = paragraph3.AppendText("非首页页眉")
NH.CharacterFormat.FontSize = 12
Dim paragraph4 As Paragraph = section.HeadersFooters.Footer.AddParagraph
paragraph4.Format.HorizontalAlignment = HorizontalAlignment.Center
Dim NF As TextRange = paragraph4.AppendText("非首页页脚")
NF.CharacterFormat.FontSize = 12
'保存文档
document.SaveToFile("result.docx", FileFormat.Docx2010)
设置效果:
设置奇偶页页眉页脚不同
C#
//创建Document类,并加载测试文档
Document document = new Document();
document.LoadFromFile("test.docx");
//获取指定节,并设置页眉页脚奇偶页不同的属性为true
Section section = document.Sections[0];
section.PageSetup.DifferentOddAndEvenPagesHeaderFooter = true;
//设置奇偶数页的页脚
Paragraph P1 = section.HeadersFooters.EvenFooter.AddParagraph();
TextRange EF = P1.AppendText("偶数页页脚");
EF.CharacterFormat.FontName = "Calibri";
EF.CharacterFormat.FontSize = 12;
EF.CharacterFormat.TextColor = Color.Green;
EF.CharacterFormat.Bold = true;
P1.Format.HorizontalAlignment = HorizontalAlignment.Right;
Paragraph P2 = section.HeadersFooters.OddFooter.AddParagraph();
TextRange OF = P2.AppendText("奇数页页脚");
P2.Format.HorizontalAlignment = HorizontalAlignment.Left ;
OF.CharacterFormat.FontName = "Calibri";
OF.CharacterFormat.FontSize = 12;
OF.CharacterFormat.Bold = true;
OF.CharacterFormat.TextColor = Color.Blue;
//设置奇偶数页的页眉
Paragraph P3 = section.HeadersFooters.OddHeader.AddParagraph();
TextRange OH = P3.AppendText("奇数页页眉");
P3.Format.HorizontalAlignment = HorizontalAlignment.Left;
OH.CharacterFormat.FontName = "Calibri";
OH.CharacterFormat.FontSize = 12;
OH.CharacterFormat.Bold = true;
OH.CharacterFormat.TextColor = Color.Blue;
Paragraph P4 = section.HeadersFooters.EvenHeader.AddParagraph();
TextRange EH = P4.AppendText("偶数页页眉");
P4.Format.HorizontalAlignment = HorizontalAlignment.Right;
EH.CharacterFormat.FontName = "Calibri";
EH.CharacterFormat.FontSize = 12;
EH.CharacterFormat.Bold = true;
EH.CharacterFormat.TextColor = Color.Green;
//保存文档
document.SaveToFile("result.docx", FileFormat.Docx2010);
VB.NET
'创建Document类,并加载测试文档
Dim document As Document = New Document
document.LoadFromFile("test.docx")
'获取指定节,并设置页眉页脚奇偶页不同的属性为true
Dim section As Section = document.Sections(0)
section.PageSetup.DifferentOddAndEvenPagesHeaderFooter = true
'设置奇偶数页的页脚
Dim P1 As Paragraph = section.HeadersFooters.EvenFooter.AddParagraph
Dim EF As TextRange = P1.AppendText("偶数页页脚")
EF.CharacterFormat.FontName = "Calibri"
EF.CharacterFormat.FontSize = 12
EF.CharacterFormat.TextColor = Color.Green
EF.CharacterFormat.Bold = true
P1.Format.HorizontalAlignment = HorizontalAlignment.Right
Dim P2 As Paragraph = section.HeadersFooters.OddFooter.AddParagraph
Dim OF As TextRange = P2.AppendText("奇数页页脚")
P2.Format.HorizontalAlignment = HorizontalAlignment.Left
OF.CharacterFormat.FontName = "Calibri"
OF.CharacterFormat.FontSize = 12
OF.CharacterFormat.Bold = true
OF.CharacterFormat.TextColor = Color.Blue
'设置奇偶数页的页眉
Dim P3 As Paragraph = section.HeadersFooters.OddHeader.AddParagraph
Dim OH As TextRange = P3.AppendText("奇数页页眉")
P3.Format.HorizontalAlignment = HorizontalAlignment.Left
OH.CharacterFormat.FontName = "Calibri"
OH.CharacterFormat.FontSize = 12
OH.CharacterFormat.Bold = true
OH.CharacterFormat.TextColor = Color.Blue
Dim P4 As Paragraph = section.HeadersFooters.EvenHeader.AddParagraph
Dim EH As TextRange = P4.AppendText("偶数页页眉")
P4.Format.HorizontalAlignment = HorizontalAlignment.Right
EH.CharacterFormat.FontName = "Calibri"
EH.CharacterFormat.FontSize = 12
EH.CharacterFormat.Bold = true
EH.CharacterFormat.TextColor = Color.Green
'保存文档
document.SaveToFile("result.docx", FileFormat.Docx2010)
设置效果: