12

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

我有一小段代码来加载不受保护的 PDF 表单

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

谁能帮帮我..谢谢

4

2 回答 2

12

试试这个代码:

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"));
        }
    }
于 2013-02-08T09:38:26.440 回答
8

你可以使用

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();
    } 
 }
于 2016-04-12T06:22:06.957 回答