Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect dynamic XFA with iText

Tags:

pdf

itext

xfa

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.

like image 675
Abe Voelker Avatar asked Sep 06 '25 04:09

Abe Voelker


1 Answers

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.");
}
like image 187
Moe Howard Avatar answered Sep 11 '25 01:09

Moe Howard