赞
踩
添加 pom 依赖
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
对pdf文件加密
private static boolean pdfEncrypt(String filePath, String savePath, String password){ try{ com.itextpdf.text.pdf.PdfReader reader = new com.itextpdf.text.pdf.PdfReader(filePath); com.itextpdf.text.pdf.PdfStamper stamper = new com.itextpdf.text.pdf.PdfStamper(reader, new FileOutputStream(savePath)); // 设置密码 stamper.setEncryption(password.getBytes(),password.getBytes(), com.itextpdf.text.pdf.PdfWriter.ALLOW_PRINTING, com.itextpdf.text.pdf.PdfWriter.ENCRYPTION_AES_128); stamper.close(); reader.close(); logger.debug("pdfEncrypt-加密成功"); return true; }catch (Exception e){ e.printStackTrace(); logger.error("对【{}】文件加密处理异常:{}",filePath,e.getMessage()); return false; } }
main 测试
public static void main(String[] args) {
String filePath = "D:\\liang/office加密/pdf加密-前-1.pdf";
String savePath = "D:\\liang/office加密/pdf加密-后-1.pdf";
String password = "123";
pdfEncrypt(filePath,savePath,password);
}
加密示例
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。