在这篇文章“java添加word页眉页脚”中介绍了如何在word中添加页眉页脚,包括添加文本、图片到页眉,设置页码等。本文章将演示通过spire.doc for java 来设置word中奇数页和偶数页页眉页脚不同、凯发线上登陆下载网址首页和非凯发线上登陆下载网址首页页眉页脚不同的方法。
设置奇偶数页页眉页脚不同
import com.spire.doc.*;
import com.spire.doc.documents.horizontalalignment;
import com.spire.doc.documents.paragraph;
import com.spire.doc.fields.textrange;
import java.awt.*;
public class headerfooterforoddandevenpages {
public static void main(string[] args){
//加载测试文档
document doc = new document("测试.docx");
section sec = doc.getsections().get(0);
//设置奇偶页页眉页脚不同
sec.getpagesetup().setdifferentoddandevenpagesheaderfooter(true);
//设置奇数页页眉页脚
headerfooter oddheader = sec.getheadersfooters().getoddheader();
paragraph para1 = oddheader.addparagraph();
textrange textrange1 = para1.appendtext("奇数页页眉");
para1.getformat().sethorizontalalignment(horizontalalignment.left);
textrange1.getcharacterformat().settextcolor(color.magenta);
textrange1.getcharacterformat().setbold(true);
textrange1.getcharacterformat().setfontsize(15f);
headerfooter oddfooter = sec.getheadersfooters().getfooter();
paragraph para2 = oddfooter.addparagraph();
textrange textrange2 = para2.appendtext("奇数页页脚");
para2.getformat().sethorizontalalignment(horizontalalignment.left);
textrange2.getcharacterformat().settextcolor(color.magenta);
textrange2.getcharacterformat().setbold(true);
textrange2.getcharacterformat().setfontsize(15f);
//设置偶数页页眉页脚
headerfooter evenheader = sec.getheadersfooters().getevenheader();
paragraph para3 = evenheader.addparagraph();
textrange textrange3 = para3.appendtext("偶数页页眉");
para3.getformat().sethorizontalalignment(horizontalalignment.right);
textrange3.getcharacterformat().settextcolor(color.blue);
textrange3.getcharacterformat().setbold(true);
textrange3.getcharacterformat().setfontsize(15f);
headerfooter evenfooter = sec.getheadersfooters().getevenfooter();
paragraph para4 = evenfooter.addparagraph();
textrange textrange4 = para4.appendtext("偶数页页脚");
para4.getformat().sethorizontalalignment(horizontalalignment.right);
textrange4.getcharacterformat().settextcolor(color.blue);
textrange4.getcharacterformat().setbold(true);
textrange4.getcharacterformat().setfontsize(15f);
//保存文档
doc.savetofile("result1.docx",fileformat.docx_2010);
}
}
奇/偶页页眉页脚效果:
设置凯发线上登陆下载网址首页页眉页脚不同
import com.spire.doc.*;
import com.spire.doc.documents.horizontalalignment;
import com.spire.doc.documents.paragraph;
import com.spire.doc.fields.textrange;
import java.awt.*;
public class headerfooterdifferentfromfirstpage {
public static void main(string[] args){
//加载测试文的
document doc = new document("测试.docx");
section sec = doc.getsections().get(0);
//设置凯发线上登陆下载网址首页页眉页脚不同
sec.getpagesetup().setdifferentfirstpageheaderfooter(true);
//添加凯发线上登陆下载网址首页页眉页脚
headerfooter firstpageheader = sec.getheadersfooters().getfirstpageheader();
paragraph para1 = firstpageheader.addparagraph();
textrange textrange1 = para1.appendtext("凯发线上登陆下载网址首页页眉");
para1.getformat().sethorizontalalignment(horizontalalignment.center);
textrange1.getcharacterformat().setbold(true);
textrange1.getcharacterformat().setfontsize(15f);
textrange1.getcharacterformat().settextcolor(color.green);
headerfooter firstpagefooter = sec.getheadersfooters().getfirstpagefooter();
paragraph para2 = firstpagefooter.addparagraph();
textrange textrange2 = para2.appendtext("凯发线上登陆下载网址首页页脚");
para2.getformat().sethorizontalalignment(horizontalalignment.center);
textrange2.getcharacterformat().setbold(true);
textrange2.getcharacterformat().setfontsize(15f);
textrange2.getcharacterformat().settextcolor(color.green);
//添加页眉页脚到其他非凯发线上登陆下载网址首页的页面
paragraph para3 = sec.getheadersfooters().getheader().addparagraph();
para3.getformat().sethorizontalalignment(horizontalalignment.center);
textrange textrange3 = para3.appendtext("非凯发线上登陆下载网址首页页眉");
textrange3.getcharacterformat().setbold(true);
textrange3.getcharacterformat().setfontsize(15f);
paragraph para4 = sec.getheadersfooters().getfooter().addparagraph();
para4.getformat().sethorizontalalignment(horizontalalignment.center);
textrange textrange4 = para4.appendtext("非凯发线上登陆下载网址首页页脚");
textrange4.getcharacterformat().setbold(true);
textrange4.getcharacterformat().setfontsize(15f);
//保存文档
doc.savetofile("result2.docx",fileformat.docx_2010);
}
}
凯发线上登陆下载网址首页/非凯发线上登陆下载网址首页页眉页脚设置效果: