| By Ben Litchfield | Article Rating: |
|
| March 24, 2005 12:00 AM EST | Reads: |
62,206 |
Encryption/Decryption
A popular PDF feature allows for encrypting document contents and setting access controls limiting who can view the unencrypted document. Specifically, a PDF document is encrypted with a master password and optionally a user password. If a user password has been provided, then a PDF reader such as Acrobat will prompt for a password before letting the document be viewed. The master password is required to change document permissions.
The PDF specification lets creators of PDF documents restrict certain operations when viewing the PDF in Acrobat. Some of the available document restrictions are:
- Printing
- Changing content
- Extracting text
To encrypt a document, it must first be assigned a security handler and then encrypted with a master password and user password. For example, the following code encrypts a document so a user can open it in Acrobat without entering a password (i.e., no user password), but can't print the document using the access control mechanism.
//load the document
PDDocument pdf =
PDDocument.load( "test.pdf" );
//create the encryption options
PDStandardEncryption encryptionOptions =
new PDStandardEncryption();
encryptionOptions.setCanPrint( false );
pdf.setEncryptionDictionary(
encryptionOptions );
//encrypt the document
pdf.encrypt( "master", null );
//save the encrypted document
//to the file system
pdf.save( "test-output.pdf");
For a more complete example, reference the source code for the encryption utility included in the PDFBox distribution: org.pdfbox.Encrypt.
Many applications can generate PDF documents but don't allow control over the document's security options. PDFBox can be used here to intercept and encrypt the PDF before it's sent to the user.
Form Integration
When an application's output is a series of form field values, it is usually desirable to let the user save the form for record keeping. PDF technology is a great choice for this kind of output. A developer can write code to output PDF instructions manually to draw images, tables and text. Or encapsulate the data in XML and use an XSL-FO engine to create a PDF document. However, these approaches can be time-consuming, error-prone and inflexible. A better approach for simple forms might be to create a template and generate a filled-in document for any given set of input data based on the template.
A form many of us may be familiar with is the Employment Eligibility Verification, or I-9 form: http://uscis.gov/graphics/formsfee/forms/files/i-9.pdf
Using one of the example applications distributed with PDFBox, the form field names can be listed:
java org.pdfbox.examples.fdf.PrintFields i-9.pdf
Another example utility populates a given field with textual data:
java org.pdfbox.examples.fdf.SetField i-9.pdf NAME1 Smith
Opening the PDF document in Acrobat shows that the "Last Name" field has been filled in. This functionality can be recreated in code:
PDDocument pdf =
PDDocument.load( "i-9.pdf" );
PDDocumentCatalog docCatalog =
pdf.getDocumentCatalog();
PDAcroForm acroForm =
docCatalog.getAcroForm();
PDField field =
acroForm.getField( "NAME1" );
field.setValue( "Smith" );
pdf.save( "i-9-copy.pdf" );
It's also possible to extract the values of a form field that has been previously populated, as below:
PDField field =
acroForm.getField( "NAME1" );
System.out.println(
"First Name=" + field.getValue() );
Acrobat offers the option of exporting and importing form data in a special file format called "Forms Data Format." These files come in two flavors, FDF and XFDF. An FDF stores the form data in the same format as PDF, while XFDF stores data in XML format. PDFBox handles both FDF and XFDF data with a single object: FDFDocument. The following snippet shows how to export FDF data for the I-9 form above:
PDDocument pdf =
PDDocument.load( "i-9.pdf" );
PDDocumentCatalog docCatalog =
pdf.getDocumentCatalog();
PDAcroForm acroForm =
docCatalog.getAcroForm();
FDFDocument fdf = acroForm.exportFDF();
fdf.save( "exportedData.fdf" );
PDFBox Form Integration Steps
- Create PDF Form Template using Acrobat or other visual tool
- Track the name of each desired form field
- Store the template PDF where the application can access it
- When the PDF is requested, use PDFBox to parse the template PDF
- Populate the required form fields
- Stream the PDF back to the user
Besides the library APIs mentioned above, PDFBox also has a set of command-line utilities. Table 2 lists the class name of each utility along with a short description.
Remarks
The PDF specification weighs in at 1,172 pages so implementing it is quite an undertaking. As such, PDFBox is distributed with the proviso that it is a work in progress, with new features being added over time. Its main weakness is in creating PDF documents from scratch. However, there are several other Open Source Java projects that can be used to fill the gap. For instance, the Apache FOP project lets programmers generate a PDF from a specialized XML document that describes the PDF document. Also, iText provides a high-level API for creating document elements such as tables and lists.
The next version of PDFBox will add support for the new PDF 1.5 object stream and cross-reference streams. After that will be support for embedding fonts and images. Hopefully through efforts like PDFBox, robust support for PDF technology can be made available for Java applications.
References
Published March 24, 2005 Reads 62,206
Copyright © 2005 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Ben Litchfield
Ben Litchfield is a business systems consultant within the development & integration practice at LPA Systems. He has been the lead developer of PDFBox for the past two years. Ben holds a BS in Software Engineering from the Rochester Institute of Technology. He has been providing solutions for enterprise applications for the past five years.
![]() |
Lucious 03/23/05 02:27:55 PM EST | |||
I can't believe I found this!! I was searching for tools to update pdf files in my java programs. All I could find was commercial tools that would charge for single/multiple CPU and development liscenses and then charge MORE for deployment!! I actually gave up and downloaded the pdf specs (over 1200 pages) to develop tools of my own. I can't wait to start using these tools! |
||||
![]() |
Maulik 03/23/05 08:09:43 AM EST | |||
Great easy-to-follow article for someone who knows next to nothing about integrating Java and PDF. Good job. |
||||
![]() |
Richard Bouchard 03/12/05 09:15:57 PM EST | |||
Excellent article and has great utility. |
||||
- Kindle 2 vs Nook
- Why IBM’s Server Chief Got Busted
- Is Cloud Computing Like Teenage Sex?
- Industry Experts Discuss the State of Cloud Computing
- Performance Tuning Essentials for Java
- Confessions of a Ulitzer Addict
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- It's the Java vs. C++ Shootout Revisited!
- Cloud Computing Can Revitalize Your Career as Software Developer
- IBM Could "Reinvent" Java: Mills
- Oracle & Cloud Computing: Exclusive Q&A with SVP Richard Sarwal
- A Brief History of Cloud Computing
- Kindle 2 vs Nook
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Why IBM’s Server Chief Got Busted
- Is Cloud Computing Like Teenage Sex?
- Industry Experts Discuss the State of Cloud Computing
- Performance Tuning Essentials for Java
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- The i-Technology Right Stuff
- JavaServer Faces (JSF) vs Struts
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- What's New in Eclipse?
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- i-Technology Predictions for 2007: Where's It All Headed?


































