1. 程式人生 > >itext pdf 新增水印

itext pdf 新增水印

public class Watermark extends PdfPageEventHelper {
 
        protected Phrase watermark = new Phrase("WATERMARK", new Font(FontFamily.HELVETICA, 60, Font.NORMAL, BaseColor.LIGHT_GRAY));
 
        @Override
        public void onEndPage(PdfWriter writer, Document document) {
            PdfContentByte canvas =
writer.getDirectContentUnder(); ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, watermark, 298, 421, 45); } }
public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance
(document, new FileOutputStream(dest)); writer.setPageEvent(new Watermark()); document.open(); //--------
                document.close();
    }