当前位置: 代码迷 >> J2SE >> itext 水印的有关问题
  详细解决方案

itext 水印的有关问题

热度:726   发布时间:2016-04-24 00:41:54.0
itext 水印的问题
有用itext 5.0以后版本生成PFD的么? 怎么生成图片水印? 给个参考

------解决方案--------------------
Java code
try {            PdfReader Read_PDF_To_Watermark = new PdfReader("Sample.pdf");            int number_of_pages = Read_PDF_To_Watermark.getNumberOfPages();            PdfStamper stamp = new PdfStamper(Read_PDF_To_Watermark, new FileOutputStream("New_PDF_With_Watermark_Image.pdf"));            int i = 0;            Image watermark_image = Image.getInstance("watermark.jpg");            watermark_image.setAbsolutePosition(200, 400);            PdfContentByte add_watermark;                        while (i < number_of_pages) {              i++;              add_watermark = stamp.getUnderContent(i);              add_watermark.addImage(watermark_image);            }            stamp.close();        }        catch (Exception i1) {            i1.printStackTrace();        }
------解决方案--------------------
Image watermarkImage = new Image(imgPath);
watermarkImage.setAbsolutePosition(x, y);

writer.setPageEvent( new MyPageEvent(watermarkImage) );


public MyPageEvent extends PdfPageEventHelper {
private Image waterMark;
public MyPageEvent(Image img) {
waterMark = img;
}
public void OnEndPage(PdfWriter writer, Document doc) {
PdfContentByte content = writer.getContent();
content.addImage( waterMark );
}
}