This topic describes how to obtain the name of a specific document field, insert multiple document fields at a time, and apply a document field style when you use a text document.
Obtain document fields
Obtain all document fields in a document.
Syntax
expression.ActiveDocument.DocumentFieldsexpression: an Application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain document fields in the document. const documentFields = await app.ActiveDocument.DocumentFields; }
Insert a document field
Insert a document field at a specified position.
Syntax
expression.ActiveDocument.DocumentFields.Add()expression: an Application object.
Parameters
Parameter
Type
Required
Description
Name
String
Yes
The name of the document field.
Range
Object
Yes
The range of the document field.
Hidden
Boolean
No
Specifies whether the document field is hidden. Valid values:
false: The document field is displayed. This is the default value.
true: The document field is hidden.
PrintOut
Boolean
No
Specifies whether the document field is printable. Valid values:
true: The document field is printable. This is the default value.
false: The document field is not printable.
ReadOnly
Boolean
No
Specifies whether the document field is read-only. Valid values:
false: The document field is not read-only. This is the default value.
true: The document field is read-only.
Description of the Range parameter
Field
Type
Required
Description
Start
Number
Yes
The start position of the document field.
End
Number
Yes
The end position of the document field.
Return values
Attribute
Type
Description
params1
String
The description of return value 1.
params2
Number
The description of return value 2.
Example
async function example() { await instance.ready(); const app = instance.Application; // Insert a document field. await app.ActiveDocument.DocumentFields.Add({ Name: '1', Range: { Start: 12, End: 20 }, Hidden: false, // Specify that the document field is not hidden. The default value is false. PrintOut: true, // Specify that the document field is printable. The default value is true. ReadOnly: true, // Specify that the document field is read-only. The default value is false. }); }
Insert multiple document fields at a time
Insert multiple document fields at a time at a specified position.
Syntax
expression.ActiveDocument.DocumentFields.AddDocumentFields()expression: an Application object.
Parameters
Parameter
Type
Required
Description
Name
String
Yes
The name of the document field.
Range
Object
Yes
The range of the document field.
Hidden
Boolean
No
Specifies whether the document field is hidden. Valid values:
false: The document field is displayed. This is the default value.
true: The document field is hidden.
PrintOut
Boolean
No
Specifies whether the document field is printable. Valid values:
true: The document field is printable. This is the default value.
false: The document field is not printable.
ReadOnly
Boolean
No
Specifies whether the document field is read-only. Valid values:
false: The document field is not read-only. This is the default value.
true: The document field is read-only.
Value
String
No
The value of the document field.
Description of the Range parameter
Field
Type
Required
Description
Start
Number
Yes
The start position of the document field.
End
Number
Yes
The end position of the document field.
Example
async function example() { await instance.ready(); const app = instance.Application; // Insert multiple fields at a time. await app.ActiveDocument.DocumentFields.AddDocumentFields([ { Name: '1', // Specify the name of the document field. Range: { Start: 0, End: 10 }, // Specify the position of the document field. Value: 'WebOffice1', // Specify the value of the document field. }, { Name: '2', // Specify the name of the document field. Range: { Start: 12, End: 18 }, // Specify the position of the document field. Value: 'WebOffice2', // Specify the value of the document field. }, ]); }
Obtain the total number of document fields.
Syntax
expression.ActiveDocument.DocumentFields.Countexpression: an Application object.
Return value
Returns a
Numberthat indicates the number of document fields.Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the number of document fields. const count = await app.ActiveDocument.DocumentFields.Count; console.log(count); }
Obtain the document field names
Obtain the names of all document fields.
Syntax
expression.ActiveDocument.DocumentFields.GetAllNames()expression: an Application object.
Return value
Returns an
Arraythat contains the names of all document fields in the document.Example
async function example() { await instance.ready(); const app = instance.Application; // Insert multiple fields at a time. await app.ActiveDocument.DocumentFields.AddDocumentFields([ { Name: '1', // Specify the name of the document field. Range: { Start: 0, End: 10 }, // Specify the position of the document field. Value: 'WebOffice1', // Specify the value of the document field. }, { Name: '2', // Specify the name of the document field. Range: { Start: 12, End: 18 }, // Specify the position of the document field. Value: 'WebOffice2', // Specify the value of the document field. }, ]); // Obtain the names of all document fields in the document. const names = await app.ActiveDocument.DocumentFields.GetAllNames(); console.log(names); // ['1', '2'] }
Determine whether a document field exists
Determine whether a document field exists.
Syntax
expression.ActiveDocument.DocumentFields.Exists(Name)expression: an Application object.
Parameters
Parameter
Type
Required
Description
Name
String
Yes
The name of the document field.
Return value
Returns a
Booleanvalue that indicates whether the document field exists.Example
async function example() { await instance.ready(); const app = instance.Application; // Insert multiple fields at a time. await app.ActiveDocument.DocumentFields.AddDocumentFields([ { Name: '1', // Specify the name of the document field. Range: { Start: 0, End: 10 }, // Specify the position of the document field. Value: 'WebOffice1', // Specify the value of the document field. }, { Name: '2', // Specify the name of the document field. Range: { Start: 12, End: 18 }, // Specify the position of the document field. Value: 'WebOffice2', // Specify the value of the document field. }, ]); // Determine whether a document field exists. const hasOne = await app.ActiveDocument.DocumentFields.Exists('1'); console.log(hasOne); // true }
Single document field
Obtain a single document field
Obtain a single document field from a collection of document fields.
Syntax
expression.ActiveDocument.DocumentFields.Item({ Index, Name })expression: an Application object.
Parameters
Parameter
Type
Required
Description
Index
Number
No
The index of the document field.
Name
String
No
The name of the document field.
Return value
Returns a
DocumentFieldobject that indicates a single document field.Example
async function example() { await instance.ready(); const app = instance.Application; // Insert a document field. await app.ActiveDocument.DocumentFields.Add({ Name: '1', Range: { Start: 12, End: 20 }, Hidden: false, // Specify that the document field is not hidden. The default value is false. PrintOut: true, // Specify that the document field is printable. The default value is true. ReadOnly: true, // Specify that the document field is read-only. The default value is false. }); // Obtain a single document field from a collection of document fields. const documentField = await app.ActiveDocument.DocumentFields.Item({ Name: '1' }); console.log(documentField); }
Delete a specified document field
Syntax
expression.ActiveDocument.DocumentFields.Item({ Index, Name }).Delete()expression: an Application object.
NoteYou can use a loop to delete multiple document fields.
Parameters
Parameter
Type
Required
Description
Index
Number
No
The index of the document field.
Name
String
No
The name of the document field.
Example
async function example() { await instance.ready(); const app = instance.Application; // Insert a document field. await app.ActiveDocument.DocumentFields.Add({ Name: '1', Range: { Start: 12, End: 20 }, Hidden: false, // Specify that the document field is not hidden. The default value is false. PrintOut: true, // Specify that the document field is printable. The default value is true. ReadOnly: true, // Specify that the document field is read-only. The default value is false. }); // Obtain a single document field from a collection of document fields. const documentField = await app.ActiveDocument.DocumentFields.Item({ Name: '1' }); // Delete the document field. await documentField.Delete(); }
Go to the start position of the document field
Syntax
expression.ActiveDocument.DocumentFields.Item({ Index, Name }).GotoBegin()expression: an Application object.
Parameters
Parameter
Type
Required
Description
Index
Number
No
The index of the document field.
Name
String
No
The name of the document field.
Example
async function example() { await instance.ready(); const app = instance.Application; // Insert a document field. await app.ActiveDocument.DocumentFields.Add({ Name: '1', Range: { Start: 12, End: 20 }, Hidden: false, // Specify that the document field is not hidden. The default value is false. PrintOut: true, // Specify that the document field is printable. The default value is true. ReadOnly: true, // Specify that the document field is read-only. The default value is false. }); // Obtain a single document field from a collection of document fields. const documentField = await app.ActiveDocument.DocumentFields.Item({ Name: '1' }); // Go to the start position of the document field. await documentField.GotoBegin(); }
Go to the end position of the document field
Syntax
expression.ActiveDocument.DocumentFields.Item({ Index, Name }).GotoEnd()expression: an Application object.
Parameters
Parameter
Type
Required
Description
Index
Number
No
The index of the document field.
Name
String
No
The name of the document field.
Example
async function example() { await instance.ready(); const app = instance.Application; // Insert a document field. await app.ActiveDocument.DocumentFields.Add({ Name: '1', Range: { Start: 12, End: 20 }, Hidden: false, // Specify that the document field is not hidden. The default value is false. PrintOut: true, // Specify that the document field is printable. The default value is true. ReadOnly: true, // Specify that the document field is read-only. The default value is false. }); // Obtain a single document field from a collection of document fields. const documentField = await app.ActiveDocument.DocumentFields.Item({ Name: '1' }); // Go to the end position of the document field. await documentField.GotoEnd(); }
View whether the document field is hidden
Syntax
expression.ActiveDocument.DocumentFields.Item({ Index, Name }).Hiddenexpression: an Application object.
Parameters
Parameter
Type
Required
Description
Index
Number
No
The index of the document field.
Name
String
No
The name of the document field.
Return value
Returns a
Booleanvalue that indicates whether the document field is hidden.Example
async function example() { await instance.ready(); const app = instance.Application; // Insert a document field. await app.ActiveDocument.DocumentFields.Add({ Name: '1', Range: { Start: 12, End: 20 }, Hidden: true, // Specify that the document field is hidden. The default value is false. PrintOut: true, // Specify that the document field is printable. The default value is true. ReadOnly: true, // Specify that the document field is read-only. The default value is false. }); // Obtain a single document field from a collection of document fields. const documentField = await app.ActiveDocument.DocumentFields.Item({ Name: '1' }); // Check whether the document field is hidden. const isHidden = await documentField.Hidden; console.log(isHidden); }
View whether the document field is printable
Syntax
expression.ActiveDocument.DocumentFields.Item({ Index, Name }).PrintOutexpression: an Application object.
Parameters
Parameter
Type
Required
Description
Index
Number
No
The index of the document field.
Name
String
No
The name of the document field.
Return value
Returns a
Booleanvalue that indicates whether the document field can be printed.Example
async function example() { await instance.ready(); const app = instance.Application; // Insert a document field. await app.ActiveDocument.DocumentFields.Add({ Name: '1', Range: { Start: 12, End: 20 }, Hidden: false, // Specify that the document field is not hidden. The default value is false. PrintOut: true, // Specify that the document field is printable. The default value is true. ReadOnly: true, // Specify that the document field is read-only. The default value is false. }); // Obtain a single document field from a collection of document fields. const documentField = await app.ActiveDocument.DocumentFields.Item({ Name: '1' }); // Check whether the document field is printable. const isPrintOut = await documentField.PrintOut; console.log(isPrintOut); }
View whether the document field is read-only
Syntax
expression.ActiveDocument.DocumentFields.Item({ Index, Name }).ReadOnlyexpression: an Application object.
Parameters
Parameter
Type
Required
Description
Index
Number
No
The index of the document field.
Name
String
No
The name of the document field.
Return value
Returns a
Booleanvalue that indicates whether the document field is read-only.Example
async function example() { await instance.ready(); const app = instance.Application; // Insert a document field. await app.ActiveDocument.DocumentFields.Add({ Name: '1', Range: { Start: 12, End: 20 }, Hidden: true, // Specify that the document field is hidden. The default value is false. PrintOut: true, // Specify that the document field is printable. The default value is true. ReadOnly: true, // Specify that the document field is read-only. The default value is false. }); // Obtain a single document field from a collection of document fields. const documentField = await app.ActiveDocument.DocumentFields.Item({ Name: '1' }); // Check whether the document field is read-only. const isReadOnly = await documentField.ReadOnly; console.log(isReadOnly); }
View the range of a document field
Syntax
expression.ActiveDocument.DocumentFields.Item({ Index, Name }).Rangeexpression: an Application object.
Parameters
Parameter
Type
Required
Description
Index
Number
No
The index of the document field.
Name
String
No
The range of the document field.
Return value
Returns a
Rangeobject to indicate the range of the document field.Example
async function example() { await instance.ready(); const app = instance.Application; // Insert a document field. await app.ActiveDocument.DocumentFields.Add({ Name: '1', Range: { Start: 12, End: 20 }, Hidden: false, // Specify that the document field is not hidden. The default value is false. PrintOut: true, // Specify that the document field is printable. The default value is true. ReadOnly: true, // Specify that the document field is read-only. The default value is false. }); // Obtain a single document field from a collection of document fields. const documentField = await app.ActiveDocument.DocumentFields.Item({ Name: '1' }); // View the range of the document field. const range = await documentField.Range; console.log(range); }
View the name of a document field
Syntax
expression.ActiveDocument.DocumentFields.Item({ Index, Name }).Nameexpression: an Application object.
Parameters
Parameter
Type
Required
Description
Index
Number
No
The index of the document field.
Name
String
No
The name of the document field.
Return value
Returns a
Stringthat indicates the name of the document field.Example
async function example() { await instance.ready(); const app = instance.Application; // Insert a document field. await app.ActiveDocument.DocumentFields.Add({ Name: '1', Range: { Start: 12, End: 20 }, Hidden: true, // Specify that the document field is hidden. The default value is false. PrintOut: true, // Specify that the document field is printable. The default value is true. ReadOnly: true, // Specify that the document field is read-only. The default value is false. }); // Obtain a single document field from a collection of document fields. const documentField = await app.ActiveDocument.DocumentFields.Item({ Name: '1' }); console.log(documentField); // View the name of the document field. const name = await documentField.Name; console.log(name); }
View the value of a document field
Syntax
expression.ActiveDocument.DocumentFields.Item({ Index, Name }).Valueexpression: an Application object.
Return value
Returns a
Stringthat indicates the value of the document field.Example
async function example() { await instance.ready(); const app = instance.Application; // Insert a document field. await app.ActiveDocument.DocumentFields.Add({ Name: '1', Range: { Start: 12, End: 20 }, Hidden: false, // Specify that the document field is not hidden. The default value is false. PrintOut: true, // Specify that the document field is printable. The default value is true. ReadOnly: true, // Specify that the document field is not read-only. The default value is false. }); // Obtain a single document field from a collection of document fields. const documentField = await app.ActiveDocument.DocumentFields.Item({ Name: '1' }); // View the value of the document field. const value = await documentField.Value; console.log(value); // Specify the value of the document field. documentField.Value = 'WebOffice'; }
Style
View the style of the document field
Syntax
expression.ActiveDocument.DocumentFields.Item({ Index, Name }).Styleexpression: an Application object.
Parameters
Parameter
Type
Required
Description
Index
Number
No
The index of the document field.
Name
String
No
The style of the document field.
Return value
Returns a
Stylethat indicates the style of the document field.Example
async function example() { await instance.ready(); const app = instance.Application; // Insert a document field. await app.ActiveDocument.DocumentFields.Add({ Name: '1', Range: { Start: 12, End: 20 }, Hidden: false, // Specify that the document field is not hidden. The default value is false. PrintOut: true, // Specify that the document field is printable. The default value is true. ReadOnly: true, // Specify that the document field is read-only. The default value is false. }); // Obtain a single document field from a collection of document fields. const documentField = await app.ActiveDocument.DocumentFields.Item({ Name: '1' }); // View the style of the document field. const style = await documentField.Style; console.log(style); }
Apply the document field style
Apply the style of a document field to another document field.
Syntax
expression.ActiveDocument.DocumentFields.Item({ Index, Name }).Style.ApplyTo(Name)expression: an Application object.
Parameters
Parameter
Type
Required
Description
Name
String
Yes
The name of the document field.
Return value
Returns an
Arraythat contains the names of all document fields in the document.Example
async function example() { await instance.ready(); const app = instance.Application; // Insert multiple fields at a time. await app.ActiveDocument.DocumentFields.AddDocumentFields([ { Name: '1', // Specify the name of the document field. Range: { Start: 0, End: 10 }, // Specify the position of the document field. Value: 'WebOffice1', // Specify the value of the document field. }, { Name: '2', // Specify the name of the document field. Range: { Start: 12, End: 18 }, // Specify the position of the document field. Value: 'WebOffice2', // Specify the value of the document field. }, ]); // Obtain a single document field from a collection of document fields. const documentField = await app.ActiveDocument.DocumentFields.Item({ Name: '1' }); // Apply the style. const style = await documentField.Style.ApplyTo('2'); console.log(style); }
Replace the content marked by document fields in batches
Syntax
expression.ActiveDocument.DocumentFields.SetDocumentFieldsValues({Value, Data})expression: an Application object.
Parameters
Parameter
Type
Required
Description
Value
String
Yes
The value that is used to replace the content in multiple document fields at a time.
Data
Array.<Object>Yes
The index or name of the document field.
Description of the Data parameter
Parameter
Type
Required
Description
Index
Number
Either Index or Name must be specified.
The index of the document field.
Name
String
Either Index or Name must be specified.
The name of the document field.
Example
async function example() { await instance.ready(); const app = instance.Application; const dfs = [ { Name: '2', Range: { Start: 30, End: 31 }, Value: '2', }, { Name: '3', Range: { Start: 33, End: 34 }, Value: '3', }, { Name: '4', Range: { Start: 35, End: 36 }, Value: '4', }]; await app.ActiveDocument.DocumentFields.AddDocumentFields(dfs); await app.ActiveDocument.DocumentFields.SetDocumentFieldsValues({ Value: "Replace the content marked by document fields in batches", Arrays: [{Name: '2'}, {Name: '3'}, {Name: '4'}] }); }