We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to open a PDF which password protected?

Hi,

We have a use case were we need to open a PDF document having security enabled.

Is there a way to open/supply this password and ynamically open the PDF and then optimize the same?

Currently our code is as below:

ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

com.aspose.pdf.Document document = new com.aspose.pdf.Document(filenameSource);
document.optimize();
document.convert("pdfVersioning.txt", profileProcessor.getPdfSettings().pdfVersion, ConvertErrorAction.None);
document.save(filenameSource);
document.dispose();

The above code works fine for PDF's which do not have password protection.

But for PDF's which are password protected, we get below exception:

com.aspose.pdf.exceptions.InvalidPasswordException: Invalid password
at com.aspose.pdf.engine.i.a.n.(Unknown Source)
at com.aspose.pdf.engine.i.a.a(Unknown Source)
at com.aspose.pdf.engine.i.a.(Unknown Source)
at com.aspose.pdf.engine.c.a.a(Unknown Source)
at com.aspose.pdf.engine.data.PdfTrailer.getEncryptor(Unknown Source)
at com.aspose.pdf.engine.data.PdfTrailer.a(Unknown Source)
at com.aspose.pdf.engine.data.PdfTrailer$XrefSerializer.deserialize(Unknown Source)
at com.aspose.pdf.engine.g.c.f.deserialize(Unknown Source)
at com.aspose.pdf.engine.g.i.a(Unknown Source)
at com.aspose.pdf.engine.g.i.i(Unknown Source)
at com.aspose.pdf.engine.g.i.(Unknown Source)
at com.aspose.pdf.engine.g.i.(Unknown Source)
at com.aspose.pdf.engine.c.a.a(Unknown Source)
at com.aspose.pdf.engine.g.j.(Unknown Source)
at com.aspose.pdf.engine.c.a.e(Unknown Source)
at com.aspose.pdf.engine.d.(Unknown Source)
at com.aspose.pdf.Document.a(Unknown Source)
at com.aspose.pdf.Document.(Unknown Source)
at com.aspose.pdf.Document.(Unknown Source)

Please can you let me know how can we open such a PDF file? Or is there any other way to handle this use case?

Thanks,

Satyendra Acharya

Hi Satyendra,


Thanks for your inquiry. Your query is related to Aspose.Pdf. I am moving this forum thread to Aspose.Pdf fourm. My colleagues from Aspose.Pdf team will reply you shortly.

Hi Satyendra,


Thanks for your inquiry. Please check following code snippet to open a password protected PDF document. Later you can optimize the resultant file. Hopefully it will serve the purpose.

//open document
Document document = new Document(“Input.pdf”, “userpassword”);
//decrypt PDF
document.decrypt();
//save updated PDF
document.save(“output.pdf”);
Please feel free to contact us for any further assistance.

Best Regards,