Working with Fields in JavaScript Word Library
19 Oct 20267 minutes to read
Fields are placeholders in a Word document that store dynamic values such as page numbers, conditional text, merge data, and hyperlinks. In @syncfusion/ej2-docx, a field is represented by a Field instance (or a specialized type such as IfField or MergeField) together with field marks that separate the field code from the field result.
For hyperlink authoring helpers (
appendHyperlink,HyperlinkType), see Working with Hyperlinks.
Formatting fields
After you insert a field, the paragraph items between the field begin mark and the field end mark include the field result text ranges. You can format those result runs—for example, set a smaller font size on the page-number result.
The following code example illustrates how to format the result of a PAGE field.
import { WordDocument, FieldMark, FieldMarkType, FieldType, TextRange } from '@syncfusion/ej2-docx';
// Creates a new Word document
let document = WordDocument.create();
// Gets the paragraph of the section
let paragraph = document.sections[0].body.paragraphs[0];
// Appends text before the field
paragraph.appendText('Page number: ');
// Inserts a PAGE field and keeps the insertion index
let fieldIndex = paragraph.items.count;
paragraph.appendField('Page', FieldType.Page);
// Formats text ranges in the field result (between the field and its end mark)
for (let i = fieldIndex + 1; i < paragraph.items.count; i++) {
let item = paragraph.items[i];
if (item instanceof TextRange) {
item.characterFormat.fontSize = 6;
} else if (item instanceof FieldMark && item.fieldMarkType === FieldMarkType.End) {
break;
}
}
// Saves the Word document to disk
document.save('FormattingField.docx');// Creates a new Word document
let document = ej.docx.WordDocument.create();
// Gets the paragraph of the section
let paragraph = document.sections[0].body.paragraphs[0];
// Appends text before the field
paragraph.appendText('Page number: ');
// Inserts a PAGE field and keeps the insertion index
let fieldIndex = paragraph.items.count;
paragraph.appendField('Page', ej.docx.FieldType.Page);
// Formats text ranges in the field result (between the field and its end mark)
for (let i = fieldIndex + 1; i < paragraph.items.count; i++) {
let item = paragraph.items[i];
if (item instanceof ej.docx.TextRange) {
item.characterFormat.fontSize = 6;
} else if (item instanceof ej.docx.FieldMark && item.fieldMarkType === ej.docx.FieldMarkType.End) {
break;
}
}
// Saves the Word document to disk
document.save('FormattingField.docx');IF field
An IF field evaluates a comparison and shows one of two results. Insert the field with FieldType.If, then assign the full IF expression to fieldCode.
The following code example illustrates how to add IF fields that compare text or numeric values.
import { WordDocument, FieldType } from '@syncfusion/ej2-docx';
// Creates a new Word document
let document = WordDocument.create();
// Access first section
let section = document.sections[0];
// Gets the default paragraph
let paragraph = section.body.paragraphs[0];
// Appends a description and an IF field that compares text
paragraph.appendText(
'If field that compares a string value and displays the result.'
);
paragraph = section.body.appendParagraph();
let ifField = paragraph.appendField('If', FieldType.If);
// Expression: IF "100" = "100" "correct" "not correct"
ifField.fieldCode = 'IF "100" = "100" "correct" "not correct"';
// Appends another IF field that compares numbers
paragraph = section.body.appendParagraph();
paragraph.appendText(
'If field that compares a number value and displays the result.'
);
paragraph = section.body.appendParagraph();
let ifField2 = paragraph.appendField('If', FieldType.If);
// Expression: IF 100 >= 50 "correct" "not correct"
ifField2.fieldCode = 'IF 100 >= 50 "correct" "not correct"';
// Saves the Word document to disk
document.save('IfField.docx');// Creates a new Word document
let document = ej.docx.WordDocument.create();
// Access first section
let section = document.sections[0];
// Gets the default paragraph
let paragraph = section.body.paragraphs[0];
// Appends a description and an IF field that compares text
paragraph.appendText(
'If field that compares a string value and displays the result.'
);
paragraph = section.body.appendParagraph();
let ifField = paragraph.appendField('If', ej.docx.FieldType.If);
// Expression: IF "100" = "100" "correct" "not correct"
ifField.fieldCode = 'IF "100" = "100" "correct" "not correct"';
// Appends another IF field that compares numbers
paragraph = section.body.appendParagraph();
paragraph.appendText(
'If field that compares a number value and displays the result.'
);
paragraph = section.body.appendParagraph();
let ifField2 = paragraph.appendField('If', ej.docx.FieldType.If);
// Expression: IF 100 >= 50 "correct" "not correct"
ifField2.fieldCode = 'IF 100 >= 50 "correct" "not correct"';
// Saves the Word document to disk
document.save('IfField.docx');Merge field
Merge fields are placeholders replaced with data source values during mail merge. Insert them with FieldType.MergeField. The field name you pass to appendField becomes the merge field name in the document.
The following code example illustrates how to add merge fields.
import { WordDocument, FieldType } from '@syncfusion/ej2-docx';
// Creates a new Word document
let document = WordDocument.create();
// Access first section
let section = document.sections[0];
// Gets the default paragraph
let paragraph = section.body.paragraphs[0];
// Inserts merge fields for Name and Address
paragraph.appendField('Name', FieldType.MergeField);
paragraph = section.body.appendParagraph();
paragraph.appendField('Address', FieldType.MergeField);
// Saves the Word document to disk
document.save('MergeField.docx');// Creates a new Word document
let document = ej.docx.WordDocument.create();
// Access first section
let section = document.sections[0];
// Gets the default paragraph
let paragraph = section.body.paragraphs[0];
// Inserts merge fields for Name and Address
paragraph.appendField('Name', ej.docx.FieldType.MergeField);
paragraph = section.body.appendParagraph();
paragraph.appendField('Address', ej.docx.FieldType.MergeField);
// Saves the Word document to disk
document.save('MergeField.docx');