0

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

Alex K
  • 22,315
  • 19
  • 108
  • 236
  • Why iText 5.3.2 and not iText 5.5.12? – Amedee Van Gasse Oct 18 '17 at 09:26
  • `java.lang.noclassdeffounderror: com/lowagie/text/documentexception`: this is to be expected. You need to replace `com.lowagie` with `com.itextpdf` in all your import statements. – Amedee Van Gasse Oct 18 '17 at 09:27
  • _I read somewhere_ - where did you read that? Edit your post and add the link. – Amedee Van Gasse Oct 18 '17 at 09:27
  • @AlexK This question is not really a duplicate of the questions you mention: The main issue in it essentially is finding the appropriate BouncyCastle versions, the erroneous iText update was merely an attempt to get around that. – mkl Oct 18 '17 at 10:54
  • @AlexK Ok. As those additional duplicates don't point to the BC version required by iText 2.1.7, though, I'll leave my answer here for those who wonder which BC version they shall use with an iText compatible with JR. – mkl Oct 18 '17 at 13:00
  • @AlexK By the way, one of the duplicate questions you listed was actually about a contrary problem: The OP of that issue used a JR version which required a 5.x iText version and still had a 2.1.7 iText in his class path which is the other way around than the issue here... ;) – mkl Oct 19 '17 at 09:43
  • @AlexK ;)))) One might consider creating a general Q&A about JR dependencies (including iText and BC versions but dealing with other external libraries, too) to have a good answer to dup-close all these questions to. – mkl Oct 19 '17 at 10:06
  • 1
    @mkl I agree with you. It’s a pity that documentation is gone away - it was a good place. – Alex K Oct 19 '17 at 11:42

1 Answers1

2

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.

mkl
  • 90,588
  • 15
  • 125
  • 265