DocumentBuilderInsertField Method (String, String)
Inserts a Word field into a document without updating the field result.

Namespace: Aspose.Words
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.10
Syntax
public Field InsertField(
	string fieldCode,
	string fieldValue
)

Parameters

fieldCode
Type: SystemString
The field code to insert (without curly braces).
fieldValue
Type: SystemString
The field value to insert. Pass null for fields that do not have a value.

Return Value

Type: Field
A Field object that represents the inserted field.
Remarks

Fields in Microsoft Word documents consist of a field code and a field result. The field code is like a formula and the field result is like the value that the formula produces. The field code may also contain field switches that are like additional instructions to perform a specific action.

You can switch between displaying field codes and results in your document in Microsoft Word using the keyboard shortcut Alt+F9. Field codes appear between curly braces ( { } ).

To create a field, you need to specify a field type, field code and a "placeholder" field value. If you are not sure about a particular field code syntax, create the field in Microsoft Word first and switch to see its field code.

Aspose.Words can calculate field results for most of the field types, but this method does not update the field result automatically. Because the field result is not calculated automatically, you are expected to pass some string value (or even an empty string) that will be inserted into the field result. This value will remain in the field result as a placeholder until the field is updated. To update the field result you can call Update on the field object returned to you or UpdateFields to update fields in the whole document.

Examples
Shows how to control page numbering per section.
[C#]

// This document has two sections, but no page numbers yet.
Document doc = new Document(MyDir + "PageSetup.PageNumbering.doc");

// Use document builder to create a header with a page number field for the first section.
// The page number will look like "Page V".
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToSection(0);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Write("Page ");
builder.InsertField("PAGE", "");

// Set first section page numbering.
Section section = doc.Sections[0];
section.PageSetup.RestartPageNumbering = true;
section.PageSetup.PageStartingNumber = 5;
section.PageSetup.PageNumberStyle = NumberStyle.UppercaseRoman;

// Create a header for the section. 
// The page number will look like " - 10 - ".
builder.MoveToSection(1);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Write(" - ");
builder.InsertField("PAGE", "");
builder.Write(" - ");

// Set second section page numbering.
section = doc.Sections[1];
section.PageSetup.PageStartingNumber = 10;
section.PageSetup.RestartPageNumbering = true;
section.PageSetup.PageNumberStyle = NumberStyle.Arabic;

doc.Save(ArtifactsDir + "PageSetup.PageNumbering.doc");
See Also