I used the PageXofY.java example to create a
PdfPageEventHelper class. It works good for the page
number and a "DEMO" watermark. I am generating this
PDF from a servlet.
There is 1 more feature that I need, for certain pages
I need to add an additional watermark("ASSUMES
SURRENDER"). At the time the page is generated I know
if it needs this watermark. What I want to do is add
the watermark in the onEndPage method, so that method
needs to know when to add the watermark. I tried to
"re-use" the document property Html Style Class. I
would set it to a particular value in the servlet,
then check for this value in the onEndPage method.
But the value is always null in onEndPage method. Is
there any other technique for doing this? Is there
something I need to do to get the onEndPage method to
see my "servlet updated" value of the Html Style
Class.
____________________________________________________________________________________
Got a little couch potato?
Check out fun summer activities for kids.
http://search.yahoo.com/search?fr=oni_on_mail&p=summer+activities+for+kids&cs=bz
package com.onfs.web.portlet.funds;
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.log4j.Logger;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.Image;
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.PdfGState;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
import com.onfs.web.Props;
import com.onfs.web.portlet.funds.ItextUtils;
/**
* This example was written by Bruno Lowagie. It is part of the book 'iText in
* Action' by Manning Publications.
* ISBN: 1932394796
* http://itext.ugent.be/itext-in-action/
* http://www.manning.com/lowagie/
*/
public class PageXofY extends PdfPageEventHelper {
private static Logger log = Logger.getLogger(PageXofY.class);
/** The PdfTemplate that contains the total number of pages. */
protected PdfTemplate total;
/** The font that will be used. */
protected BaseFont helv;
/** The Graphics State for the watermark. */
protected PdfGState gstate;
/**
* @see
com.lowagie.text.pdf.PdfPageEvent#onOpenDocument(com.lowagie.text.pdf.PdfWriter,
* com.lowagie.text.Document)
*/
public void onOpenDocument(PdfWriter writer, Document document) {
log.debug("In onOpenDocument...");
total = writer.getDirectContent().createTemplate(20, 10);
total.setBoundingBox(new Rectangle(-10, -10, 20, 10));
try {
helv = BaseFont.createFont(BaseFont.HELVETICA_BOLD,
BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
} catch (Exception e) {
throw new ExceptionConverter(e);
}
gstate = new PdfGState();
gstate.setFillOpacity(0.3f);
gstate.setStrokeOpacity(0.3f);
}
/**
* @see
com.lowagie.text.pdf.PdfPageEvent#onEndPage(com.lowagie.text.pdf.PdfWriter,
* com.lowagie.text.Document)
*/
public void onEndPage(PdfWriter writer, Document document) {
log.debug("In onEndPage, page = " + document.getPageNumber());
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
String text = "Page " + writer.getPageNumber() + " of ";
float textSize1 = helv.getWidthPoint(text, 6);
cb.beginText();
cb.setFontAndSize(helv, 6);
cb.setTextMatrix(document.left(), document.bottom() - 10);
cb.showText(text);
cb.endText();
cb.addTemplate(total, document.left() + textSize1,
document.bottom() - 10);
text = "Not valid without all pages.";
float textSize2 = helv.getWidthPoint(text, 6);
cb.beginText();
cb.setFontAndSize(helv, 6);
cb.setTextMatrix(document.right() - textSize2,
document.bottom() - 10);
cb.showText(text);
cb.endText();
cb.restoreState();
//Add demo "watermark" to the page
String demo = Props.getProperty("funds.pdf.demo","false");
if (demo != null && demo.equalsIgnoreCase("true")) {
PdfContentByte contentunder =
writer.getDirectContentUnder();
contentunder.saveState();
contentunder.setGState(gstate);
contentunder.setColorFill(Color.GRAY);
contentunder.beginText();
contentunder.setFontAndSize(helv, 96);
contentunder.showTextAligned(Element.ALIGN_CENTER,"DEMO",
document.getPageSize().getWidth() / 2, document.getPageSize().getHeight() / 2,
0);
contentunder.endText();
contentunder.restoreState();
}
log.debug("In PageXofY, document.getHtmlStyleClass() = " +
document.getHtmlStyleClass());
if (document.getHtmlStyleClass() != null &&
document.getHtmlStyleClass().equals(ItextUtils.STANDARD_KEY)) {
PdfContentByte contentunder =
writer.getDirectContentUnder();
contentunder.saveState();
contentunder.setGState(gstate);
contentunder.setColorFill(Color.GRAY);
contentunder.beginText();
contentunder.setFontAndSize(helv, 60);
contentunder.showTextAligned(Element.ALIGN_CENTER,"ASSUMES SURRENDER",
document.getPageSize().getWidth() / 2, document.getPageSize().getHeight() / 2,
45);
contentunder.endText();
contentunder.restoreState();
}
}
/**
* @see
com.lowagie.text.pdf.PdfPageEvent#onCloseDocument(com.lowagie.text.pdf.PdfWriter,
* com.lowagie.text.Document)
*/
public void onCloseDocument(PdfWriter writer, Document document) {
log.debug("In onCloseDocument, page = " +
document.getPageNumber());
total.beginText();
total.setFontAndSize(helv, 6);
total.setTextMatrix(0, 0);
total.showText(String.valueOf(document.getPageNumber() - 1));
total.endText();
}
/**
* Generates a file with a header and a footer.
*
* @param args
* no arguments needed here
*/
public static void main(String[] args) {
System.out.println("Chapter 14: Page X of Y Example");
System.out.println("-> Creates a PDF file with page numbers.");
System.out.println("-> jars needed: iText.jar");
System.out.println("-> files generated in /results
subdirectory:");
System.out.println(" page_numbers.pdf");
// step 1: creation of a document-object
Document document = new Document();
try {
// step 2:
PdfWriter writer = PdfWriter.getInstance(document,
new
FileOutputStream("c:/downloads/itext/page_numbers.pdf"));
writer.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft);
writer.setPageEvent(new PageXofY());
document.setMargins(36, 36, 36, 54);
// step 3:
document.open();
Paragraph p = new Paragraph();
// step 4: we grab the ContentByte and do some stuff
with it
for (int k = 1; k <= 30; ++k) {
p.add(new Phrase("Quick brown fox jumps over
the lazy dog. "));
}
p.setAlignment(Element.ALIGN_JUSTIFIED);
for (int k = 1; k <= 12; ++k) {
document.add(p);
}
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
document.close();
}
}-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/