Get some checkbox field properties from a Dynamic PDF file

Hi,


I am working on a dynamic XFA PDF file with checkbox fields. I know I cannot get their properties like positions, checkedValue, FullName using Aspose.PDF.InteractiveFeatures.Forms.CheckboxField.Rect, Aspose.PDF.InteractiveFeatures.Forms.CheckboxField.States and Aspose.PDF.InteractiveFeatures.Forms.CheckboxField.FullName because they are only valid for static fields. So is there any example or idea how to get those properties straightly from an XFA field? Thanks in advance!

Hi Arashi,


Thanks for contacting support and sorry for the delayed response.

Please visit the following link for required information on Working with XFA Forms.

In case you encounter any issue, please share the resource file, so that we can test the scenario in our environment.

Hi , I have read that one but I am still confused. Can you code it for me how to get checkboxfield.States or checkboxfield.FullNames? Thanks!

Hi Arashi,


Please try using the following code lines to get the names of all the XFA fields inside PDF form.

[C#]

//open document<o:p></o:p>

Document pdfDocument = new Document(myDir + "BCTR Main.pdf");

foreach (string field in pdfDocument.Form.XFA.FieldNames)

{

Console.WriteLine("field name is :{0}", field);

// Console.ReadKey();

}


Currently you cannot get the state value but you can set the state/value for fields inside XFA form.

[C#]

pdfDocument.Form.XFA[“Form[0].Header[0].FilingName[0]”]
= “its a test value.”;<o:p></o:p>

//Checkbox field

pdfDocument.Form.XFA["Form[0].Header[0].TypeOfFiling[0].initialReport[0]"] = "";

// enable check box

pdfDocument.Form.XFA["Form[0].AmountandType[0].TransactionType[0].TransTypes[0].Car[0]"] = "1";

//save updated document

pdfDocument.Save(myDir + "XFAForm_out.pdf");


In case you encounter any issue, please share your input/resource file.

Hi,

My code is like:

Aspose.Pdf.Document doc = new Aspose.Pdf.Document(pdfFile, pw);
Aspose.Pdf.InteractiveFeatures.Forms.Form form= doc.Form;
foreach (object field in form) {
...........
}

And my test PDF is attached.

Actually my problem is that, when I use the codes above. In that foreach loop, I expect object field to be some type like RadioButtonField, TextBoxField. And it works fine if a PDF don't contain any XFA forms. But I find if my input PDF document has XFA contents(like the one I have attached). The FieldType of field object is not what I want. Their field type is correct in form(code line 2), and when you watch the Fields array in form, all the properties and Type like FieldType is correct, the only difference from a static PDFis that its XFA entry is not null; But when it goes into the foreach loop, every field object becomes Aspose.PDF.InteractiveFeatures.Forms.Field type, not RadioButtonField, TextBoxField ....type, so I am not able to cast field to the proper type I want. At the same time, some properties in derived class are also lost because everything is base class Aspose.PDF.InteractiveFeatures.Forms.Field now. That's why I am asking if XFA PDF has a different way to get their field properties.

So I am now looking for two ways to solve this problem. One is to find out why those correct Fields information in form in not correct when they are assigned in an foreach loop. Or if I know it is a XFA form, is there a alternative way to get information from its XFA entry. It would be great if you can use my codes and PDF to do some test and let me know what's wrong and how I have solve it. Thanks a lot!


Hi Arashi,


Thanks for sharing the details.

As per my understanding, you need to get the XFA field names, Type and their respective values. Therefore I am working on preparing the required code snippet and will share in this forum thread.

Yes, that is what I want:) And it is better if there is a general way to get other properties so I can add other functions if I need them in the future. Also I am wondering why in that foreach loop, objects are not derived field type but base field type. Thanks.

Hi,


Thanks for the acknowledgement.

I am working on preparing the required code snippet and will get back to you soon.

Thanks. Hope to see your snippet soon.

Hi Arashi,


I am afraid currently we do not have the feature to get properties associated with XFA field. However for the sake of correction, I have logged this problem as PDFNEWNET-39170 in
our issue tracking system. We will further look into the details of this
problem and will keep you updated on the status of correction. Please be
patient and spare us little time. We are sorry for this inconvenience.

Hi, are there any updates on this?

Hi Arashi,


Thanks for your patience.

As we recently have logged this requirement, so its is still pending for review, as the team has been busy fixing previously reported issues. As soon as we have made some definite progress towards its implementation, we will let you know.

Your patience and comprehension is greatly appreciated in this regard.

Hi, is there any update on this issue?

Hi Arashi,


Thanks for your patience.

The earlier reported issue is still pending for review and I am afraid its not yet resolved. Please note that product team is currently busy fixing previously reported issues and as soon as we have some further updates, we will let you know.

Hi,


As regular, is there any update on this, thanks!

Hi Arashi,


Thanks for your inquiry. I am afraid the reported issue is still not resolved as product team is busy in resolving other issues in the queue, reported earlier. However I have raised the issue priority of your above reported issue and requested our team to share an ETA at their earliest. We will notify you as soon as we made some significant progress towards issue resolution.

We are sorry for the inconvenience caused.

Best Regards,

Hi Arashi,


Thanks for your patience.

We have further investigated the issue reported earlier and since the document which you have shared contains static XFA form fields, that is why its fields may be accessed using both Document.Form (as for standard form), and via XFA.

You also need to take into consideration that form has hierarchical structure and that’s why it requires recursive scan of the field. If field contains subfields, they should be iterated.

Simple example:
[C#]

private void ScanFields(string rootName, Field
field)<o:p></o:p>

{

foreach (Field subField in field)

{

//if this field does not contain subfield, it is "terminal" field and we should get its type and value.

//RadioButton field is "special case", it contains subfiels (we can access to every radio button option as different field) but we should query value from RadioButton field in order to check which exactpy option is checked.

if (subField.Count == 0 || subField is RadioButtonField))

{

Console.WriteLine(rootName + "." + subField.PartialName + " type = " + subField.GetType() + " value = " + subField.Value);

}

//else we call this method recursively for each subfield.

else

{

ScanFields(rootName + "." + subField.PartialName, subField);

}

}

}

public void ShowFields()

{

Document doc = new Document("RadioButtons.pdf");

foreach (Field field in doc.Form)

{

ScanFields("", field);

}

}


In order to determine field type, you should check field template which can be retrieved using GetFieldTempate method.

Field template is XML which have the following form <$fieldtype>… </$fieldtype> where $fieldtype is one of field types, for example textEdit, checkBox etc.
For example …

[C#]

Document doc = new
Document(“RadioButtons.pdf”);<o:p></o:p>

foreach (string field in doc.Form.XFA.FieldNames)

{

XmlNode tmpl = doc.Form.XFA.GetFieldTemplate(field);

Console.WriteLine(field + " type = " + tmpl.ChildNodes[0].ChildNodes[0].Name + " value = " + doc.Form.XFA[field]);

}

Hi Nayyer,

Thanks for the snippet and detailed comments. I really appreciate that. Now I can easily get correct field types. And I also modified the code a little.

[C#]

private void ScanFields(Field field)

{

foreach (Field subField in field)

{

if (subField.Count == 0 || subField is RadioButtonField))

{

var test = subField.PageIndex; //what I have modified.

var test2 = subField.AnnotationIndex;

}

//else we call this method recursively for each subfield.

else

{

ScanFields( subField);

}

}

}

public void ShowFields()

{

Document doc = new Document("CanWriteField.pdf");

foreach (Field field in doc.Form)

{

ScanFields(field);

}

}

In most cases the results of PageIndex and AnnotationIndex are correct, but I always get an IndexOutOfRangeException exception and in a few cases subField.PageIndex will return 0. I think the first exception is related to this thread:

https://forum.aspose.com/t/28488

And the second question will occur if you use the new pdf file I have attached. As far as I know subField.PageIndex should start at 1 not 0, but if you use CanWriteField.pdf you will find a RadioButtonField has a PageIndex property with value equals to 0.

I am not sure if my codework is corrcet. Please let me know if I need to improve my code or wait for some fixes from Aspose. Thanks again!





Hi Arashi,


Thanks for sharing the details.

I have tested the scenario using code snippet and resource file you have shared in your earlier post and have managed to reproduce IndexOutOfRangeException. As per my observations, the PageIndex value is properly being returned but an exception occurs when trying to read value for RadioButton field. For the sake of correction, I have logged this problem
as PDFNEWNET-39566 in our issue tracking system. We will
further look into the details of this problem and will keep you updated on the
status of correction. Please be patient and spare us little time. We are sorry
for this inconvenience.

Following is the output in console generated over my end.

  • Partial Name = name[0] type = Aspose.Pdf.InteractiveFeatures.Forms.TextBoxField value = Page Index1 Annotation Index1
  • Partial Name = address[0] type = Aspose.Pdf.InteractiveFeatures.Forms.TextBoxField value = Page Index1 Annotation Index2
  • Partial Name = postal_code[0] type = Aspose.Pdf.InteractiveFeatures.Forms.TextBoxField value = Page Index1 Annotation Index3
  • Partial Name = email[0] type = Aspose.Pdf.InteractiveFeatures.Forms.TextBoxField value = Page Index1 Annotation Index4
  • Partial Name = programming[0] type = Aspose.Pdf.InteractiveFeatures.Forms.ListBoxField value = Page Index1 Annotation Index5
  • Partial Name = language[0] type = Aspose.Pdf.InteractiveFeatures.Forms.ComboBoxField value = Page Index1 Annotation Index6

Yes. For the second problem( PDFNEWNET-39566) , I have also tried many different PDFs, in most cases, PageIndex = 0 occurs with a RadioButtonField (and in my code it is not an exception but only makes PageIndex = 0).


As for the first problem, I think there is a way to work around it temporary. Now, it only happens to a RadioButtonField with at least one RadioButtonOptionField , and every RadioButtonOptionField has a valid AnnotationIndex. Thus I can choose any of the RadioButtonOptionFields to represent Annotation index of that RadioButtonField. This works fine for me so far. So I was wondering if the second problem is also related to some codes in RadioButtonOptionField/RadioButtonField?

And by the way, we have RadioButtonOptionField for RadioButtonField. Are there any similar fields (something like CheckBox***Field)? Thanks!