Skip to content
Advertisement

Merge encrypted pdf file programmatically exception

I have been using the following code to merge encrypted pdf files programmatically.

JavaScript

I started getting this error recently upon trying to create the pdfReader at this line of code:

JavaScript

com.itextpdf.text.exceptions.InvalidPdfException: Unknown encryption type R = 6 at com.itextpdf.text.pdf.PdfReader.readPdf(PdfReader.java:738) at com.itextpdf.text.pdf.PdfReader.(PdfReader.java:181) at com.itextpdf.text.pdf.PdfReader.(PdfReader.java:219) at com.itextpdf.text.pdf.PdfReader.(PdfReader.java:207) at com.project.mainPageShop.mergeMyFiles(mainPageShop.java:4368) at com.project.mainPageShop$DownloadFileAsync.onPostExecute(mainPageShop.java:11757) at com.project.mainPageShop$DownloadFileAsync.onPostExecute(mainPageShop.java:11628) at android.os.AsyncTask.finish(AsyncTask.java:755) at android.os.AsyncTask.access$900(AsyncTask.java:192) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:772) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:237) at android.app.ActivityThread.main(ActivityThread.java:7814) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)

Kindly note that the password is correct, and same file which use to work using this code, is now raising this exception.

UPDATE:

I have used the following code using com.itextpdf:itext7-core:7.0.2

JavaScript

This is the logcat Result:

FATAL EXCEPTION: main Process: com.project, PID: 7665 com.itextpdf.kernel.PdfException: Unknown encryption type R == 6. at com.itextpdf.kernel.pdf.PdfEncryption.readAndSetCryptoModeForStdHandler(PdfEncryption.java:508) at com.itextpdf.kernel.pdf.PdfEncryption.(PdfEncryption.java:181) at com.itextpdf.kernel.pdf.PdfReader.readDecryptObj(PdfReader.java:1061) at com.itextpdf.kernel.pdf.PdfReader.readPdf(PdfReader.java:531) at com.itextpdf.kernel.pdf.PdfDocument.open(PdfDocument.java:1585) at com.itextpdf.kernel.pdf.PdfDocument.(PdfDocument.java:281) at com.itextpdf.kernel.pdf.PdfDocument.(PdfDocument.java:249) at com.project.mainPageShop.mergePDFFiles(mainPageShop.java:4353) at com.neelwafurat.iKitabForAndroid.mainPageShop$DownloadFileAsync.onPostExecute(mainPageShop.java:11788) at com.project.mainPageShop$DownloadFileAsync.onPostExecute(mainPageShop.java:11659) at android.os.AsyncTask.finish(AsyncTask.java:755) at android.os.AsyncTask.access$900(AsyncTask.java:192) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:772) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:237) at android.app.ActivityThread.main(ActivityThread.java:7814) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)

The error is occurring at the following line:

JavaScript

Find below links for sample encrypted pdf files used with password: 123456

https://smallpdf.com/shared#st=8921059d-6615-4264-a3f6-c76d476dc168&fn=test+1.pdf&ct=1602755420749&tl=share-document&rf=link

https://smallpdf.com/shared#st=7d3c11c7-b34d-4399-bc03-c66b7be788d0&fn=test+2.pdf&ct=1602755505331&tl=share-document&rf=link

Advertisement

Answer

Revision 6 of the standard security handler is not supported in iText 5 or iText 7.0.2. It was introduced in iText 7.1.0 in the context of PDF 2.0 support.

I have slightly tweaked your code sample to test. Note that you were not passing the password to the PdfReader instance.

Also note that the password you shared for the sample PDFs (123456) is the user password, not the owner password.

A user password, also called open password, is used to open a PDF document, i.e. to give access to the content. All other restrictions are handled by an owner password, also called permissions password, e.g. allowing/preventing a document being printed.

For your sample documents, both the open password and permissions password are set:

PDF permissions

If you have the owner password available, you should use it the process the PDF, i.e. pass it to the PdfReader instance. That allows you to avoid the unethicalreading setting.

Test code:

JavaScript

iText 7.0.2 does not support revision 6, as you’ve noticed:

JavaScript

Starting from iText 7.1.0 up until the current release (7.1.13), your 2 sample files will merge correctly, resulting in a 4-page output file.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement