用itext解密受保护的pdf和用户密码

时间:2017-01-12 04:58:00

标签: java itext

使用 itext 用用户密码解密受保护的pdf。在控制台中显示为用法:

{{1}}

帮助我成为 itext

的新手

1 个答案:

答案 0 :(得分:0)

您可以参考此代码,它在我的情况下正常工作:

public class PDFUtils {

    public static final String SOURCE_FILE = "encrypted.pdf";
    public static final String DESTINATION_FILE = "decrypted.pdf";

    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DESTINATION_FILE );
        file.getParentFile().mkdirs();
        new PDFUtils ().decryptPdf(SOURCE_FILE , DESTINATION_FILE );
    }

    public void decryptPdf(String srcFile, String destFile) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(srcFile, "XXX".getBytes());
        System.out.println(new String(reader.computeUserPassword()));
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destfile));
        stamper.close();
        reader.close();
    }
}