Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a password protected PDF form using PDFBOX

Tags:

java

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

like image 574
Ganeshja Avatar asked Feb 08 '13 09:02

Ganeshja


2 Answers

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();
    } 
 }
like image 187
ksh Avatar answered Oct 08 '22 05:10

ksh


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"));
        }
    }
like image 30
SANN3 Avatar answered Oct 08 '22 06:10

SANN3