Fields in RadFlowDocument consist of code fragment and result fragment as explained in the Fields article. Some fields have direct representation in the document model – for example Hyperlinks. For all other fields you can use the CustomCodeField class – however you will need some knowledge of how to correctly form the code of the field.
Here is the basic syntax of a field code:
field-type [field-argument] [switches]
field-type: The type of the field. For example: HYPERLINK.
argument: The argument of the field. This is optional as some of the fields do not require an argument.
switches: Additional properties of the field. More information on the topic can be found in the Fields article.
The suggested approach for inserting code fields is by using RadFlowDocumentEditor. The InsertField() method accepts code as a first argument and the result as a second argument.
Here are some commonly used fields. The complete list of field codes and the switches for each of them can be found in the Docx specification.
In all examples the result fragment is also inserted. However, if you export the document to Docx format, you can make use of the AutoUpdateFields property. It forces all fields to be updated when the document is opened in MS Word or another editor.
Example 1 shows how to insert a PAGE field representing the current page number in the document.
[C#] Example 1: Insert PAGE field
editor.InsertField("PAGE \\* ROMAN", "VII");
The * ROMAN is general formatting switch that formats a numeric result using uppercase Roman numerals.
Example 2 demonstrates how a combination of PAGE and NUMPAGES fields can be inserted to show which is the current page as well as the total page count in the document.
[C#] Example 2: Insert NUMPAGES field
editor.InsertText("Page "); editor.InsertField("PAGE", "3"); editor.InsertText(" of "); editor.InsertField("NUMPAGES", "5");
In Example 3 is demonstrated how to insert AUTHOR field showing the name of the author of the document.
[C#] Example 3: Insert AUTHOR field
editor.InsertField("AUTHOR \\* Upper", "JOHN DOE");
The \\* Upper switch will convert all letters in the result to uppercase.
Example 4 shows how to insert a table of contents (TOC) field.
[C#] Example 4: Insert Table of Contents field
FieldInfo tocField = editor.InsertField("TOC \\o \"1-3\" \\h \\z \\u", "result"); tocField.IsDirty = true;
There are several switches which can be used for this field:
\o "1-3": Specifies that the first 3 heading levels should be included in the table of contents.
\h: Makes the table of contents entries hyperlinks.
\z: Hides tab leader and page numbers in Web layout view.
\u: Uses the applied paragraph outline level.
The IsDirty property is set so that the TOC field is updated when the document is loaded inside an editor like Microsoft Word.