/* * Created on May 22, 2008 * */ package jPDFPrintSamples; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.print.PageFormat; import java.awt.print.Printable; import java.awt.print.PrinterException; import java.awt.print.PrinterJob; import com.qoppa.pdfPrint.PDFPrint; public class Watermark implements Printable { private PDFPrint m_PDFPrint; public static void main (String [] args) { try { // Load PDF document PDFPrint pdfPrint = new PDFPrint ("c:\\qoppa\\auto\\test01.pdf", null); // Create printer job PrinterJob pJob = PrinterJob.getPrinterJob(); if (pJob.printDialog()) { // Set the printable to our custom printable pJob.setPrintable (new Watermark (pdfPrint)); // Print pJob.print(); } } catch (Throwable t) { t.printStackTrace(); } } public Watermark (PDFPrint pdfPrint) { m_PDFPrint = pdfPrint; } public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { // Print the PDF page int rc = m_PDFPrint.print(graphics, pageFormat, pageIndex); // Print the watermark if there is such a page if (rc == PAGE_EXISTS) { paintWatermark((Graphics2D)graphics); } return rc; } public static void paintWatermark (Graphics2D g) { // Watermark color (this can have transparency, but Java then makes the spool file very large) g.setColor(new Color (160, 160, 160)); // Set the font g.setFont (new Font ("sansserif", Font.BOLD, 96)); // Draw the watermark string, rotated 45 degrees g.rotate (Math.toRadians(45)); g.drawString ("Watermark", 100, g.getFontMetrics().getMaxDescent()); } }