4

I need to make pre-populated PDF/XFA forms read-only (as in no inputs, such as text, checkboxes, radio buttons etc. can have their values changed).

For regular AcroForms PDFs and static XFA forms, I can accomplish this by calling setFormFlattening(true) on the PdfStamper instance. For dynamic XFA forms, I have to set an access attribute of the XDP's field node to be readOnly.

The problem is, how do I detect if a form is dynamic XFA? isXfaPresent doesn't differentiate between static or dynamic XFA forms, so isn't useful.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Abe Voelker
  • 30,124
  • 14
  • 81
  • 98
  • @Bruno Sorry for the text confusion, thanks for all your hard work on iText! And if you see a license pricing estimate come through for CCAP soon, give us a good price! ;) – Abe Voelker Oct 30 '12 at 17:42
  • I don't answer sales requests, but I've sent a message to our sales people in Europe (assuming that you're based in Europe). – Bruno Lowagie Oct 30 '12 at 18:00
  • @Bruno Actually, I'm in America. Again, thanks for your hard work on iText (I'm familiar with the story on why you went commercial). – Abe Voelker Oct 30 '12 at 18:06

2 Answers2

4

To add to Bruno's answer and to provide C# example code:

PdfReader reader = new PdfReader(filePath);
XfaForm xfa = new XfaForm(reader);

//Check if PDF file contains Dynamic XFA data
if (xfa != null && xfa.XfaPresent && xfa.Reader.AcroFields.Fields.Keys.Count == 0)
{
   MessageBox.Show("This PDF contains Dynamic XFA data.");
}
Moe Howard
  • 498
  • 7
  • 12
3

iText is free as in free speech, not free as in free beer. Read http://lowagie.com/bumodels for more info about the business models we've tried in order to avoid switching from the MPL/LGPL to the AGPL.

Q1: making dynamic XFA forms read-only

That's a no-brainer with the most recent version of iText, but not supported in the obsolete version you're using. Please read http://lowagie.com/itext2 to find out why you shouldn't use iText 4.2.0 (which as far as I know isn't even an official release; and I know, because I'm responsible for every single iText release). Maybe you're talking about iTextSharp.

Q2: flattening dynamic XFA forms

If you want to flatten a dynamic XFA form, you have two options: either use Adobe LiveCycle ES (which will cost you an arm and a leg), or use iText's XFA Worker (which is a much less expensive closed source product built on top of the F/OSS iText). Given the cost and the number of man hours that went into this product, I don't think you'll find a solution that is free as in free beer. I for one don't know of any such product.

Q3: how to find out if an XFA form is dynamic

This is explained in my book, "iText in Action - Second Edition." You already have half of the solution. Condition 1: isXfaPresent() needs to return true. Condition 2: getFields() needs to have an empty key set. See the method readFieldnames() in this example.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • 1
    Extra info: **making read-only** = the PDF still acts as a container for XML. All you do, is changing a setting. People with technical skills can undo that change. **flattening** = remove all the XML syntax from the PDF and convert it into PDF syntax. All interactivity is lost. The form is no longer a form, but a flat PDF. – Bruno Lowagie Oct 30 '12 at 17:55
  • Just a note about the iText version - you're right that it's not an actual released version; I had originally found it based off of [this answer](http://stackoverflow.com/a/4544513/215168). As it's not a released version I understand it probably has bugs that are fixed in recent iText versions. – Abe Voelker Oct 30 '12 at 18:09
  • I am familiar with the XML Worker that you've since added to iText; we've had the exact necessity for that functionality for some time on a project and it's the reason I've asked my manager to explore a license for iText. It's awesome! Just a suggestion - I had signed up for the [beta list](http://itextpdf.com/themes/betalist.php) quite some time ago, but never got a notification email that the functionality got rolled into iText 5.3.3. Maybe you want to send a ping out on that list letting people know? – Abe Voelker Oct 30 '12 at 18:17
  • @AbeVoelker There actually is a Java release 4.2.0 in the iText SVN repository. This release is explicitly attributed "unofficial", though; it was used for synchronizing the C# and Java versions before the license change. While there are many issues in that revision which meanwhile have been resolved and even more features added, it implements a few fixes when compared whith the final 2.1.*... – mkl Oct 30 '12 at 19:20
  • @mkl Thanks for the info! I'm hoping that we'll get a license to the newer iText so it won't be an issue :-) – Abe Voelker Oct 30 '12 at 19:58
  • @AbeVoelker Getting a license to use the current version is indeed the way to go... (a hint for my employer... *sigh*) – mkl Oct 30 '12 at 21:31
  • Yes, we created a tag before making the jump from 2 to 5 ;-) – Bruno Lowagie Oct 31 '12 at 08:47
  • @mkl All in due time ;-) I've had a report from Frank's visit to you. I've already used some of your documentation when answering questions after the dry-run of my digital signatures presentation at Ghent University. That's another perk of being a customer: your company gets mentioned in documentation / presentations if you're doing something that is innovative and interesting. – Bruno Lowagie Oct 31 '12 at 08:50