InsertField

InsertField(FieldType, bool, Node, bool)

Inserts a field into this paragraph.

public Field InsertField(FieldType fieldType, bool updateField, Node refNode, bool isAfter)
ParameterTypeDescription
fieldTypeFieldTypeThe type of the field to insert.
updateFieldBooleanSpecifies whether to update the field immediately.
refNodeNodeReference node inside this paragraph (if refNode is null, then appends to the end of the paragraph).
isAfterBooleanWhether to insert the field after or before reference node.

Return Value

A Field object that represents the inserted field.

Examples

Shows various ways of adding fields to a paragraph.

Document doc = new Document();
Paragraph para = doc.FirstSection.Body.FirstParagraph;

// Below are three ways of inserting a field into a paragraph.
// 1 -  Insert an AUTHOR field into a paragraph after one of the paragraph's child nodes:
Run run = new Run(doc) { Text = "This run was written by " };
para.AppendChild(run);

doc.BuiltInDocumentProperties["Author"].Value = "John Doe";
para.InsertField(FieldType.FieldAuthor, true, run, true);

// 2 -  Insert a QUOTE field after one of the paragraph's child nodes:
run = new Run(doc) { Text = "." };
para.AppendChild(run);

Field field = para.InsertField(" QUOTE \" Real value\" ", run, true);

// 3 -  Insert a QUOTE field before one of the paragraph's child nodes,
// and get it to display a placeholder value:
para.InsertField(" QUOTE \" Real value.\"", " Placeholder value.", field.Start, false);

Assert.AreEqual(" Placeholder value.", doc.Range.Fields[1].Result);

// This field will display its placeholder value until we update it.
doc.UpdateFields();

Assert.AreEqual(" Real value.", doc.Range.Fields[1].Result);

doc.Save(ArtifactsDir + "Paragraph.InsertField.docx");

See Also


InsertField(string, Node, bool)

Inserts a field into this paragraph.

public Field InsertField(string fieldCode, Node refNode, bool isAfter)
ParameterTypeDescription
fieldCodeStringThe field code to insert (without curly braces).
refNodeNodeReference node inside this paragraph (if refNode is null, then appends to the end of the paragraph).
isAfterBooleanWhether to insert the field after or before reference node.

Return Value

A Field object that represents the inserted field.

Examples

Shows various ways of adding fields to a paragraph.

Document doc = new Document();
Paragraph para = doc.FirstSection.Body.FirstParagraph;

// Below are three ways of inserting a field into a paragraph.
// 1 -  Insert an AUTHOR field into a paragraph after one of the paragraph's child nodes:
Run run = new Run(doc) { Text = "This run was written by " };
para.AppendChild(run);

doc.BuiltInDocumentProperties["Author"].Value = "John Doe";
para.InsertField(FieldType.FieldAuthor, true, run, true);

// 2 -  Insert a QUOTE field after one of the paragraph's child nodes:
run = new Run(doc) { Text = "." };
para.AppendChild(run);

Field field = para.InsertField(" QUOTE \" Real value\" ", run, true);

// 3 -  Insert a QUOTE field before one of the paragraph's child nodes,
// and get it to display a placeholder value:
para.InsertField(" QUOTE \" Real value.\"", " Placeholder value.", field.Start, false);

Assert.AreEqual(" Placeholder value.", doc.Range.Fields[1].Result);

// This field will display its placeholder value until we update it.
doc.UpdateFields();

Assert.AreEqual(" Real value.", doc.Range.Fields[1].Result);

doc.Save(ArtifactsDir + "Paragraph.InsertField.docx");

See Also


InsertField(string, string, Node, bool)

Inserts a field into this paragraph.

public Field InsertField(string fieldCode, string fieldValue, Node refNode, bool isAfter)
ParameterTypeDescription
fieldCodeStringThe field code to insert (without curly braces).
fieldValueStringThe field value to insert. Pass null for fields that do not have a value.
refNodeNodeReference node inside this paragraph (if refNode is null, then appends to the end of the paragraph).
isAfterBooleanWhether to insert the field after or before reference node.

Return Value

A Field object that represents the inserted field.

Examples

Shows various ways of adding fields to a paragraph.

Document doc = new Document();
Paragraph para = doc.FirstSection.Body.FirstParagraph;

// Below are three ways of inserting a field into a paragraph.
// 1 -  Insert an AUTHOR field into a paragraph after one of the paragraph's child nodes:
Run run = new Run(doc) { Text = "This run was written by " };
para.AppendChild(run);

doc.BuiltInDocumentProperties["Author"].Value = "John Doe";
para.InsertField(FieldType.FieldAuthor, true, run, true);

// 2 -  Insert a QUOTE field after one of the paragraph's child nodes:
run = new Run(doc) { Text = "." };
para.AppendChild(run);

Field field = para.InsertField(" QUOTE \" Real value\" ", run, true);

// 3 -  Insert a QUOTE field before one of the paragraph's child nodes,
// and get it to display a placeholder value:
para.InsertField(" QUOTE \" Real value.\"", " Placeholder value.", field.Start, false);

Assert.AreEqual(" Placeholder value.", doc.Range.Fields[1].Result);

// This field will display its placeholder value until we update it.
doc.UpdateFields();

Assert.AreEqual(" Real value.", doc.Range.Fields[1].Result);

doc.Save(ArtifactsDir + "Paragraph.InsertField.docx");

See Also