📄 casepageevent.java
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Created on 2006/8/31
*
* @Author: Xiaojun Chen
* $Revision$ 1.0
*
*/
package eti.bi.alphaminer.tools.ExportCase;
import java.awt.Color;
import java.io.IOException;
import com.lowagie.text.Chunk;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Document;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDestination;
import com.lowagie.text.pdf.PdfGState;
import com.lowagie.text.pdf.PdfOutline;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPageEvent;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
import eti.bi.common.ImageLocation;
import eti.bi.common.System.SysConfig;
import eti.bi.util.ResourceLoader;
class CasePageEvent implements PdfPageEvent {
/** The headertable. */
public PdfPTable table;
/** The Graphic state */
public PdfGState gstate;
/** A template that will hold the total number of pages. */
public PdfTemplate tpl;
/** Alphaminer logo */
private Image alphaminerLogo;
private BaseFont bf;
private String alphaminerText;
private float text_markx;
private float text_marky;
private float headtabley;
private float logoheight=30;
private float pageoutwidth=20;
public CasePageEvent() throws DocumentException, IOException {
alphaminerText = "AlphaMiner(Version " + SysConfig.getAlphaminerVersion() + ")";
// bottomText = "Powered by HIT-HKU BI Lab(2006).";
alphaminerLogo = Image.getInstance(ResourceLoader.getImagePath(ImageLocation.APP_ICON));
alphaminerLogo.scaleAbsolute(logoheight, logoheight);
bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
}
public void onOpenDocument(PdfWriter writer, Document document) {
try {
table = new PdfPTable(2);
Phrase p = new Phrase();
Chunk ck = new Chunk("http://bi.hitsz.edu.cn/AlphaMiner/index.htm\n", new Font(Font.TIMES_ROMAN, 13,
Font.BOLDITALIC, Color.blue));
ck.setAnchor("http://bi.hitsz.edu.cn/AlphaMiner/index.htm");
p.add(ck);
ck = new Chunk("HIT-HKU Business Intelligence Lab", new Font(Font.TIMES_ROMAN, 12, Font.BOLD, Color.BLUE));
ck.setAnchor("http://bi.hitsz.edu.cn");
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(alphaminerLogo, 0, 0)));
// initialization of the Graphic State
gstate = new PdfGState();
gstate.setFillOpacity(0.2f);
gstate.setStrokeOpacity(0.1f);
// initialization of the template
tpl = writer.getDirectContent().createTemplate(100, 100);
tpl.setBoundingBox(new Rectangle(-20, -20, 100, 100));
Rectangle page = writer.getPageSize();
text_markx = page.width() - document.rightMargin() - bf.getWidthPoint("Xiaojun Chen", 20)/2-10;
text_marky = document.bottomMargin() + 30;
headtabley = page.height()+logoheight-document.topMargin();
if(headtabley<pageoutwidth) {
headtabley = pageoutwidth;
}
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
public void onStartPage(PdfWriter writer, Document document) {
}
public void onEndPage(PdfWriter writer, Document document) {
if (document.getPageNumber() <3) {
return;
}
try {
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
// write the headertable
table.setTotalWidth(document.right() - document.left());
table.writeSelectedRows(0, -1, document.left(), headtabley, cb);
//draw line
/*
cb.setColorStroke(Color.RED);
cb.setLineWidth(2);
cb.moveTo(documentleft, lineheight);
cb.lineTo(focumentright, lineheight);
cb.stroke();
cb.saveState();
*/
// compose the footer
String text = "Page " + (writer.getPageNumber() - 2) + " of ";
float textSize = bf.getWidthPoint(text, 12);
float textBase = document.bottom() - 20;
cb.beginText();
cb.setFontAndSize(bf, 12);
// show the footer at the left
float adjust = bf.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.orange);
cb.setLineWidth(2);
cb.rectangle(pageoutwidth, pageoutwidth, document.getPageSize().width() - 2*pageoutwidth, document.getPageSize().height() - 2*pageoutwidth);
cb.stroke();
cb.restoreState();
// starting on page 3, a watermark with an Image that is made
// transparent
if (writer.getPageNumber() >= 5) {
cb.setGState(gstate);
cb.setColorFill(Color.red);
cb.beginText();
cb.setFontAndSize(bf, 50);
if (writer.getPageNumber() % 2 == 1) {
cb.showTextAligned(Element.ALIGN_CENTER, alphaminerText, document.getPageSize().width() / 2,
document.getPageSize().height() / 2, 45);
} else {
cb.showTextAligned(Element.ALIGN_CENTER, alphaminerText, document.getPageSize().width() / 2,
document.getPageSize().height() / 2, -45);
}
cb.setFontAndSize(bf, 20);
try {
cb.showTextAligned(Element.ALIGN_CENTER, "Xiaojun Chen", text_markx, text_marky, 0);
} catch (Exception e) {
throw new ExceptionConverter(e);
}
cb.endText();
cb.restoreState();
}
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
public void onCloseDocument(PdfWriter writer, Document arg1) {
tpl.beginText();
tpl.setFontAndSize(bf, 12);
tpl.setTextMatrix(0, 0);
tpl.showText("" + (writer.getPageNumber() - 3));
tpl.endText();
}
public void onParagraph(PdfWriter writer, Document document, float position) {
String bookMarkTitle = CaseDocument.BookMark();
if (bookMarkTitle == null) {
return;
}
PdfContentByte cb = writer.getDirectContent();
PdfDestination destination = new PdfDestination(PdfDestination.FITH, position);
PdfOutline outline = new PdfOutline(cb.getRootOutline(), destination, bookMarkTitle);
outline.setColor(Color.blue);
}
public void onParagraphEnd(PdfWriter arg0, Document arg1, float arg2) {
// TODO Auto-generated method stub
}
public void onChapter(PdfWriter arg0, Document arg1, float arg2, Paragraph arg3) {
// TODO Auto-generated method stub
}
public void onChapterEnd(PdfWriter arg0, Document arg1, float arg2) {
// TODO Auto-generated method stub
}
public void onSection(PdfWriter arg0, Document arg1, float arg2, int arg3, Paragraph arg4) {
// TODO Auto-generated method stub
}
public void onSectionEnd(PdfWriter arg0, Document arg1, float arg2) {
// TODO Auto-generated method stub
}
public void onGenericTag(PdfWriter arg0, Document arg1, Rectangle arg2, String arg3) {
// TODO Auto-generated method stub
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -