PeopleCode PDF Password Protection Example

We saw how to create a password protected secure PDF document using iText in Java earlier. In this post, we will do an extension to what we discussed. We will discuss how to generate a password protected PDF document from PeopleCode. We will be using Javaobjects to accomplish this ; so make sure that you have loaded the latest iText jar file to your application server class folder. Also, load the bcprov-jdk15-136.jar to the class folder as this JAR file is required to support PDF encryption. If you are running Java 1.4 then you need to pick the appropriate JAR files for the example to work. You will need  iText-2.1.7.jar and bcprov-ext-jdk14-146.jar. The PeopleCode example to password protect a PDF is provided below
/* Create a document object */
Local JavaObject &Obj_pdfdocument_l = CreateJavaObject("com.lowagie.text.Document");
/* Get an instance of PDF Writer */
Local JavaObject &obj_writePDF_l = GetJavaClass("com.lowagie.text.pdf.PdfWriter").getInstance(&Obj_pdfdocument_l, CreateJavaObject("", "Hello.pdf", True));
/* Call setEncryption and provide user password and owner password */
&obj_writePDF_l.setEncryption(CreateJavaObject("java.lang.String", "pwd").getBytes(), CreateJavaObject("java.lang.String", "pwd").getBytes(), 0, GetJavaClass("com.lowagie.text.pdf.PdfWriter").STANDARD_ENCRYPTION_128);
/* add some contents into the PDF */
&Obj_pdfdocument_l.add(CreateJavaObject("com.lowagie.text.Paragraph", "Hello, this is PeopleCode PDF Password Protection"));

If you are running this on version 1.4 and above, then you have to change the path as "lowagie" folder does not exist in new version. The explanation for this is on the same lines as of our java example. In the "setEncryption" call , to pass a byte array for password, we create a Java object of type java.lang.String and use the "getBytes" method to convert it to Byte array. This can be done in some other way, but at the time of writing I found this quicker to covert a string to byte array in PeopleCode.

When you run this example, you can find a PDF file generated in your application server / process scheduler folder. Since we are passing 0 to "setEncryption" call, you will not be able to print / copy the contents of the PDF file.You will get the following error message if you attempt to run a higher version of itext.jar, in 1.4 version of Java.

Java Exception: java.lang.UnsupportedClassVersionError: com/itextpdf/text/Document (Unsupported major.minor version 49.0): finding class com.itextpdf.text.Document

1 comment:

  1. We have some pdf files in our FTP server and we need to make those files password protected. Please help.