0

I am using Flying Saucer to generate PDF from XHTML. Rendering works nice, but any attempt to password-protect the PDF document fails with an Exception.

I am using code recommended by Ajit Soman in https://stackoverflow.com/a/46024498/4517737. A copy of code is shown below. If you un-comment the commented line, Exception is thrown in line "renderer.createPDF(os, false)".

The Exception is "Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException" (for JavaFX application).

For plain Java, the Exception is "Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/asn1/ASN1Encodable at com.itextpdf.text.pdf.PdfEncryption.(PdfEncryption.java:147)..... Caused by: java.lang.ClassNotFoundException:org.bouncycastle.asn1.ASN1Encodable"

final File outputFile = File.createTempFile(fileName, ".pdf");
FileOutputStream os = new FileOutputStream(outputFile);
PDFEncryption pdfEncryption  = new PDFEncryption();
String password= "password@123";
pdfEncryption.setUserPassword(password.getBytes());
ITextRenderer renderer = new ITextRenderer();

// the following line causes Exception
// renderer.setPDFEncryption(pdfEncryption);

renderer.setDocumentFromString(htmlContent);
renderer.layout();
renderer.createPDF(os, false);
renderer.finishPDF();

Please help.

0 Answers0