I am trying to generate a password protected PDF file using JasperReports and Java.
Code is as follows:
jrPdfExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, path.concat(filename).concat(".pdf"));
jrPdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jprint);
jrPdfExporter.setParameter(JRPdfExporterParameter.OWNER_PASSWORD, "Bancosol123");
jrPdfExporter.setParameter(JRPdfExporterParameter.USER_PASSWORD, "Bancosol123");
jrPdfExporter.setParameter(JRPdfExporterParameter.IS_ENCRYPTED, Boolean.TRUE);
jrPdfExporter.exportReport();
I was using iText 2.1.7 to generate the PDF, it was working fine, but when I included the password part, I got the error
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.asn1.DEREncodable
I included the following two jars:
bcprov-jdk15on-1.47.jar
bcmail-jdk15on-1.47.jar
But the issue still was not solved. I read somewhere it might be a compatibility issue with iText 2.1.7
So I removed iText 2.1.7 and included iText 5.3.2
Now I have the following error
exception in thread "awt-eventqueue-0" java.lang.noclassdeffounderror: com/lowagie/text/documentexception
JasperReports PDF generation is based on iText 2.1.7 (at least current versions, there were some JR versions which used a 5.x version but for some reason this change had been reverted), actually even a slightly patched variant thereof.
Thus, you most likely will want to continue using a pre-5.0.0 version of iText.
Now you should be aware that those old iText versions are programmed against an equally old BouncyCastle version: The Maven dependency information in the JR iText 2.1.7 variant indicate the use of BouncyCastle 1.38.
This also explains the original error message, there are substantial changes between BC 1.38 and 1.47, in particular version 1.47 itself was a major change of the BC API.
So you should try with older BC versions, preferably 1.38.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.