2014-03-19 23:08:44 +07:00
.. _elements:
Elements
========
2014-04-26 14:21:52 +07:00
Below are the matrix of element availability in each container. The
column shows the containers while the rows lists the elements.
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
| Num | Element | Section | Header | Footer | Cell | Text Run | Footnote |
+=======+=================+===========+==========+==========+=========+============+============+
| 1 | Text | v | v | v | v | v | v |
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
| 2 | Text Run | v | v | v | v | - | - |
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
| 3 | Link | v | v | v | v | v | v |
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
| 4 | Title | v | ? | ? | ? | ? | ? |
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
2014-05-29 16:21:15 +07:00
| 5 | Preserve Text | ? | v | v | v\* | - | - |
2014-04-26 14:21:52 +07:00
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
| 6 | Text Break | v | v | v | v | v | v |
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
| 7 | Page Break | v | - | - | - | - | - |
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
| 8 | List | v | v | v | v | - | - |
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
2014-05-31 12:37:38 +07:00
| 9 | Table | v | v | v | v | - | - |
2014-04-26 14:21:52 +07:00
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
| 10 | Image | v | v | v | v | v | v |
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
| 11 | Watermark | - | v | - | - | - | - |
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
| 12 | Object | v | v | v | v | v | v |
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
| 13 | TOC | v | - | - | - | - | - |
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
| 14 | Footnote | v | - | - | v\*\* | v\*\* | - |
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
| 15 | Endnote | v | - | - | v\*\* | v\*\* | - |
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
2014-05-29 16:21:15 +07:00
| 16 | CheckBox | v | v | v | v | - | - |
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
| 17 | TextBox | v | v | v | v | - | - |
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
| 18 | Field | v | v | v | v | v | v |
2014-04-26 14:21:52 +07:00
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
2014-05-30 01:05:55 +07:00
| 19 | Line | v | v | v | v | v | v |
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
2014-04-02 09:01:44 +07:00
Legend:
2015-10-10 19:06:23 +04:00
- `` v `` . Available.
- `` v* `` . Available only when inside header/footer.
- `` v** `` . Available only when inside section.
- `` - `` . Not available.
- `` ? `` . Should be available.
2014-04-02 09:01:44 +07:00
2014-03-19 23:08:44 +07:00
Texts
-----
2014-04-02 11:02:56 +07:00
Text can be added by using `` addText `` and `` addTextRun `` method.
2015-10-10 19:06:23 +04:00
`` addText `` is used for creating simple paragraphs that only contain texts with the same style.
`` addTextRun `` is used for creating complex paragraphs that contain text with different style (some bold, other
italics, etc) or other elements, e.g. images or links. The syntaxes are as follow:
2014-03-19 23:08:44 +07:00
2014-03-21 10:04:00 +01:00
.. code-block :: php
2014-03-19 23:08:44 +07:00
$section->addText($text, [$fontStyle], [$paragraphStyle]);
2014-04-02 11:02:56 +07:00
$textrun = $section->addTextRun([$paragraphStyle]);
2014-03-19 23:08:44 +07:00
2015-10-10 19:06:23 +04:00
- `` $text `` . Text to be displayed in the document.
- `` $fontStyle `` . See :ref: `font-style` .
- `` $paragraphStyle `` . See :ref: `paragraph-style` .
2014-03-19 23:08:44 +07:00
2015-10-10 19:06:23 +04:00
For available styling options see :ref: `font-style` and :ref: `paragraph-style` .
2014-03-19 23:08:44 +07:00
Titles
~~~~~~
2015-10-10 19:06:23 +04:00
If you want to structure your document or build table of contents, you need titles or headings.
To add a title to the document, use the `` addTitleStyle `` and `` addTitle `` method.
2014-03-19 23:08:44 +07:00
2014-03-21 10:04:00 +01:00
.. code-block :: php
2014-03-19 23:08:44 +07:00
2014-03-23 17:37:26 +07:00
$phpWord->addTitleStyle($depth, [$fontStyle], [$paragraphStyle]);
2014-03-19 23:08:44 +07:00
$section->addTitle($text, [$depth]);
2015-10-10 19:06:23 +04:00
- `` depth `` .
- `` $fontStyle `` . See :ref: `font-style` .
- `` $paragraphStyle `` . See :ref: `paragraph-style` .
- `` $text `` . Text to be displayed in the document.
It's necessary to add a title style to your document because otherwise the title won't be detected as a real title.
2014-03-19 23:08:44 +07:00
Links
~~~~~
You can add Hyperlinks to the document by using the function addLink:
2014-03-21 10:04:00 +01:00
.. code-block :: php
2014-03-19 23:08:44 +07:00
$section->addLink($linkSrc, [$linkName], [$fontStyle], [$paragraphStyle]);
2015-10-10 19:06:23 +04:00
- `` $linkSrc `` . The URL of the link.
- `` $linkName `` . Placeholder of the URL that appears in the document.
- `` $fontStyle `` . See :ref: `font-style` .
- `` $paragraphStyle `` . See :ref: `paragraph-style` .
2014-03-19 23:08:44 +07:00
Preserve texts
~~~~~~~~~~~~~~
2015-10-10 19:06:23 +04:00
The `` addPreserveText `` method is used to add a page number or page count to headers or footers.
2014-03-19 23:08:44 +07:00
2014-03-21 10:04:00 +01:00
.. code-block :: php
2014-03-19 23:08:44 +07:00
$footer->addPreserveText('Page {PAGE} of {NUMPAGES}.');
Breaks
------
Text breaks
~~~~~~~~~~~
2015-10-10 19:06:23 +04:00
Text breaks are empty new lines. To add text breaks, use the following syntax. All parameters are optional.
2014-03-19 23:08:44 +07:00
2014-03-21 10:04:00 +01:00
.. code-block :: php
2014-03-19 23:08:44 +07:00
$section->addTextBreak([$breakCount], [$fontStyle], [$paragraphStyle]);
2015-10-10 19:06:23 +04:00
- `` $breakCount `` . How many lines.
- `` $fontStyle `` . See :ref: `font-style` .
- `` $paragraphStyle `` . See :ref: `paragraph-style` .
2014-03-19 23:08:44 +07:00
Page breaks
~~~~~~~~~~~
There are two ways to insert a page breaks, using the `` addPageBreak ``
method or using the `` pageBreakBefore `` style of paragraph.
2014-09-29 17:01:43 +02:00
:: code-block:: php
2014-03-19 23:08:44 +07:00
2014-09-29 17:01:43 +02:00
\\$section->addPageBreak();
2014-03-19 23:08:44 +07:00
Lists
-----
To add a list item use the function `` addListItem `` .
2014-04-11 17:59:48 +07:00
Basic usage:
2014-03-21 10:04:00 +01:00
.. code-block :: php
2014-03-19 23:08:44 +07:00
$section->addListItem($text, [$depth], [$fontStyle], [$listStyle], [$paragraphStyle]);
2014-04-11 17:59:48 +07:00
Parameters:
2015-10-10 19:06:23 +04:00
- `` $text `` . Text that appears in the document.
- `` $depth `` . Depth of list item.
- `` $fontStyle `` . See :ref: `font-style` .
2017-09-09 01:25:33 +02:00
- `` $listStyle `` . List style of the current element TYPE\_NUMBER,
TYPE\_ALPHANUM, TYPE\_BULLET\_FILLED, etc. See list of constants in PHPWord\\Style\\ListItem.
2015-10-10 19:06:23 +04:00
- `` $paragraphStyle `` . See :ref: `paragraph-style` .
2014-03-19 23:08:44 +07:00
2014-04-11 17:59:48 +07:00
Advanced usage:
2015-10-10 19:06:23 +04:00
You can also create your own numbering style by changing the `` $listStyle `` parameter with the name of your numbering style.
2014-04-11 17:59:48 +07:00
.. code-block :: php
$phpWord->addNumberingStyle(
'multilevel',
2015-10-10 19:06:23 +04:00
array(
'type' => 'multilevel',
'levels' => array(
array('format' => 'decimal', 'text' => '%1.', 'left' => 360, 'hanging' => 360, 'tabPos' => 360),
array('format' => 'upperLetter', 'text' => '%2.', 'left' => 720, 'hanging' => 360, 'tabPos' => 720),
2014-04-11 17:59:48 +07:00
)
2015-10-10 19:06:23 +04:00
)
2014-04-11 17:59:48 +07:00
);
$section->addListItem('List Item I', 0, null, 'multilevel');
$section->addListItem('List Item I.a', 1, null, 'multilevel');
$section->addListItem('List Item I.b', 1, null, 'multilevel');
$section->addListItem('List Item II', 0, null, 'multilevel');
2015-10-10 19:06:23 +04:00
For available styling options see :ref: `numbering-level-style` .
2014-04-11 17:59:48 +07:00
2014-03-19 23:08:44 +07:00
Tables
------
2015-10-10 19:06:23 +04:00
To add tables, rows, and cells, use the `` addTable `` , `` addRow `` , and `` addCell `` methods:
2014-03-19 23:08:44 +07:00
2014-03-21 10:04:00 +01:00
.. code-block :: php
2014-03-19 23:08:44 +07:00
$table = $section->addTable([$tableStyle]);
$table->addRow([$height], [$rowStyle]);
$cell = $table->addCell($width, [$cellStyle]);
Table style can be defined with `` addTableStyle `` :
2014-03-21 10:04:00 +01:00
.. code-block :: php
2014-03-19 23:08:44 +07:00
$tableStyle = array(
'borderColor' => '006699',
2015-10-10 19:06:23 +04:00
'borderSize' => 6,
'cellMargin' => 50
2014-03-19 23:08:44 +07:00
);
$firstRowStyle = array('bgColor' => '66BBFF');
2014-03-23 17:37:26 +07:00
$phpWord->addTableStyle('myTable', $tableStyle, $firstRowStyle);
2014-03-19 23:08:44 +07:00
$table = $section->addTable('myTable');
2015-10-10 19:06:23 +04:00
For available styling options see :ref: `table-style` .
2014-03-19 23:08:44 +07:00
Cell span
~~~~~~~~~
2015-10-10 19:06:23 +04:00
You can span a cell on multiple columns by using `` gridSpan `` or multiple rows by using `` vMerge `` .
2014-03-19 23:08:44 +07:00
2014-03-21 10:04:00 +01:00
.. code-block :: php
2014-03-19 23:08:44 +07:00
$cell = $table->addCell(200);
$cell->getStyle()->setGridSpan(5);
2014-03-20 22:53:48 +07:00
See `` Sample_09_Tables.php `` for more code sample.
2014-03-19 23:08:44 +07:00
Images
------
2015-10-10 19:06:23 +04:00
To add an image, use the `` addImage `` method to sections, headers, footers, textruns, or table cells.
2014-03-19 23:08:44 +07:00
2014-03-21 10:04:00 +01:00
.. code-block :: php
2014-03-19 23:08:44 +07:00
$section->addImage($src, [$style]);
2014-03-25 10:25:48 +07:00
2016-12-12 20:39:10 +01:00
- `` $src `` . String path to a local image, URL of a remote image or the image data, as a string.
2015-10-10 19:06:23 +04:00
- `` $style `` . See :ref: `image-style` .
2014-03-19 23:08:44 +07:00
Examples:
2014-03-21 10:04:00 +01:00
.. code-block :: php
2014-03-19 23:08:44 +07:00
2014-04-02 11:02:56 +07:00
$section = $phpWord->addSection();
2014-03-19 23:08:44 +07:00
$section->addImage(
'mars.jpg',
array(
2015-10-10 19:06:23 +04:00
'width' => 100,
'height' => 100,
'marginTop' => -1,
'marginLeft' => -1,
2014-03-19 23:08:44 +07:00
'wrappingStyle' => 'behind'
)
);
2014-04-02 11:02:56 +07:00
$footer = $section->addFooter();
2014-03-25 10:25:48 +07:00
$footer->addImage('http://example.com/image.php');
2014-04-02 11:02:56 +07:00
$textrun = $section->addTextRun();
2014-03-25 10:25:48 +07:00
$textrun->addImage('http://php.net/logo.jpg');
2016-12-12 20:39:10 +01:00
$source = file_get_contents('/path/to/my/images/earth.jpg');
$textrun->addImage($source);
2014-03-19 23:08:44 +07:00
Watermarks
~~~~~~~~~~
To add a watermark (or page background image), your section needs a
header reference. After creating a header, you can use the
`` addWatermark `` method to add a watermark.
2014-03-21 10:04:00 +01:00
.. code-block :: php
2014-03-19 23:08:44 +07:00
2014-04-02 11:02:56 +07:00
$section = $phpWord->addSection();
$header = $section->addHeader();
2014-03-19 23:08:44 +07:00
$header->addWatermark('resources/_earth.jpg', array('marginTop' => 200, 'marginLeft' => 55));
Objects
-------
You can add OLE embeddings, such as Excel spreadsheets or PowerPoint
presentations to the document by using `` addObject `` method.
2014-03-21 10:04:00 +01:00
.. code-block :: php
2014-03-19 23:08:44 +07:00
$section->addObject($src, [$style]);
Table of contents
-----------------
To add a table of contents (TOC), you can use the `` addTOC `` method.
2015-10-10 19:06:23 +04:00
Your TOC can only be generated if you have add at least one title (See "Titles").
2014-03-19 23:08:44 +07:00
2014-03-21 10:04:00 +01:00
.. code-block :: php
2014-03-19 23:08:44 +07:00
2014-04-06 00:56:48 +07:00
$section->addTOC([$fontStyle], [$tocStyle], [$minDepth], [$maxDepth]);
2015-10-10 19:06:23 +04:00
- `` $fontStyle `` . See font style section.
- `` $tocStyle `` . See available options below.
- `` $minDepth `` . Minimum depth of header to be shown. Default 1.
- `` $maxDepth `` . Maximum depth of header to be shown. Default 9.
2014-04-06 00:56:48 +07:00
Options for `` $tocStyle `` :
2014-03-19 23:08:44 +07:00
2017-09-09 01:25:33 +02:00
- `` tabLeader `` . Fill type between the title text and the page number. Use the defined constants in PHPWord\\Style\\TOC.
2015-10-10 19:06:23 +04:00
- `` tabPos `` . The position of the tab where the page number appears in twips.
- `` indent `` . The indent factor of the titles in twips.
2014-03-19 23:08:44 +07:00
2014-04-09 21:35:55 +07:00
Footnotes & endnotes
--------------------
2014-03-19 23:08:44 +07:00
2014-04-26 14:21:52 +07:00
You can create footnotes with `` addFootnote `` and endnotes with
`` addEndnote `` in texts or textruns, but it's recommended to use textrun
to have better layout. You can use `` addText `` , `` addLink `` ,
`` addTextBreak `` , `` addImage `` , `` addObject `` on footnotes and endnotes.
2014-03-19 23:08:44 +07:00
On textrun:
2014-03-21 10:04:00 +01:00
.. code-block :: php
2014-03-19 23:08:44 +07:00
2014-04-02 11:02:56 +07:00
$textrun = $section->addTextRun();
2014-03-19 23:08:44 +07:00
$textrun->addText('Lead text.');
2014-04-02 11:02:56 +07:00
$footnote = $textrun->addFootnote();
2014-03-29 00:57:23 +07:00
$footnote->addText('Footnote text can have ');
$footnote->addLink('http://test.com', 'links');
$footnote->addText('.');
$footnote->addTextBreak();
$footnote->addText('And text break.');
2014-03-19 23:08:44 +07:00
$textrun->addText('Trailing text.');
2014-04-09 21:35:55 +07:00
$endnote = $textrun->addEndnote();
$endnote->addText('Endnote put at the end');
2014-03-19 23:08:44 +07:00
On text:
2014-03-21 10:04:00 +01:00
.. code-block :: php
2014-03-19 23:08:44 +07:00
$section->addText('Lead text.');
2014-04-02 11:02:56 +07:00
$footnote = $section->addFootnote();
2014-03-19 23:08:44 +07:00
$footnote->addText('Footnote text.');
2014-03-29 00:57:23 +07:00
2017-05-30 00:31:32 +02:00
By default the footnote reference number will be displayed with decimal number
starting from 1. This number uses the `` FooterReference `` style which you can
redefine with the `` addFontStyle `` method. Default value for this style is
2014-03-29 00:57:23 +07:00
`` array('superScript' => true) `` ;
2014-03-30 01:30:25 +07:00
2017-05-30 00:31:32 +02:00
The footnote numbering can be controlled by setting the FootnoteProperties on the Section.
.. code-block :: php
$fp = new PhpWord\SimpleType\FootnoteProperties();
//sets the position of the footnote (pageBottom (default), beneathText, sectEnd, docEnd)
$fp->setPos(FootnoteProperties::POSITION_DOC_END);
//set the number format to use (decimal (default), upperRoman, upperLetter, ...)
$fp->setNumFmt(FootnoteProperties::NUMBER_FORMAT_LOWER_ROMAN);
//force starting at other than 1
$fp->setNumStart(2);
//when to restart counting (continuous (default), eachSect, eachPage)
$fp->setNumRestart(FootnoteProperties::RESTART_NUMBER_EACH_PAGE);
//And finaly, set it on the Section
$section->setFootnoteProperties($properties);
2014-03-30 01:30:25 +07:00
Checkboxes
----------
2015-10-10 19:06:23 +04:00
Checkbox elements can be added to sections or table cells by using `` addCheckBox `` .
2014-03-30 01:30:25 +07:00
.. code-block :: php
$section->addCheckBox($name, $text, [$fontStyle], [$paragraphStyle])
2015-10-10 19:06:23 +04:00
- `` $name `` . Name of the check box.
- `` $text `` . Text to be displayed in the document.
- `` $fontStyle `` . See :ref: `font-style` .
- `` $paragraphStyle `` . See :ref: `paragraph-style` .
2014-05-29 16:21:15 +07:00
Textboxes
---------
To be completed
Fields
------
2017-06-11 12:56:59 +02:00
Currently the following fields are supported:
- PAGE
- NUMPAGES
- DATE
- XE
- INDEX
.. code-block :: php
$section->addField($fieldType, [$properties], [$options], [$fieldText])
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 `` \ `` .
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> `_ ):
.. code-block :: 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('INDEX', array(), array('\\e " " \\h "A" \\c "3"'), $fieldText);
2014-05-30 01:05:55 +07:00
Line
------
2014-06-24 09:27:18 +02:00
Line elements can be added to sections by using `` addLine `` .
.. code-block :: php
$linestyle = array('weight' => 1, 'width' => 100, 'height' => 0, 'color' => 635552);
$section->addLine($lineStyle)
Available line style attributes:
2015-10-10 19:06:23 +04:00
- `` weight `` . Line width in twips.
- `` color `` . Defines the color of stroke.
- `` dash `` . Line types: dash, rounddot, squaredot, dashdot, longdash, longdashdot, longdashdotdot.
- `` beginArrow `` . Start type of arrow: block, open, classic, diamond, oval.
- `` endArrow `` . End type of arrow: block, open, classic, diamond, oval.
- `` width `` . Line-object width in pt.
- `` height `` . Line-object height in pt.
2017-05-15 22:49:02 +02:00
- `` flip `` . Flip the line element: true, false.
Comments
---------
Comments can be added to a document by using `` addComment `` .
The comment can contain formatted text. Once the comment has been added, it can be linked to any to any element.
.. code-block :: php
// first create a comment
$comment= new \PhpOffice\PhpWord\Element\Comment('Authors name', new \DateTime(), 'my_initials');
$comment->addText('Test', array('bold' => true));
// add it to the document
$phpWord->addComment($comment);
$textrun = $section->addTextRun();
$textrun->addText('This ');
$text = $textrun->addText('is');
// link the comment to the text you just created
$text->setCommentStart($comment);
If no end is set for a comment using the `` setCommentEnd `` , the comment will be ended automatically at the end of the element it is started on.