Field
Currently the following fields are supported:
- PAGE
- NUMPAGES
- DATE
- XE
- INDEX
- FILENAME
- REF
<?php
$section->addField($fieldType, [$properties], [$options], [$fieldText], [$fontStyle])
$fontStyle. SeeStyles > Font.
See \PhpOffice\PhpWord\Element\Field for list of properties and options available for each field type.
Options which are not specifically defined can be added. Those must start with a \.
ODText support
The ODText writer uses native ODF fields instead of copying Word field instruction strings into the ODT package:
PAGE,NUMPAGES,DATE, andFILENAMEuse the corresponding ODF fields.REFis written astext:reference-refand supports text references and the page-reference option.XEis written astext:alphabetical-index-mark.INDEXis written as an ODF alphabetical-index field.MACROBUTTONandSTYLEREFhave no meaningful portable ODF equivalent and are omitted by the ODText writer.
The ODF fields are dynamic and may be refreshed by the consuming application. Word-specific formatting and macro behavior that has no ODF equivalent is not preserved.
For instance for the INDEX field, you can do the following (See Index Field for list of available options <https://support.office.com/en-us/article/Field-codes-Index-field-adafcf4a-cb30-43f6-85c7-743da1635d9e?ui=en-US&rs=en-US&ad=US>_ ):
<?php
// the $fieldText can be either a simple string
$fieldText = 'The index value';
// or a 'TextRun', to be able to format the text you want in the index
$fieldText = new TextRun();
$fieldText->addText('My ');
$fieldText->addText('bold index', ['bold' => true]);
$fieldText->addText(' entry');
$section->addField('XE', array(), array(), $fieldText);
// this actually adds the index
$section->addField('INDEX', array(), array('\\e " " \\h "A" \\c "3"'), 'right click to update index');
// Adding reference to a bookmark
$fieldText->addField('REF', [
'name' => 'bookmark'
], [
'InsertParagraphNumberRelativeContext',
'CreateHyperLink',
]);