That depends on your definition of "Lock".
 
If you want them all set to "read-only", then no... not directly.  There are functions available to set a given field read-only, and a way to enumerate a PDFs fields.
 
AcroFields acFields = myPdfStamper.getAcroFields();
Map fieldMap = acFields.getFields();
Iterator keyIter = fieldMap.keySet().iterator();
while (keyIter.hasNext()) {
  acFields.setFieldProperty(
    (String) keyIter.next(),   // field name
    "setfflags",               // turn some bits on, leave the rest
    PdfFormField.FF_READ_ONLY, // turn on the read-only bit
    null );                    // all instances of this field
}
 
 
If you want them to be totally uneditable, I can think of three methods:
 
1) Flatten the PDF.  The strips out the fields themselves, and draws the fields and their values directly into the PDF page.  The appearance of a give page doesn't change... it still /looks/ like the fields are present with their values.  The down-side of flattening is that you can loose some data.  Anything outside the visible portion of the field is gone.  Multi-line text fields and lists that allow multiple selection are vulnerable to this... most other fields are not.
 
---
 
PdfStamper foo = ...
foo.setFormFlattening( true );
// and if you'd like to flatten only certain fields:
foo.partialFormFlattening( "bar" ); // only flatten bar, baz, and qux.
foo.partialFormFlattening( "baz" );
foo.partialFormFlattening( "qux" );
 
---
 
2) Sign the PDF.  PDF supports several kinds of signatures, including some that are keyed to particular form fields. I'm not sure that iText supports this particular kind of signature.  Let me check.  Nope.  You can still sign the document as a whole.
 
There's some sample code in the online API reference.  PdfStamper.createSignature has a block of 16 lines of code walking you through the process.  It assumes you have some 'infrastructure' in place to assist... You need to have a private key in a ".pfx" file.  Acrobat can create a "self-signed" (*I* say I'm me) private key, and a number of businesses will create a file for you for a fee (*they* say I'm me).
 
 
(that's quite a link)
 
Warning:  Signing a PDF doesn't make it impossible to change.  It makes it possible to retrieve older versions... an important difference.  The original data is always available, but you can't be sure that all users are aware of this.
 
 
3)  Encrypt the PDF with a blank "open" password, using the desired PdfWriter.Allow* permissions.  To really lock a PDF down, you probably want to turn off everything with the possible exception of PdfWriter.AllowScreenReaders (text to speach software will still work).
 
--Mark Storer
  Senior Software Engineer
  Cardiff Software

#include <disclaimer>
typedef std::Disclaimer<Cardiff> DisCard;

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]On Behalf Of Radek Pawlowski
Sent: Tuesday, May 16, 2006 8:25 AM
To: itext-questions@lists.sourceforge.net
Subject: [iText-questions] function to lock form fields inside the PDF

Is there a function that would lock all the form fields inside of the PDF document?

 

Sincerely

-Radek

 

 

 

Reply via email to