Fields in Blazor DocumentEditor Component
8 Jul 20241 minute to read
Document Editor has preservation support for all types of fields in an existing word document without any data loss.
Adding Fields
You can add a field to the document by using InsertFieldAsync
method in EditorModule
.
The following example code illustrates how to insert merge field programmatically by providing the field code and field result.
string fieldCode = "MERGEFIELD First Name \\* MERGEFORMAT ";
string fieldResult = "«First Name»";
await container.DocumentEditor.Editor.InsertFieldAsync(fieldCode, fieldResult);
NOTE
Document editor does not validate/process the field code/field result. it simply inserts the field with specified field information.
Update fields
Document Editor provides support for updating bookmark cross reference field. The following example code illustrates how to update bookmark cross reference field.
//Update all the bookmark cross reference field in the document.
await container.DocumentEditor.UpdateFieldsAsync();
Bookmark cross reference fields can be updated through UI by using update fields option in Toolbar
.
The following type of fields are automatically updated in Document Editor.
- NUMPAGES
- SECTION
- PAGE
Get field info
You can get field code and field result of the current selected field by using GetFieldInfoAsync
method in the SelectionModule
.
//Gets the field information of the selected field.
FieldInfo fieldInfo = await container.DocumentEditor.Selection.GetFieldInfoAsync();
NOTE
For nested fields, this method returns combined field code and result.
Set field info
You can modify the field code and field result of the current selected field by using SetFieldInfoAsync
method in the EditorModule
.
//Gets the field information for the selected field.
FieldInfo fieldInfo = await container.DocumentEditor.Selection.GetFieldInfoAsync();
//Modify field code
fieldInfo.Code = "MERGEFIELD First Name \\* MERGEFORMAT ";
//Modify field result
fieldInfo.Result = "«First Name»";
//Modify field code and result of the current selected field.
await container.DocumentEditor.Editor.SetFieldInfoAsync(fieldInfo);
NOTE
For nested field, entire field gets replaced completely with the specified field information.