Hi,
Hi Arashi,
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
Document pdfDocument = new Document(myDir + "BCTR Main.pdf");
foreach (string field in pdfDocument.Form.XFA.FieldNames) {
Console.WriteLine("field Printfield 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.";
//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 Arashi,
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. Hope to see your snippet soon.
Hi Arashi,
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,
Hi, is there any update on this issue?
Hi Arashi,
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,
Hi Arashi,
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){
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 <textEdit></textEdit>
[C#]
Document doc = new Document("RadioButtons.pdf");
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
{
//else we call this method recursively for each subfield.
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.
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 code work is correct. Please let me know if I need to improve my code or wait for some fixes from Aspose. Thanks again!
Hi Arashi,
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.
- 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).