分类: Java
2010-10-11 14:02:29
import java.awt.Color; import java.io.FileOutputStream; import java.io.IOException; import com.lowagie.text.DocumentException; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfReader; import com.lowagie.text.pdf.PdfStamper; /** * 文字水印添加 * */ public class Simple8 { @SuppressWarnings("deprecation") public static void main(String[] args) throws DocumentException, IOException { //待加水印的文件 PdfReader reader = new PdfReader("E:/HelloWorld.pdf"); //加完水印的文件 PdfStamper stamper = new PdfStamper(reader, new FileOutputStream( "E:/HelloWorld_water.pdf")); int total = reader.getNumberOfPages()+1; PdfContentByte content; //设置字体 BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED); //水印文字 String waterText=""; int j = waterText.length(); //文字长度 char c = 0; int high = 0;//高度 //循环对每页插入水印 for (int i = 1; i < total; i++) { //水印的起始 high = 500; content = stamper.getUnderContent(i); //开始 content.beginText(); //设置颜色 content.setColorFill(Color.GRAY); //设置字体及字号 content.setFontAndSize(base, 18); //设置起始位置 content.setTextMatrix(100, 120); //开始写入水印 for (int k = 0; k < j; k++) { content.setTextRise(rise); c = waterText.charAt(k); //将char转成字符串 content.showText(c + ""); high -= 5; } content.endText(); } stamper.close(); } }
stamper.setEncryption("user".getBytes(), "admin".getBytes(), 16, false);