After calling the UpdateFields function, all form fields have their result set to an empty string.
When you call the UpdateFields
method in Aspose.Words, it is designed to update the results of fields in the document, which can sometimes lead to unexpected behavior with form fields. If you are experiencing an issue where all form fields have their results set to an empty string after calling this method, it may be due to how the fields are being processed.
Here are a few points to consider:
-
Field Structure: Each field in a Word document consists of multiple components, including the field code and the field result. When updating fields, ensure that the field codes are correctly defined and that they can generate valid results.
-
Form Field Properties: You can access and set the result of a form field using the
setResult(String value)
method. If the field is not properly configured or if the field code does not return a valid result, it may default to an empty string. -
Field Update Process: The
Document.updateFields()
method updates all fields in the document. If any field is marked as “dirty” (i.e., its result is stale due to changes in the document), it will be recalculated. If the field’s code does not yield a valid result, it may result in an empty output. -
Debugging: To troubleshoot this issue, you can check the field codes of your form fields before and after the update. Ensure that they are correctly set and that they can produce the expected results. You can also manually update individual fields using the
update()
method on the specific field if needed.
Here is a simple example of how to set and update a form field:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a text input form field
FormField textInput = builder.insertTextInput("TextInput1", TextFormFieldType.REGULAR, "Default text", "Placeholder", 0);
// Update fields in the document
doc.updateFields();
// Check the result of the form field
String result = textInput.getResult();
System.out.println("Form Field Result: " + result);
If the issue persists, consider checking the specific field codes and ensuring that they are valid and correctly formatted. You may also want to review the documentation for any additional details on handling form fields and their updates.
If you need further assistance, please provide more details about the specific form fields and their configurations.
An example form field that has the issue is a text form field. I have verified that there was no issue in Aspose.Words 24.7.0, but the issue exists in 24.8.0 and newer.
@hylantdevelopers This is an expected behavior. You can lock form fields in your document to avoid updating them. Please see the following code:
Document doc = new Document(@"C:\Temp\in.docx");
doc.Range.Fields.Where(f => f.Type == FieldType.FieldFormTextInput || f.Type == FieldType.FieldFormDropDown || f.Type == FieldType.FieldFormCheckBox)
.ToList().ForEach(f => f.IsLocked = true);
doc.UpdateFields();
doc.Save(@"C:\Temp\out.pdf");
Did the behavior change from 24.7.0 to 24.8.0 and beyond because it never used to clear the values out. Updating the field never removed the value before and that surely causes issues. Why is it expected to remove a value if the field is updated?
@hylantdevelopers This behavior reflects MS Word behavior while updating fields. If you update fields in MS Word, values of form fields are cleared. So Aspose.Words does the same.
That’s fair. Thanks for the clarification and assistance. We will make adjustments to our processes to take this new behavior into account.
Is there a way to update text form fields that have a calcuation type now? That is one of the reasons we used UpdateFields before. It does not appear UpdateFields is working for calculated form fields.
@hylantdevelopers Could you please attach your problematic input and output documents here for our reference? We will check the issue and provide you more information.
Aspose Words Form Field Test - Updated.docx (18.8 KB)
Aspose Words Form Field Test.docx (26.8 KB)
Attached are the test form and the result after trying to update the fields.
@hylantdevelopers To achieve what you need you should move the formula field outside the form field code. Please see the modified input and output document produced by the following code:
Document doc = new Document(@"C:\Temp\in.docx");
doc.Range.FormFields["Test1"].SetTextInputValue("2");
doc.Range.FormFields["Test2"].SetTextInputValue("3");
doc.Range.Fields.Where(f => f.Type == FieldType.FieldFormTextInput || f.Type == FieldType.FieldFormDropDown || f.Type == FieldType.FieldFormCheckBox)
.ToList().ForEach(f => f.IsLocked = true);
doc.UpdateFields();
doc.Save(@"C:\Temp\out.docx");
So, this solution requires changing all calculated form fields to a different type of field? If so, that will not be acceptable as there are too many documents to change. Microsoft Word has no problem updating the calculated form field in the document I provided, so that is the solution we are expecting. Also, previous versions of Aspose.Words would update calculated fields without issue.
@hylantdevelopers To get the expected output with your input document it is required to lock all form fields except form fields that must be calculated. Please try using the following code:
Document doc = new Document(@"C:\Temp\in.docx");
doc.Range.FormFields["Test1"].SetTextInputValue("2");
doc.Range.FormFields["Test2"].SetTextInputValue("3");
foreach (FormField ff in doc.Range.FormFields)
{
// Get parent field.
Field f = null;
Node nextNode = ff.NextSibling;
while (nextNode != null)
{
if (nextNode.NodeType == NodeType.FieldEnd)
{
f = ((FieldEnd)nextNode).GetField();
break;
}
nextNode = nextNode.NextSibling;
}
if (f == null)
continue;
// Lock form field if it's TextInputType is not calculated field.
if (ff.TextInputType != TextFormFieldType.Calculated)
f.IsLocked = true;
}
doc.UpdateFields();
doc.Save(@"C:\Temp\out.docx");
Aspose Words Form Field Test.docx (26.8 KB)
out.docx (18.8 KB)
Yes, this works! I greatly appreciate your quick assistance with this. Thank you!