Itext生成PDF文件加密与加水印

最近的项目中使用Itext将txt文件转换为PDF文件,并且实现对文件的一些权限控制。现实对pdf文件加密,添加水印等。最主要的是要实现对pdf文件实现密钥签名,一直都还没有实现成功!等实现好了后再加上来。 
Java代码   收藏代码
  1. public class PdfConvertor {  
  2.     //txt原始文件的路径  
  3.     private static final String txtFilePath = "d:/Itext/test.txt";  
  4.     //生成的pdf文件路径  
  5.     private static final String pdfFilePath = "d:/Itext/test.pdf";  
  6.     //添加水印图片路径  
  7.     private static final String imageFilePath = "D:/image/b.gif";  
  8.     //生成临时文件前缀  
  9.     private static final String prefix = "tempFile";  
  10.     //所有者密码  
  11.     private static final String OWNERPASSWORD = "12345678";  
  12.   
  13.     /** 
  14.      * txt文件转换为pdf文件 
  15.      *  
  16.      * @param txtFile 
  17.      *            txt文件路径 
  18.      * @param pdfFile 
  19.      *            pdf文件路径 
  20.      * @param userPassWord 
  21.      *            用户密码 
  22.      * @param waterMarkName 
  23.      *            水印内容 
  24.      * @param permission 
  25.      *            操作权限 
  26.      */  
  27.     public static void generatePDFWithTxt(String txtFile, String pdfFile,  
  28.             String userPassWord, String waterMarkName, int permission) {  
  29.         try {  
  30.             // 生成临时文件  
  31.             File file = File.createTempFile(prefix, ".pdf");  
  32.             // 创建pdf文件到临时文件  
  33.             if (createPDFFile(txtFile, file)) {  
  34.                 // 增加水印和加密  
  35.                 waterMark(file.getPath(), pdfFile, userPassWord, OWNERPASSWORD,  
  36.                         waterMarkName, permission);  
  37.             }  
  38.         } catch (Exception e) {  
  39.             e.printStackTrace();  
  40.         }  
  41.   
  42.     }  
  43.   
  44.     /** 
  45.      * 创建PDF文档 
  46.      *  
  47.      * @param txtFilePath 
  48.      *            txt文件路径(源文件) 
  49.      * @param pdfFilePath 
  50.      *            pdf文件路径(新文件) 
  51.      */  
  52.     private static boolean createPDFFile(String txtFilePath, File file) {  
  53.         // 设置纸张  
  54.         Rectangle rect = new Rectangle(PageSize.A4);  
  55.         // 设置页码  
  56.         HeaderFooter footer = new HeaderFooter(new Phrase("页码:", PdfConvertor  
  57.                 .setChineseFont()), true);  
  58.         footer.setBorder(Rectangle.NO_BORDER);  
  59.         // step1  
  60.         Document doc = new Document(rect, 50505050);  
  61.         doc.setFooter(footer);  
  62.         try {  
  63.             FileReader fileRead = new FileReader(txtFilePath);  
  64.             BufferedReader read = new BufferedReader(fileRead);  
  65.             // 设置pdf文件生成路径 step2  
  66.             PdfWriter.getInstance(doc, new FileOutputStream(file));  
  67.             // 打开pdf文件 step3  
  68.             doc.open();  
  69.             // 实例化Paragraph 获取写入pdf文件的内容,调用支持中文的方法. step4  
  70.             while (read.ready()) {  
  71.                 // 添加内容到pdf(这里将会按照txt文件的原始样式输出)  
  72.                 doc.add(new Paragraph(read.readLine(), PdfConvertor  
  73.                         .setChineseFont()));  
  74.             }  
  75.             // 关闭pdf文件 step5  
  76.             doc.close();  
  77.             return true;  
  78.         } catch (Exception e) {  
  79.             e.printStackTrace();  
  80.             return false;  
  81.         }  
  82.     }  
  83.   
  84.     /** 
  85.      * 在pdf文件中添加水印 
  86.      *  
  87.      * @param inputFile 
  88.      *            原始文件 
  89.      * @param outputFile 
  90.      *            水印输出文件 
  91.      * @param waterMarkName 
  92.      *            水印名字 
  93.      */  
  94.     private static void waterMark(String inputFile, String outputFile,  
  95.             String userPassWord, String ownerPassWord, String waterMarkName,  
  96.             int permission) {  
  97.         try {  
  98.             PdfReader reader = new PdfReader(inputFile);  
  99.             PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(  
  100.                     outputFile));  
  101.             // 设置密码     
  102.             stamper.setEncryption(userPassWord.getBytes(), ownerPassWord  
  103.                     .getBytes(), permission, false);  
  104.             BaseFont base = BaseFont.createFont("STSong-Light""UniGB-UCS2-H",  
  105.                     BaseFont.NOT_EMBEDDED);  
  106.             int total = reader.getNumberOfPages() + 1;  
  107.             Image image = Image.getInstance(imageFilePath);  
  108.             image.setAbsolutePosition(200400);  
  109.             PdfContentByte under;  
  110.             int j = waterMarkName.length();  
  111.             char c = 0;  
  112.             int rise = 0;  
  113.             for (int i = 1; i < total; i++) {  
  114.                 rise = 500;  
  115.                 under = stamper.getUnderContent(i);  
  116.                 // 添加图片  
  117.                 // under.addImage(image);  
  118.                 under.beginText();  
  119.                 under.setColorFill(Color.CYAN);  
  120.                 under.setFontAndSize(base, 30);  
  121.                 // 设置水印文字字体倾斜 开始  
  122.                 if (j >= 15) {  
  123.                     under.setTextMatrix(200120);  
  124.                     for (int k = 0; k < j; k++) {  
  125.                         under.setTextRise(rise);  
  126.                         c = waterMarkName.charAt(k);  
  127.                         under.showText(c + "");  
  128.                         rise -= 20;  
  129.                     }  
  130.                 } else {  
  131.                     under.setTextMatrix(180100);  
  132.                     for (int k = 0; k < j; k++) {  
  133.                         under.setTextRise(rise);  
  134.                         c = waterMarkName.charAt(k);  
  135.                         under.showText(c + "");  
  136.                         rise -= 18;  
  137.                     }  
  138.                 }  
  139.                 // 字体设置结束  
  140.                 under.endText();  
  141.                 // 画一个圆  
  142.                 // under.ellipse(250, 450, 350, 550);  
  143.                 // under.setLineWidth(1f);  
  144.                 // under.stroke();  
  145.             }  
  146.             stamper.close();  
  147.         } catch (Exception e) {  
  148.             e.printStackTrace();  
  149.         }  
  150.     }  
  151.   
  152.     /** 
  153.      * 设置中文 
  154.      *  
  155.      * @return Font 
  156.      */  
  157.     private static Font setChineseFont() {  
  158.         BaseFont base = null;  
  159.         Font fontChinese = null;  
  160.         try {  
  161.             base = BaseFont.createFont("STSong-Light""UniGB-UCS2-H",  
  162.                     BaseFont.EMBEDDED);  
  163.             fontChinese = new Font(base, 12, Font.NORMAL);  
  164.         } catch (DocumentException e) {  
  165.             e.printStackTrace();  
  166.         } catch (IOException e) {  
  167.             e.printStackTrace();  
  168.         }  
  169.         return fontChinese;  
  170.     }  
  171.   
  172.         public static void main(String[] args) {  
  173.         generatePDFWithTxt(txtFilePath,    pdfFilePath, "123""www.emice.com"16);  
  174.         }  
  175. }  
  • Itext_lib.rar (2.5 MB)
  • 描述: bcprov-jdk15-139.jar 加密时用到。 iText-2.1.2u.jar Itext包。 iTextAsian.jar 在导入中文是要用到。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值