zoukankan      html  css  js  c++  java
  • Java 加密PDF设置密码并添加水印

    /**
     * Project Name:XXX
     * File Name:EncryptLogFile.java
     * Date:2016-6-12上午11:56:38
     * Copyright (c) 2016, All Rights Reserved.
     *
     */
    
    package com.XXX.XXX.utils;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    
    import com.lowagie.text.Document;
    import com.lowagie.text.DocumentException;
    import com.lowagie.text.Font;
    import com.lowagie.text.HeaderFooter;
    import com.lowagie.text.PageSize;
    import com.lowagie.text.Paragraph;
    import com.lowagie.text.Phrase;
    import com.lowagie.text.Rectangle;
    import com.lowagie.text.pdf.BaseFont;
    import com.lowagie.text.pdf.PdfReader;
    import com.lowagie.text.pdf.PdfStamper;
    import com.lowagie.text.pdf.PdfWriter;
    
    /**
     * ClassName:EncryptLogFile
     * Function: 日志文档转PDF并添加密码及水印. Date: 2016-6-12
     * 上午11:56:38
     *
     * @author yang
     * @version
     */
    public class EncryptLogFile {
        // 生成临时文件前缀
        private static final String prefix = "tempFile";
    
        // 所有者密码
        private static final String OWNERPASSWORD = "12345678";
    
        /**
         * txt文件转换为pdf文件
         *
         * @param txtFile
         *            txt文件路径
         * @param pdfFile
         *            pdf文件路径
         * @param userPassWord
         *            用户密码
         * @param waterMarkName
         *            水印内容
         * @param permission
         *            操作权限
         */
        public static void generatePDFWithTxt(String txtFile, String pdfFile,
                String userPassWord, String waterMarkName, int permission) {
            try {
                // 生成临时文件
                File file = File.createTempFile(prefix, ".pdf");
                // 创建pdf文件到临时文件
                if (createPDFFile(txtFile, file)) {
                    // 增加水印和加密
                    waterMark(file.getPath(), pdfFile, userPassWord, OWNERPASSWORD,
                            waterMarkName, permission);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 创建PDF文档
         *
         * @param txtFilePath
         *            txt文件路径(源文件)
         * @param pdfFilePath
         *            pdf文件路径(新文件)
         */
        private static boolean createPDFFile(String txtFilePath, File file) {
            // 设置纸张
            Rectangle rect = new Rectangle(PageSize.A4);
            // 设置页码
            HeaderFooter footer = new HeaderFooter(new Phrase("页码:",
                    setChineseFont()), true);
            footer.setBorder(Rectangle.NO_BORDER);
    
            // step1
            Document doc = new Document(rect, 50, 50, 50, 50);
            doc.setFooter(footer);
            try {
                FileReader fileRead = new FileReader(txtFilePath);
                BufferedReader read = new BufferedReader(fileRead);
                // 设置pdf文件生成路径 step2
                PdfWriter.getInstance(doc, new FileOutputStream(file));
                // 打开pdf文件 step3
                doc.open();
                // 实例化Paragraph 获取写入pdf文件的内容,调用支持中文的方法. step4
                while (read.ready()) {
                    // 添加内容到pdf(这里将会按照txt文件的原始样式输出)
                    doc.add(new Paragraph(read.readLine(), EncryptLogFile.setChineseFont()));
                }
                // 关闭pdf文件 step5
                doc.close();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 在pdf加密
         *
         * @param inputFile
         *            原始文件
         * @param outputFile
         *            水印输出文件
         * @param waterMarkName
         *            水印名字
         */
        private static void waterMark(String inputFile, String outputFile,
                String userPassWord, String ownerPassWord, String waterMarkName,
                int permission) {
            try {
                PdfReader reader = new PdfReader(inputFile);
                PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
                        outputFile));
                // 设置密码
                stamper.setEncryption(userPassWord.getBytes(),
                        ownerPassWord.getBytes(), permission, false);
                stamper.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 设置中文
         *
         * @return Font
         */
        private static Font setChineseFont() {
            BaseFont base = null;
            Font fontChinese = null;
            try {
                base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                        BaseFont.EMBEDDED);
                fontChinese = new Font(base, 20, Font.NORMAL);
            } catch (DocumentException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return fontChinese;
        }
    }


    传送门:加密所需Jar包

  • 相关阅读:
    Eclipse安装代码反编译插件Enhanced Class Decompiler
    使用idea创建web项目
    shell编程学习笔记(十二):Shell中的break/continue跳出循环
    shell编程学习笔记(十一):Shell中的while/until循环
    windows环境下安装zookeeper(仅本地使用)
    解决base64-encoded secret key cannot be null or empty问题
    不就一个上传图片功能吗,为什么要搞得那么复杂?
    解决WARN警告:Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended
    Jmeter发送jdbc请求(操作mysql)
    Jmeter-ForEach控制器
  • 原文地址:https://www.cnblogs.com/shinubi/p/5579746.html
Copyright © 2011-2022 走看看