本教程详细介绍如何使用spire.pdf for java在新建pdf文档时添加页眉页脚。
import java.awt.*;
import java.awt.geom.dimension2d;
import com.spire.pdf.*;
import com.spire.pdf.automaticfields.pdfautomaticfield;
import com.spire.pdf.automaticfields.pdfcompositefield;
import com.spire.pdf.automaticfields.pdfpagecountfield;
import com.spire.pdf.automaticfields.pdfpagenumberfield;
import com.spire.pdf.graphics.*;
public class headerfooter {
public static void main(string[] args) throws exception {
//创建 pdfdocument 对象
pdfdocument doc = new pdfdocument();
//创建pdfmargins对象, 并设置的页边距
pdfmargins margin = new pdfmargins(60,60,40,40);
//调用 addheaderandfooter()方法添加页眉页脚
addheaderandfooter(doc, pdfpagesize.a4, margin);
//在文档中添加两页并写入文字
pdfpagebase page1 = doc.getpages().add();
pdfpagebase page2 = doc.getpages().add();
pdftruetypefont font = new pdftruetypefont(new font("arial unicode ms",font.plain,14),true);
string text1 = "spire.pdf 示例";
string text2 = "添加pdf页眉页脚";
page1.getcanvas().drawstring(text1, font, pdfbrushes.getblack(),0,0);
page2.getcanvas().drawstring(text2, font, pdfbrushes.getblack(),0,0);
//保存文档
doc.savetofile("output/headerfooter.pdf");
doc.close();
}
static void addheaderandfooter(pdfdocument doc, dimension2d pagesize, pdfmargins margin) {
pdfpagetemplateelement header = new pdfpagetemplateelement(margin.getleft(), pagesize.getheight());
doc.gettemplate().setleft(header);
pdfpagetemplateelement topspace = new pdfpagetemplateelement(pagesize.getwidth(), margin.gettop());
topspace.setforeground(true);
doc.gettemplate().settop(topspace);
//添加页眉
pdftruetypefont font= new pdftruetypefont(new font("arial unicode ms",font.plain,10),true);
pdfstringformat format = new pdfstringformat(pdftextalignment.left);
string label = "成都冰蓝科技有限公司";
dimension2d dimension2d = new dimension();
dimension2d.setsize(font.measurestring(label, format));
float y = topspace.getheight() - font.getheight() - 1;
pdfpen pen = new pdfpen(new pdfrgbcolor(color.black), 0.75f);
topspace.getgraphics().settransparency(0.5f);
topspace.getgraphics().drawline(pen, margin.getleft(), y, pagesize.getwidth() - margin.getright(), y);
y = y - 1 - (float) dimension2d.getheight();
topspace.getgraphics().drawstring(label, font, pdfbrushes.getblack(), margin.getleft(), y, format);
pdfpagetemplateelement rightspace = new pdfpagetemplateelement(margin.getright(), pagesize.getheight());
doc.gettemplate().setright(rightspace);
//添加显示当前页及总页数的域作为页脚
pdfpagetemplateelement footer = new pdfpagetemplateelement(pagesize.getwidth(), margin.getbottom());
footer.setforeground(true);
doc.gettemplate().setbottom(footer);
y = font.getheight() 1;
footer.getgraphics().settransparency(0.5f);
footer.getgraphics().drawline(pen, margin.getleft(), y, pagesize.getwidth() - margin.getright(), y);
y = y 1;
pdfpagenumberfield pagenumber = new pdfpagenumberfield();
pdfpagecountfield pagecount = new pdfpagecountfield();
pdfcompositefield pagenumberlabel = new pdfcompositefield();
pagenumberlabel.setautomaticfields(new pdfautomaticfield[]{pagenumber, pagecount});
pagenumberlabel.setbrush(pdfbrushes.getblack());
pagenumberlabel.setfont(font);
format = new pdfstringformat(pdftextalignment.right);
pagenumberlabel.setstringformat(format);
pagenumberlabel.settext("第{0}页共{1}页");
pagenumberlabel.setbounds(footer.getbounds());
pagenumberlabel.draw(footer.getgraphics(), - margin.getleft(), y);
}
}
添加的pdf页眉页脚示例图: