Ganeshja
Ganeshja

Reputation: 2765

How to load a password protected PDF form using PDFBOX

How to load a password protected PDF form using PDFBOX

I have a small piece of code to load non protected PDF form

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

Can any one help me out .. Thanks

Upvotes: 12

Views: 14214

Answers (2)

ksh
ksh

Reputation: 649

You can just use

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();
    } 
 }

Upvotes: 10

SANN3
SANN3

Reputation: 10069

Try this code :

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"));
        }
    }

Upvotes: 12

Related Questions