java 为pdf添加水印图片

来源:互联网 发布:前瞻网数据 编辑:程序博客网 时间:2024/06/05 04:48

from  http://blog.csdn.net/oxiaoxio/article/details/49512763

首先需要引入两个Jar包分别是:iTextAsian.jar 、itext-2.1.7.jar  可以去  http://download.csdn.net/detail/work201003/9227159  下载;

代码:

[java] view plain copy
 print?
  1. import java.awt.Color;  
  2. import java.io.BufferedOutputStream;  
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.text.SimpleDateFormat;  
  7. import java.util.Calendar;  
  8. import com.lowagie.text.DocumentException;  
  9. import com.lowagie.text.Element;  
  10. import com.lowagie.text.Image;  
  11. import com.lowagie.text.PageSize;  
  12. import com.lowagie.text.pdf.BaseFont;  
  13. import com.lowagie.text.pdf.PdfContentByte;  
  14. import com.lowagie.text.pdf.PdfGState;  
  15. import com.lowagie.text.pdf.PdfReader;  
  16. import com.lowagie.text.pdf.PdfStamper;  
  17.   
  18. public class TestWaterPrint {  
  19.     public static void main(String[] args) throws DocumentException,  
  20.             IOException {  
  21.         // 要输出的pdf文件  
  22.         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("E:/abc.pdf")));  
  23.         Calendar cal = Calendar.getInstance();  
  24.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
  25.         // 将pdf文件先加水印然后输出  
  26.         setWatermark(bos, "D:/itext.pdf", format.format(cal.getTime())  
  27.                 + "  下载使用人:" + "测试user"16);  
  28.     }  
  29.   
  30.     public static void setWatermark(BufferedOutputStream bos, String input,  
  31.             String waterMarkName, int permission) throws DocumentException,  
  32.             IOException {  
  33.           
  34.         PdfReader reader = new PdfReader(input);  
  35.         PdfStamper stamper = new PdfStamper(reader, bos);  
  36.         int total = reader.getNumberOfPages() + 1;  
  37.         PdfContentByte content;  
  38.         BaseFont base = BaseFont.createFont("STSong-Light""UniGB-UCS2-H",BaseFont.EMBEDDED);  
  39.         PdfGState gs = new PdfGState();  
  40.         for (int i = 1; i < total; i++) {  
  41.             content = stamper.getOverContent(i);// 在内容上方加水印  
  42.             //content = stamper.getUnderContent(i);//在内容下方加水印  
  43.             gs.setFillOpacity(0.2f);  
  44.             // content.setGState(gs);  
  45.             content.beginText();  
  46.             content.setColorFill(Color.LIGHT_GRAY);  
  47.             content.setFontAndSize(base, 50);  
  48.             content.setTextMatrix(70200);  
  49.             content.showTextAligned(Element.ALIGN_CENTER, "公司内部文件,请注意保密!"300,35055);  
  50.             Image image = Image.getInstance("D:/itext2.png");  
  51.             /*img.setAlignment(Image.LEFT | Image.TEXTWRAP); 
  52.             img.setBorder(Image.BOX); 
  53.             img.setBorderWidth(10); 
  54.             img.setBorderColor(BaseColor.WHITE); 
  55.             img.scaleToFit(1000, 72);//大小 
  56.             img.setRotationDegrees(-30);//旋转 */  
  57.             image.setAbsolutePosition(200206); // set the first background image of the absolute   
  58.             image.scaleToFit(200,200);  
  59.             content.addImage(image);  
  60.             content.setColorFill(Color.BLACK);  
  61.             content.setFontAndSize(base, 8);  
  62.             content.showTextAligned(Element.ALIGN_CENTER, "下载时间:"  
  63.                     + waterMarkName + ""300100);  
  64.             content.endText();  
  65.   
  66.         }  
  67.         stamper.close();  
  68.     }  
  69. }  

原文档图



效果图:



更多详细代码请查看:http://www.open-open.com/lib/view/open1339726488115.html


0 0
原创粉丝点击