本文将介绍通过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)
设置效果: