Field class

Field class

Represents a Microsoft Word document field. To learn more, visit the documentation article.

Remarks

A field in a Word document is a complex structure consisting of multiple nodes that include field start, field code, field separator, field result and field end. Fields can be nested, contain rich content and span multiple paragraphs or sections in a document. The Field class is a “facade” object that provides properties and methods that allow to work with a field as a single object.

The Field.start, Field.separator and Field.end properties point to the field start, separator and end nodes of the field respectively.

The content between the field start and separator is the field code. The content between the field separator and field end is the field result. The field code typically consists of one or more Run objects that specify instructions. The processing application is expected to execute the field code to calculate the field result.

The process of calculating field results is called the field update. Aspose.Words can update field results of most of the field types in exactly the same way as Microsoft Word does it. Most notably, Aspose.Words can calculate results of even the most complex formula fields. To calculate the field result of a single field use the Field.update() method. To update fields in the whole document use Document.updateFields().

You can get the plain text version of the field code using the Field.getFieldCode() method. You can get and set the plain text version of the field result using the Field.result property. Both the field code and field result can contain complex content, such as nested fields, paragraphs, shapes, tables and in this case you might want to work with the field nodes directly if you need more control.

You do not create instances of the Field class directly. To create a new field use the DocumentBuilder.insertField() method.

Properties

NameDescription
displayResultGets the text that represents the displayed field result.
endGets the node that represents the field end.
formatGets a FieldFormat object that provides typed access to field’s formatting.
isDirtyGets or sets whether the current result of the field is no longer correct (stale) due to other modifications made to the document.
isLockedGets or sets whether the field is locked (should not recalculate its result).
localeIdGets or sets the LCID of the field.
resultGets or sets text that is between the field separator and field end.
separatorGets the node that represents the field separator. Can be null.
startGets the node that represents the start of the field.
typeGets the Microsoft Word field type.

Methods

NameDescription
asField()
asFieldAddIn()Cast field to FieldAddIn.
asFieldAddressBlock()Cast field to FieldAddressBlock.
asFieldAdvance()Cast field to FieldAdvance.
asFieldAsk()Cast field to FieldAsk.
asFieldAuthor()Cast field to FieldAuthor.
asFieldAutoNum()Cast field to FieldAutoNum.
asFieldAutoNumLgl()Cast field to FieldAutoNumLgl.
asFieldAutoNumOut()Cast field to FieldAutoNumOut.
asFieldAutoText()Cast field to FieldAutoText.
asFieldAutoTextList()Cast field to FieldAutoTextList.
asFieldBarcode()Cast field to FieldBarcode.
asFieldBibliography()Cast field to FieldBibliography.
asFieldBidiOutline()Cast field to FieldBidiOutline.
asFieldCitation()Cast field to FieldCitation.
asFieldComments()Cast field to FieldComments.
asFieldCompare()Cast field to FieldCompare.
asFieldCreateDate()Cast field to FieldCreateDate.
asFieldData()Cast field to FieldData.
asFieldDatabase()Cast field to FieldDatabase.
asFieldDate()Cast field to FieldDate.
asFieldDde()Cast field to FieldDde.
asFieldDdeAuto()Cast field to FieldDdeAuto.
asFieldDisplayBarcode()Cast field to FieldDisplayBarcode.
asFieldDocProperty()Cast field to FieldDocProperty.
asFieldDocVariable()Cast field to FieldDocVariable.
asFieldEQ()Cast field to FieldEQ.
asFieldEditTime()Cast field to FieldEditTime.
asFieldEmbed()Cast field to FieldEmbed.
asFieldFileName()Cast field to FieldFileName.
asFieldFileSize()Cast field to FieldFileSize.
asFieldFillIn()Cast field to FieldFillIn.
asFieldFootnoteRef()Cast field to FieldFootnoteRef.
asFieldFormCheckBox()Cast field to FieldFormCheckBox.
asFieldFormDropDown()Cast field to FieldFormDropDown.
asFieldFormText()Cast field to FieldFormText.
asFieldFormula()Cast field to FieldFormula.
asFieldGlossary()Cast field to FieldGlossary.
asFieldGoToButton()Cast field to FieldGoToButton.
asFieldGreetingLine()Cast field to FieldGreetingLine.
asFieldHyperlink()Cast field to FieldHyperlink.
asFieldIf()Cast field to FieldIf.
asFieldImport()Cast field to FieldImport.
asFieldInclude()Cast field to FieldInclude.
asFieldIncludePicture()Cast field to FieldIncludePicture.
asFieldIncludeText()Cast field to FieldIncludeText.
asFieldIndex()Cast field to FieldIndex.
asFieldInfo()Cast field to FieldInfo.
asFieldKeywords()Cast field to FieldKeywords.
asFieldLastSavedBy()Cast field to FieldLastSavedBy.
asFieldLink()Cast field to FieldLink.
asFieldListNum()Cast field to FieldListNum.
asFieldMacroButton()Cast field to FieldMacroButton.
asFieldMergeBarcode()Cast field to FieldMergeBarcode.
asFieldMergeField()Cast field to FieldMergeField.
asFieldMergeRec()Cast field to FieldMergeRec.
asFieldMergeSeq()Cast field to FieldMergeSeq.
asFieldNext()Cast field to FieldNext.
asFieldNextIf()Cast field to FieldNextIf.
asFieldNoteRef()Cast field to FieldNoteRef.
asFieldNumChars()Cast field to FieldNumChars.
asFieldNumPages()Cast field to FieldNumPages.
asFieldNumWords()Cast field to FieldNumWords.
asFieldOcx()Cast field to FieldOcx.
asFieldPage()Cast field to FieldPage.
asFieldPageRef()Cast field to FieldPageRef.
asFieldPrint()Cast field to FieldPrint.
asFieldPrintDate()Cast field to FieldPrintDate.
asFieldPrivate()Cast field to FieldPrivate.
asFieldQuote()Cast field to FieldQuote.
asFieldRD()Cast field to FieldRD.
asFieldRef()Cast field to FieldRef.
asFieldRevNum()Cast field to FieldRevNum.
asFieldSaveDate()Cast field to FieldSaveDate.
asFieldSection()Cast field to FieldSection.
asFieldSectionPages()Cast field to FieldSectionPages.
asFieldSeq()Cast field to FieldSeq.
asFieldSet()Cast field to FieldSet.
asFieldShape()Cast field to FieldShape.
asFieldSkipIf()Cast field to FieldSkipIf.
asFieldStyleRef()Cast field to FieldStyleRef.
asFieldSubject()Cast field to FieldSubject.
asFieldSymbol()Cast field to FieldSymbol.
asFieldTA()Cast field to FieldTA.
asFieldTC()Cast field to FieldTC.
asFieldTemplate()Cast field to FieldTemplate.
asFieldTime()Cast field to FieldTime.
asFieldTitle()Cast field to FieldTitle.
asFieldToa()Cast field to FieldToa.
asFieldToc()Cast field to FieldToc.
asFieldUnknown()Cast field to FieldUnknown.
asFieldUserAddress()Cast field to FieldUserAddress.
asFieldUserInitials()Cast field to FieldUserInitials.
asFieldUserName()Cast field to FieldUserName.
asFieldXE()Cast field to FieldXE.
getFieldCode()Returns text between field start and field separator (or field end if there is no separator). Both field code and field result of child fields are included.
getFieldCode(includeChildFieldCodes)Returns text between field start and field separator (or field end if there is no separator).
remove()Removes the field from the document. Returns a node right after the field. If the field’s end is the last child of its parent node, returns its parent paragraph. If the field is already removed, returns null.
unlink()Performs the field unlink.
update()Performs the field update. Throws if the field is being updated already.
update(ignoreMergeFormat)Performs a field update. Throws if the field is being updated already.

Examples

Shows how to insert a field into a document using a field code.

let doc = new aw.Document();
let builder = new aw.DocumentBuilder(doc);

let field = builder.insertField("DATE \\@ \"dddd, MMMM dd, yyyy\"");

expect(field.type).toEqual(aw.Fields.FieldType.FieldDate);
expect(field.getFieldCode()).toEqual("DATE \\@ \"dddd, MMMM dd, yyyy\"");

// This overload of the InsertField method automatically updates inserted fields.
expect((Date.now() - Date.parse(field.result)) / 86400000).toBeLessThanOrEqual(1);

See Also