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!
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!
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.
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.
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.
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.
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 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#]
privatevoid 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 isRadioButtonField))
{
Console.WriteLine(rootName + "." + subField.PartialName + " type = " + subField.GetType() + " value = " + subField.Value);
}
//else we call this method recursively for each subfield.
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>
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#]
privatevoid ScanFields(Field field)
{
foreach (Field subField in field)
{
if (subField.Count
== 0 || subField isRadioButtonField))
{
var test
= subField.PageIndex; //what I have modified.
var test2 = subField.AnnotationIndex;
}
//else we call this method
recursively for each subfield.
else
{
ScanFields( subField);
}
}
}
publicvoid ShowFields()
{
Document doc = newDocument("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:
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!
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!