有哪位大侠做过itextpdf 添加页眉的pdf开发,设置页眉和页脚的时候的是图片。现在弄不上图片,急

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&Posts - 2,
Articles - 0,
Comments - 0
14:06 by JoannLiu, ... 阅读,
itextpdf html转pdf 添加页码和水印,需要在Document打开前给PdfWriter添加一个pageEvent
1 import java.io.ByteArrayInputS
2 import java.io.FileOutputS
3 import java.io.StringW
4 import java.nio.charset.C
6 import com.itextpdf.text.D
7 import com.itextpdf.text.pdf.PdfW
8 import com.itextpdf.tool.xml.XMLWorkerH
9 import com.jfinal.log.L
10 import com.jfinal.render.FreeMarkerR
12 import freemarker.template.T
14 public class PdfKit {
private static final Logger log = Logger.getLogger(PdfKit.class);
* @param templateView-freemarker模板相对路径
* @param os-输出流
* @param model-模板所需要的数据
* @throws Exception
public static void export(String templateView,Object model,String temp) throws Exception {
("生成的合同文件地址:"+temp);
FileOutputStream tempOs = new FileOutputStream(temp);
Template template = FreeMarkerRender.getConfiguration().getTemplate(templateView);
StringWriter result = new StringWriter();
template.process(model, result);
String htmlData = result.toString();//模版变量
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, tempOs);
//添加水印和页码
PDFBuilder builder = new PDFBuilder();
writer.setPageEvent(builder);
document.open();
XMLWorkerHelper.getInstance().parseXHtml(writer, document,
new ByteArrayInputStream(htmlData.getBytes()), Charset.forName("UTF-8"));
document.close();
PDFBuilder代码
此部分代码来自于
1 package com.sign.system.
3 import java.io.IOE
5 import com.itextpdf.text.D
6 import com.itextpdf.text.DocumentE
7 import com.itextpdf.text.E
8 import com.itextpdf.text.F
9 import com.itextpdf.text.I
10 import com.itextpdf.text.PageS
11 import com.itextpdf.text.P
12 import com.itextpdf.text.R
13 import com.itextpdf.text.pdf.BaseF
14 import com.itextpdf.text.pdf.ColumnT
15 import com.itextpdf.text.pdf.PdfContentB
16 import com.itextpdf.text.pdf.PdfPageEventH
17 import com.itextpdf.text.pdf.PdfT
18 import com.itextpdf.text.pdf.PdfW
19 import com.sign.system.PathC
20 import com.sign.util.PathU
* 设置页面附加属性
26 public class PDFBuilder extends PdfPageEventHelper {
public String header = "";
* 文档字体大小,页脚页眉最好和文本大小一致
public int presentFontSize = 12;
* 文档页面大小,最好前面传入,否则默认为A4纸张
public Rectangle pageSize = PageSize.A4;
public PdfT
// 基础字体对象
public BaseFont bf = null;
// 利用基础字体生成的字体对象,一般用于生成中文文字
public Font fontDetail = null;
* Creates a new instance of PdfReportM1HeaderFooter 无参构造方法.
public PDFBuilder() {
* Creates a new instance of PdfReportM1HeaderFooter 构造方法.
* @param yeMei
页眉字符串
* @param presentFontSize
数据体字体大小
* @param pageSize
页面文档大小,A4,A5,A6横转翻转等Rectangle对象
public PDFBuilder(String yeMei, int presentFontSize, Rectangle pageSize) {
this.header = yeM
this.presentFontSize = presentFontS
this.pageSize = pageS
public void setHeader(String header) {
this.header =
public void setPresentFontSize(int presentFontSize) {
this.presentFontSize = presentFontS
* TODO 文档打开时创建模板
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument(com.itextpdf.text.pdf.PdfWriter,
com.itextpdf.text.Document)
public void onOpenDocument(PdfWriter writer, Document document) {
total = writer.getDirectContent().createTemplate(50, 50);// 共 页 的矩形的长宽高
* TODO 关闭每页的时候,写入页眉,写入'第几页共'这几个字。
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(com.itextpdf.text.pdf.PdfWriter,
com.itextpdf.text.Document)
public void onEndPage(PdfWriter writer, Document document) {
this.addPage(writer, document);
this.addWatermark(writer);
public void addPage(PdfWriter writer, Document document){
if (bf == null) {
bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
if (fontDetail == null) {
fontDetail = new Font(bf, presentFontSize, Font.NORMAL);// 数据体字体
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
// 1.写入页眉
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_LEFT, new Phrase(header, fontDetail),
document.left(), document.top() + 20, 0);
// 2.写入前半部分的 第 X页/共
int pageS = writer.getPageNumber();
String foot1 = "第 " + pageS + " 页 /共";
Phrase footer = new Phrase(foot1, fontDetail);
// 3.计算前半部分的foot1的长度,后面好定位最后一部分的'Y页'这俩字的x轴坐标,字体长度也要计算进去 = len
float len = bf.getWidthPoint(foot1, presentFontSize);
// 4.拿到当前的PdfContentByte
PdfContentByte cb = writer.getDirectContent();
// 5.写入页脚1,x轴就是(右margin+左margin + right() -left()- len)/2.0F
// 再给偏移20F适合人类视觉感受,否则肉眼看上去就太偏左了
// ,y轴就是底边界-20,否则就贴边重叠到数据体里了就不是页脚了;注意Y轴是从下往上累加的,最上方的Top值是大于Bottom好几百开外的。
ColumnText
.showTextAligned(
Element.ALIGN_CENTER,
(document.rightMargin() + document.right()
+ document.leftMargin() - document.left() - len) / 2.0F + 20F,
document.bottom() - 20, 0);
// 6.写入页脚2的模板(就是页脚的Y页这俩字)添加到文档中,计算模板的和Y轴,X=(右边界-左边界 - 前半部分的len值)/2.0F +
// len , y 轴和之前的保持一致,底边界-20
cb.addTemplate(total, (document.rightMargin() + document.right()
+ document.leftMargin() - document.left()) / 2.0F + 20F,
document.bottom() - 20); // 调节模版显示的位置
public void addWatermark(PdfWriter writer){
// 水印图片
image = Image.getInstance(PathUtil.getRootClassPath(PathCons.FILE_PDF_IMG));
PdfContentByte content = writer.getDirectContentUnder();
content.beginText();
// 开始写入水印
for(int k=0;k&5;k++){
for (int j = 0; j &4; j++) {
image.setAbsolutePosition(150*j,170*k);
content.addImage(image);
content.endText();
} catch (IOException | DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
* TODO 关闭文档时,替换模板,完成整个页眉页脚组件
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onCloseDocument(com.itextpdf.text.pdf.PdfWriter,
com.itextpdf.text.Document)
public void onCloseDocument(PdfWriter writer, Document document) {
// 7.最后一步了,就是关闭文档的时候,将模板替换成实际的 Y 值,至此,page x of y 制作完毕,完美兼容各种文档size。
total.beginText();
total.setFontAndSize(bf, presentFontSize);// 生成的模版的字体、颜色
String foot2 = " " + (writer.getPageNumber()-1) + " 页";
total.showText(foot2);// 模版显示的内容
total.endText();
total.closePath();我要用iText打印PDF文档,但是页眉页脚不知道怎么添加图片,请各位大虾指教_百度知道
我要用iText打印PDF文档,但是页眉页脚不知道怎么添加图片,请各位大虾指教
PDF文件貌似要用Adobe Acrobat这软件才能编辑吧
楼上的那位没弄懂我得意思,不过还是谢谢他的回答!
HeaderFooter footer = new HeaderFooter(new Phrase(&-&),true);
footer.setBorder(Rectangle.NO_BORDER);
footer.setBorder(Rectangle.TOP);
footer.setBorderColor(Color.black);
   footer.setAlignment(2);
   document.setFooter(footer);
document.open();这是我设置页脚的代码,我现在想往里面添加图片怎么搞?
其他类似问题
1人觉得有用
为您推荐:
页眉页脚的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁jfinal(6)
新建class:
import java.io.IOE
import com.itextpdf.text.D
import com.itextpdf.text.DocumentE
import com.itextpdf.text.pdf.BaseF
import com.itextpdf.text.pdf.PdfContentB
import com.itextpdf.text.pdf.PdfPageEventH
import com.itextpdf.text.pdf.PdfT
import com.itextpdf.text.pdf.PdfW
&& & * 设置打印单据的头部和底部信息
&& & * @author
public&& &class HeadFootInfoPdfPageEvent extends PdfPageEventHelper {
&& &&& &//自定义传参数
&& &&& &public String pdfN//出入库名称
&& &&& &public S//日期
&& &&& &public S//单号类型
&& &&& &public S//单号
&& &&& &public PdfT
&& &&& &BaseFont bfC
&& &&& &//无参构造方法
&& &&& &public HeadFootInfoPdfPageEvent() {
&& &&& &&& &super();
&& &&& &//有参构造方法
&& &&& &public HeadFootInfoPdfPageEvent(String PdfName,String Date,String Type,String Code) {
&& &&& &&& &super();
&& &&& &&& &this.pdfName=PdfN
&& &&& &&& &this.date = D
&& &&& &&& &this.type=T
&& &&& &&& &this.code = C
&& &&& &&& &try {
&& &&& &&& &&& &bfChinese = BaseFont.createFont(&C:/WINDOWS/Fonts/SIMSUN.TTC,1&, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
&& &&& &&& &} catch (DocumentException e) {
&& &&& &&& &&& &// TODO Auto-generated catch block
&& &&& &&& &&& &e.printStackTrace();
&& &&& &&& &} catch (IOException e) {
&& &&& &&& &&& &// TODO Auto-generated catch block
&& &&& &&& &&& &e.printStackTrace();
&& &&& &&& &}
&& &&& &public void onOpenDocument(PdfWriter writer, Document document) {
&& &&& &&& &tpl = writer.getDirectContent().createTemplate(100, 20);
&& &&& &//实现页眉和页脚的方法
&& &&& &public void onEndPage(PdfWriter writer, Document document) {
&& &&& &&& &try {
&& &&& &&& &&& &String[] riqi = date.split(&-&);
&& &&& &&& &&& &PdfContentByte headAndFootPdfContent = writer.getDirectContent();
&& &&& &&& &&& &headAndFootPdfContent.saveState();
&& &&& &&& &&& &headAndFootPdfContent.beginText();
&& &&& &&& &&& &//设置中文
&& &&& &&& &&& &headAndFootPdfContent.setFontAndSize(bfChinese, 12);
&& &&& &&& &&& &//文档页头信息设置 &
&& &&& &&& &&& &float x = document.top(-20);
&& &&& &&& &&& &float x1 = document.top(-5);
&& &&& &&& &&& &//页头信息中间 &
&& &&& &&& &&& &headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_CENTER, pdfName, (document.right() &#43; document.left()) / 2, x, 0);
&& &&& &&& &&& &//页头信息左面 &
&& &&& &&& &&& &headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_LEFT, riqi[0] &#43; &年& &#43; riqi[1] &#43; &月& &#43; riqi[2] &#43; &日&,
&& &&& &&& &&& &&& &&& &document.left() &#43; 100, x1, 0);
&& &&& &&& &&& &//页头信息中间 &
&& &&& &&& &&& &headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_CENTER, type&#43;&库单号:& &#43;code&#43; &&,
&& &&& &&& &&& &&& &&& &(document.right() &#43; document.left()) / 2, x1, 0);
&& &&& &&& &&& &//页头信息右面 &
&& &&& &&& &&& &headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_RIGHT, & 单位:册&, document.right() - 100, x1, 0);
&& &&& &&& &&& &//文档页脚信息设置 &
&& &&& &&& &&& &float y = document.bottom(-20);
&& &&& &&& &&& &float y1 = document.bottom(-35);
&& &&& &&& &&& &//页脚信息左面 &
&& &&& &&& &&& &headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_LEFT, &储运部负责人:&, document.left() &#43; 100, y, 0);
&& &&& &&& &&& &//页脚信息中间 &
&& &&& &&& &&& &headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_CENTER, &&& 库管员:&&& &, (document.right() &#43; document.left()) / 2, y, 0);
&& &&& &&& &&& &//页脚信息右面 &
&& &&& &&& &&& &headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_RIGHT, & 经手人:&, document.right() - 100, y, 0);
&& &&& &&& &&& &//添加页码
&& &&& &&& &&& &//页脚信息中间 &
&& &&& &&& &&& &headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_CENTER, &--第& &#43; document.getPageNumber(),
&& &&& &&& &&& &&& &&& &(document.right() &#43; document.left()) / 2, y1, 0);
&& &&& &&& &&& &//在每页结束的时候把“第x页”信息写道模版指定位置 &
&& &&& &&& &&& &headAndFootPdfContent.addTemplate(tpl, (document.right() &#43; document.left()) / 2 &#43; 15, y1);//定位“y页” 在具体的页面调试时候需要更改这xy的坐标 &
&& &&& &&& &&& &headAndFootPdfContent.endText();
&& &&& &&& &&& &headAndFootPdfContent.restoreState();
&& &&& &&& &} catch (Exception de) {
&& &&& &&& &&& &de.printStackTrace();
&& &&& &&& &}
&& &&& &public void onCloseDocument(PdfWriter writer, Document document) {
&& &&& &&& &//关闭document的时候获取总页数,并把总页数按模版写道之前预留的位置 &
&& &&& &&& &tpl.beginText();
&& &&& &&& &tpl.setFontAndSize(bfChinese, 12);
&& &&& &&& &tpl.showText(&页,共& &#43; Integer.toString(writer.getPageNumber() - 1) &#43; &页--&);
&& &&& &&& &tpl.endText();
&& &&& &&& &tpl.closePath();//sanityCheck(); &
方法调用如下:下document的open之前写:
//设置pdf每页的页眉和页脚
&& &&& &&& &writer.setPageEvent(new HeadFootInfoPdfPageEvent(&页眉的名称&,stolist.get(0).getDate(&stsu_data&).toString(),&入&,stsu.getStr(&stsu_code&)));
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:4888次
排名:千里之外
原创:37篇
(2)(6)(1)(4)(3)(3)(1)(1)(5)(11)}

我要回帖

更多关于 itext pdf 页眉页脚 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信