I need to process a large number of protected pdf files and edit them using a java program, but i don't have the passwords for these files. Is there any way to unlock this files with a batch?
I would like a windows command like tool or a java open source api. What is the best solution ?
are these pdf files asking for passwords to be opened, or protection consist in restrictions about copy, printing, modify, text extraction and so on?
if this is your case, you can try with
qpdf
usage:
qpdf --decrypt file.pdf unlocked.pdf
and can be used in a batch operation en masse, in this way
for f in *.pdf ; do qpdf --decrypt $f ${f%%.pdf}unlocked.pdf; done
For decryption, you can use PDFBox, see my answer here : https://stackoverflow.com/a/9976481/535203
I give a sample code to decrypt a PDF given its password.
There is also a binary to do that with PDFBox, check the usage here.
You can also do it using iText library.
Here is a sample code :
PdfReader.unethicalreading = true;
PdfReader reader = new PdfReader(inputFile);
PdfEncryptor.encrypt(reader, new FileOutputStream(outputFile), null,
null, PdfWriter.ALLOW_ASSEMBLY | PdfWriter.ALLOW_COPY
| PdfWriter.ALLOW_DEGRADED_PRINTING | PdfWriter.ALLOW_FILL_IN
| PdfWriter.ALLOW_MODIFY_ANNOTATIONS | PdfWriter.ALLOW_MODIFY_CONTENTS
| PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_SCREENREADERS, false);
P.S. : use unethicalreading
flag on your own risk.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With