本⽂中将介绍C# 操作Word页眉页脚的⽅法,包含如下要点内容:
●设置Word奇偶页页眉页脚不同
●设置Word⾸页页眉页脚不同
●不连续设置页码(即对不同章节的内容设置不同页码)
●删除页眉页脚
使⽤⼯具:Free Spire.Doc for .NET(社区版)
注:编程时注意在相应程序中添加引⽤Spire.Doc.dll,dll⽂件可在安装路径下的Bin⽂件夹中获取。
【⽰例1】设置Word奇偶页页眉页脚不同
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;页码怎么删除
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace HeadersFootersForOddAndEvenPages
{
class Program
{
static void Main(string[] args)
{
//创建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); System.Diagnostics.Process.Start("result.docx");
}
}
}
奇偶页页眉页脚不同设置效果:
【⽰例2】设置Word⾸页页眉页脚不同
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace HeaderFootersDifferentFromFirstPage
{
class Program
{
static void Main(string[] args)
{
//创建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);
System.Diagnostics.Process.Start("result.docx");
}
}
}
⾸页页眉页脚不同设置效果:
【⽰例3】不连续设置页码
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace DifferentPageNumber_Doc
发布评论