Saturday, December 22, 2007

Getting started with iText - Part 16 (EncryptorExample.java)

In Part 16 of Getting Started with iText we will translate the EncryptorExample.java example. This example demonstrates how to encrypt an existing PDF file.

The java example calls a single method in the PdfEncryptor helper class to perform the encryption. You can read more about the parameters for this function (specifically permissions) in the iText online tutorial.

First a few words about deprecation and encryption


1. While you can run this example "as is" under MX7 or CF8, it does use two deprecated constants when setting permissions. According to the API PdfWriter.AllowCopy and PdfWriter.AllowPrinting are deprecated and "scheduled for removal at or after 2.2.0." For that reason you may choose to use a newer version of iText like 2.0.7+ and use ALLOW_COPY and ALLOW_PRINTING instead.

2. The tutorial states that an additional jar is required to run the example: bcprov-jdk14-138.jar. The sample code below seems to work with MX7 and CF8 without the use of any additional jars. Since MX7 uses iText for the cfdocument tag, which provides a 40-bit and 128-bit encryption option, I can only assume the necessary encryption classes are built-into CF. Though I do not know which classes are used to perform the encryption. If anyone has any insights on this topic, feel free to leave a comment.


What you will need for this example


A sample PDF from the iText site: ChapterSection.pdf. Download the file and place it in the same directory as your .cfm script.


Code


Documentation: Manipulating existing PDF documents
Source: EncryptorExample.java

MX7/CF8 Compatible Code

<h1>Encrypt an existing PDF Example</h1>
<cfscript>
savedErrorMessage = "";

// cfSearching: All file paths are relative to the current directory
fullPathToInputFile = ExpandPath("./ChapterSection.pdf");
fullPathToOutputFile = ExpandPath("./Encrypted.pdf");
userPassword = "Hello";
ownerPassword = "World";
//cfSearching: If true, use 128-bit key length. Otherwise, use 40-bit key
useStrength128Bits = true;

try {
string = createObject("java", "java.lang.String");
pdfReader = createObject("java", "com.lowagie.text.pdf.PdfReader").init(fullPathToInputFile);
pdfWriter = createObject("java", "com.lowagie.text.pdf.PdfWriter");
pdfEncryptor = createObject("java", "com.lowagie.text.pdf.PdfEncryptor");
outStream = createObject("java", "java.io.FileOutputStream").init(fullPathToOutputFile);
//cfSearching: This example uses deprecated constants. See the API for more information
//cfSearching: http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfWriter.html#AllowCopy
pdfEncryptor.encrypt( pdfReader,
outStream,
javacast("string", userPassword).getBytes(),
javacast("string", ownerPassword).getBytes(),
BitOr(PdfWriter.AllowCopy , PdfWriter.AllowPrinting),
useStrength128Bits);

WriteOutput("Finished!");
}
catch (java.language.Exception de) {
savedErrorMessage = de;
}
// cfSearching: close output stream object
if (IsDefined("outputStream")) {
outputStream.close();
}
</cfscript>


<!--- show any errors --->
<cfif len(savedErrorMessage) gt 0>
Error. Unable to create file
<cfdump var="#savedErrorMessage#">
</cfif>


Using JavaLoader and iText 2.0.7+ Code
Note, the code below uses an instance of the javaLoader stored in server scope as explained here

<h1>Encrypt an existing PDF Example</h1>
<cfscript>
savedErrorMessage = "";

// cfSearching: All file paths are relative to the current directory
fullPathToInputFile = ExpandPath("./ChapterSection.pdf");
fullPathToOutputFile = ExpandPath("./Encrypted.pdf");
userPassword = "Hello";
ownerPassword = "World";
//cfSearching: If true, uses 128-bit key length. Otherwise, uses 40-bit key
useStrength128Bits = true;

//cfSearching: uses an instance of javaloader stored in server scope
javaLoader = server[MyUniqueKeyForJavaLoader];

try {
string = createObject("java", "java.lang.String");
pdfReader = javaLoader.create("com.lowagie.text.pdf.PdfReader").init(fullPathToInputFile);
pdfWriter = javaLoader.create("com.lowagie.text.pdf.PdfWriter");
pdfEncryptor = javaLoader.create("com.lowagie.text.pdf.PdfEncryptor");
outStream = createObject("java", "java.io.FileOutputStream").init(fullPathToOutputFile);
//cfSearching: This example uses new constants available in iText 2.0.7+
pdfEncryptor.encrypt( pdfReader,
outStream,
javacast("string", userPassword).getBytes(),
javacast("string", ownerPassword).getBytes(),
BitOr(PdfWriter.AllowCopy , PdfWriter.AllowPrinting),
useStrength128Bits);

WriteOutput("Finished!");
}
catch (java.language.Exception de) {
savedErrorMessage = de;
}
// cfSearching: close output stream object
if (IsDefined("outputStream")) {
outputStream.close();
}
</cfscript>


<!--- show any errors --->
<cfif len(savedErrorMessage) gt 0>
Error. Unable to create file
<cfdump var="#savedErrorMessage#">
</cfif>

0 comments:

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Header image adapted from atomicjeep