After calling the method Document.UpdateFields some fields loose there input value. I included a sample file to reproduce the behavior.
If I first call NormalizeFieldTypes() before UpdateFields() then it works as expected but this was never needed before and the conditions are unclear. Should this always be called prior. the file is protected but with empty password.
nodes before:
Paragraph
FieldStart
BookmarkStart name=‘Text2’
Value FORMTEXT tejst
BookmarkStart
Run text=’ FORMTEXT ’
FormField fieldType=‘FieldFormTextInput’ result=‘tejst’
FieldSeparator
Run text=‘tejst’
FieldEnd
BookmarkEnd name=‘Text2’
Run text=’ is een tekstveld’
Paragraph
after:
Paragraph
FieldStart
BookmarkStart name=‘Text2’
Value FORMTEXT
BookmarkStart
Run text=’ FORMTEXT ’
FormField fieldType=‘FieldFormTextInput’ result=‘’
FieldSeparator
SpecialChar customNodeID=‘0’
SpecialChar customNodeID=‘0’
SpecialChar customNodeID=‘0’
SpecialChar customNodeID=‘0’
SpecialChar customNodeID=‘0’
FieldEnd
BookmarkEnd name=‘Text2’
Run text=’ is een tekstveld’
Paragraph
It sounds like you’re experiencing an issue where some form fields lose their input values after calling the Document.UpdateFields method. This can occur if the fields are being updated to reflect new data or formatting, which may inadvertently clear the existing values.
Cause: The loss of input values typically happens when the UpdateFields method refreshes the fields, potentially overwriting user-entered data.
Solution: To address this, you can implement the following steps:
Preserve Field Values: Before calling UpdateFields, store the current values of the form fields in a dictionary or similar structure. After updating the fields, you can reassign these values back to the respective fields.
Check Field Types: Ensure that the fields you are updating are of the correct type and that they support retaining values after an update. Some field types may not retain user input.
Use UpdateFields Carefully: If you only need to update specific fields, consider using more targeted methods instead of updating all fields at once. This can help prevent unintended changes to other fields.
Here’s a minimal code example to illustrate how you might preserve and restore field values:
Document doc = new Document("YourDocument.docx");
doc.UpdateFields();
// Store current values
Dictionary<string, string> fieldValues = new Dictionary<string, string>();
foreach (Field field in doc.Range.Fields)
{
if (field.Type == FieldType.FieldFormTextInput)
{
fieldValues[field.Code.ToString()] = field.Result;
}
}
// Update fields
doc.UpdateFields();
// Restore values
foreach (var entry in fieldValues)
{
Field field = doc.Range.Fields.Cast<Field>().FirstOrDefault(f => f.Code.ToString() == entry.Key);
if (field != null)
{
field.Result = entry.Value;
}
}
doc.Save("UpdatedDocument.docx");
Next Steps: Implement the above code in your project to preserve and restore the input values in your form fields after updating them.
If you have any further questions or need additional assistance, please let me know!
@fhellemond When Document.UpdateFields method is called field values are recalculated and form fields values are reset to their original state. This is an expected behavior. You will observe the same if update fields in MS Word.
To preserve the values you need to lock form fields. Please see the following code:
Document doc = new Document("C:\\Temp\\in.docx");
// Lock form fields.
foreach (Field f in doc.Range.Fields)
{
if (f.Type == FieldType.FieldFormTextInput ||
f.Type == FieldType.FieldFormDropDown ||
f.Type == FieldType.FieldFormCheckBox)
f.IsLocked = true;
}
doc.UpdateFields();
doc.Save("C:\\Temp\\out.docx");
Dear Alexey. Thank you for providing the code sample to stop these fields from updating. The NormalizeFieldTypes() had no effects and was a miss assumption from my end. Until Aspose version 24.7 we didn’t need to lock individual fields like this and when looking at the nodes I saw something strange after having called UpdateFields. Nodes of type SpecialChar why I made a ticket to review. I will use the option to lock the fields so the won’t get updated.