如何使用PDFBOX加载受密码保护的PDF表单

时间:2013-02-08 09:29:23

标签: java pdfbox

如何使用PDFBOX加载受密码保护的PDF表单

我有一小段代码可以加载非受保护的PDF表单

  PDDocument pdfDoc;
  pdfDoc = PDDocument.load(filePath);

任何人都可以帮我解决.. 谢谢

2 个答案:

答案 0 :(得分:9)

试用此代码:

private void openPDFDoc(final File pdfFile) throws Exception {
        File originalPDF = pdfFile;
        PDFParser parser = new PDFParser(new BufferedInputStream(new FileInputStream(
                originalPDF)));
        parser.parse();

        PDDocument originialPdfDoc = parser.getPDDocument();

        boolean isOriginalDocEncrypted = originialPdfDoc.isEncrypted();
        if (isOriginalDocEncrypted) {
            originialPdfDoc.openProtection(new StandardDecryptionMaterial("password"));
        }
    }

答案 1 :(得分:2)

你可以使用

public static void main(String[] args){
PDDocument pd;
try {
     File input = new File("p.pdf");  // password protected PDF file from where you would like to extract
     pd = PDDocument.load(input,"your_password");
     pd.setAllSecurityToBeRemoved(true);

     //for printing pdf file data
     PDFTextStripper reader = new PDFTextStripper();
     String pageText = reader.getText(pd);
     System.out.println(pageText);
     } catch (Exception e){
     e.printStackTrace();
    } 
 }
相关问题