关闭

iText输出PDF翻页控制简单例子

462人阅读 评论(0) 收藏 举报
 

HTML Tags and JavaScript tutorial



iText输出PDF翻页控制简单例子





翻页事件处理
最为实用的就数这个了,因为对应报表之类的打印几页是常有的事,
同时又希望把标题,页码之类的在每一页都出现,于是pageEvent出
场了。因为翻页事件是不可控制的,只有出现了翻页才调用此事件,
所以我们能做的就是允许页面事件,并继承页面事件,重新写一个类。
在PdfPageEvent.class里面有好几个方法,分别在不同状态下调用,
对应翻页我们只要覆写其中几个就可以了。
public void onOpenDocument(PdfWriter writer, Document document);
public void onStartPage(PdfWriter writer, Document document);
public void onEndPage(PdfWriter writer, Document document);
public void onCloseDocument(PdfWriter writer, Document document);
还有一些是处理章节的,在最简单的情况下我们要覆写onEndPage 就够了,
就在每一页结束时加些东西上去,明白的说就是加页眉页脚,或插图等等。
现在用下载网站提供的一段代码,看看怎么操作先。
package com.lowagie.tool;
import com.lowagie.text.pdf.*;
import com.lowagie.text.*;
import java.awt.Color;
public class MyEvent extends PdfPageEventHelper {
public Image headerImage;
/** The headertable. */
public PdfPTable table;
/** The Graphic state */
public PdfGState gstate;
/** A template that will hold the total number of pages. */
public PdfTemplate tpl;
/** The font that will be used. */
public BaseFont helv;
public void onOpenDocument(PdfWriter writer, Document document) {
try {
// initialization of the header table
headerImage = Image.getInstance("F://tempJava//JBuilder//Apple//pdf//logo.gif");
table = new PdfPTable(2);
Phrase p = new Phrase();
Chunk ck = new Chunk("leniz/n",
new Font(Font.TIMES_ROMAN,
16,
Font.BOLDITALIC,
Color.blue));
p.add(ck);
ck = new Chunk("author",
new Font(Font.HELVETICA,
12,
Font.NORMAL,
Color.darkGray));
p.add(ck);
table.getDefaultCell().setBackgroundColor(Color.yellow);
table.getDefaultCell().setBorderWidth(0);
table.addCell(p);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(new Phrase(new Chunk(headerImage, 0, 0)));
// initialization of the Graphic State
gstate = new PdfGState();
gstate.setFillOpacity(0.3f);
gstate.setStrokeOpacity(0.3f);
// initialization of the template
tpl = writer.getDirectContent().createTemplate(100, 100);
tpl.setBoundingBox(new Rectangle(-20, -20, 100, 100));
// initialization of the font
helv = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false);
}
catch(Exception e) {
throw new ExceptionConverter(e);
}
}
public void onEndPage(PdfWriter writer, Document document) {
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
// write the headertable
table.setTotalWidth(document.right() - document.left());
table.writeSelectedRows(0, -1, document.left(),
document.getPageSize().height() - 50, cb);
// compose the footer
String text = "Page " + writer.getPageNumber() + " of ";
float textSize = helv.getWidthPoint(text, 12);
float textBase = document.bottom() - 20;
cb.beginText();
cb.setFontAndSize(helv, 12);
// for odd pagenumbers, show the footer at the left
if ((writer.getPageNumber() & 1) == 1) {
cb.setTextMatrix(document.left(), textBase);
cb.showText(text);
cb.endText();
cb.addTemplate(tpl, document.left() + textSize, textBase);
}
else {
float adjust = helv.getWidthPoint("0", 12);
cb.setTextMatrix(document.right() - textSize - adjust, textBase);
cb.showText(text);
cb.endText();
cb.addTemplate(tpl, document.right() - adjust, textBase);
}
cb.saveState();
// draw a Rectangle around the page
cb.setColorStroke(Color.red);
cb.setLineWidth(2);
cb.rectangle(20, 20, document.getPageSize().width() - 40,
document.getPageSize().height() - 40);
cb.stroke();
cb.restoreState();
// starting on page 3, a watermark with an Image that is made transparent
if (writer.getPageNumber() >= 3) {
cb.setGState(gstate);
cb.setColorFill(Color.red);
cb.beginText();
cb.setFontAndSize(helv, 48);
cb.showTextAligned(Element.ALIGN_CENTER,
"Watermark Opacity " + writer.getPageNumber(),
document.getPageSize().width() / 2,
document.getPageSize().height() / 2, 45);
cb.endText();
try {
cb.addImage(headerImage, headerImage.width(),
0,
0,
headerImage.height(), 440, 80);
}
catch(Exception e) {
throw new ExceptionConverter(e);
}
cb.restoreState();
}
}
public void onStartPage(PdfWriter writer, Document document) {
if (writer.getPageNumber() < 3) {
PdfContentByte cb = writer.getDirectContentUnder();
cb.saveState();
cb.setColorFill(Color.pink);
cb.beginText();
cb.setFontAndSize(helv, 48);
cb.showTextAligned(Element.ALIGN_CENTER,
"My Watermark Under " + writer.getPageNumber(),
document.getPageSize().width() / 2,
document.getPageSize().height() / 2, 45);
cb.endText();
cb.restoreState();
}
}
public void onCloseDocument(PdfWriter writer,
Document document) {
tpl.beginText();
tpl.setFontAndSize(helv, 12);
tpl.setTextMatrix(0, 0);
tpl.showText("" + (writer.getPageNumber() - 1));
tpl.endText();
}
一般可以用到的东西,都这这个例子里出现了,所以说这个例子太经典,都让人舍不得删掉一点点,
要不是这个例子,我想,很多东西自己还懵懵的。 


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:150639次
    • 积分:4030
    • 等级:
    • 排名:第7724名
    • 原创:242篇
    • 转载:0篇
    • 译文:0篇
    • 评论:16条
    文章存档
    最新评论