java 对PDF文件进行密码加密

10 篇文章 0 订阅

java 对PDF文件进行密码加密

添加 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);
    }

加密示例
加密后的文件打开需输入密码
在这里插入图片描述

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要使用iTextPDF对MultipartFile文件进行加密,你可以按照以下步骤进行操作: 1. 首先,确保你的项目中已经添加了iTextPDF的相关依赖。你可以在项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13.2</version> </dependency> ``` 2. 在你的加密方法中,使用iTextPDF的相关API来实现文件加密。下面是一个简单的示例代码: ```java import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; import com.itextpdf.text.pdf.PdfWriter; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; public class PDFEncryptionUtil { public static void encryptPdf(InputStream inputPdfStream, OutputStream encryptedPdfStream, String userPassword, String ownerPassword) throws Exception { PdfReader reader = new PdfReader(inputPdfStream); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfStamper stamper = new PdfStamper(reader, baos); stamper.setEncryption(userPassword.getBytes(), ownerPassword.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128); stamper.close(); reader.close(); encryptedPdfStream.write(baos.toByteArray()); } } ``` 3. 在你的控制器或服务类中,接收MultipartFile参数,并调用PDFEncryptionUtil.encryptPdf方法进行文件加密,如下所示: ```java import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import java.io.ByteArrayOutputStream; import java.io.OutputStream; @RestController public class FileController { @PostMapping("/encrypt") public void encryptFile(@RequestParam("file") MultipartFile file) { try { InputStream inputStream = file.getInputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PDFEncryptionUtil.encryptPdf(inputStream, outputStream, "userPassword", "ownerPassword"); // 处理加密后的文件,例如保存到本地或者返回给前端 } catch (Exception e) { e.printStackTrace(); } } } ``` 在上述代码中,`encryptPdf`方法接收一个输入流`inputPdfStream`,一个输出流`encryptedPdfStream`,以及用户密码和所有者密码。该方法使用iTextPDF的`PdfStamper`类将输入的PDF文件加密,并将加密后的内容写入到输出流中。 请注意,这只是一个简单的示例,你可以根据自己的需求进行修改和扩展。 希望对你有所帮助!如果还有其他问题,请继续提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值