PHPWord/docs/containers.rst

134 lines
3.5 KiB
ReStructuredText
Raw Normal View History

2014-03-19 23:08:44 +07:00
.. _containers:
Containers
==========
Containers are objects where you can put elements (texts, lists, tables,
etc). There are 3 main containers, i.e. sections, headers, and footers.
There are 3 elements that can also act as containers, i.e. textruns,
table cells, and footnotes.
2014-03-19 23:08:44 +07:00
Sections
--------
Every visible element in word is placed inside of a section. To create a
section, use the following code:
2014-03-21 10:04:32 +01:00
.. code-block:: php
2014-03-19 23:08:44 +07:00
2014-06-08 17:10:02 +07:00
$section = $phpWord->addSection($sectionStyle);
2014-03-19 23:08:44 +07:00
2014-06-08 17:10:02 +07:00
The ``$sectionStyle`` is an optional associative array that sets the
2014-03-19 23:08:44 +07:00
section. Example:
2014-03-21 10:04:32 +01:00
.. code-block:: php
2014-03-19 23:08:44 +07:00
2014-06-08 17:10:02 +07:00
$sectionStyle = array(
2014-03-19 23:08:44 +07:00
'orientation' => 'landscape',
'marginTop' => 600,
'colsNum' => 2,
);
Page number
~~~~~~~~~~~
2014-03-19 23:08:44 +07:00
You can change a section page number by using the ``pageNumberingStart``
style of the section.
2014-03-19 23:08:44 +07:00
2014-03-21 10:04:32 +01:00
.. code-block:: php
2014-03-19 23:08:44 +07:00
// Method 1
$section = $phpWord->addSection(array('pageNumberingStart' => 1));
// Method 2
$section = $phpWord->addSection();
$section->getStyle()->setPageNumberingStart(1);
2014-03-19 23:08:44 +07:00
Multicolumn
~~~~~~~~~~~
You can change a section layout to multicolumn (like in a newspaper) by
using the ``breakType`` and ``colsNum`` style of the section.
2014-03-21 10:04:32 +01:00
.. code-block:: php
// Method 1
$section = $phpWord->addSection(array('breakType' => 'continuous', 'colsNum' => 2));
// Method 2
$section = $phpWord->addSection();
$section->getStyle()->setBreakType('continuous');
$section->getStyle()->setColsNum(2);
2014-06-08 17:10:02 +07:00
Line numbering
~~~~~~~~~~~~~~
You can apply line numbering to a section by using the ``lineNumbering``
style of the section.
.. code-block:: php
// Method 1
$section = $phpWord->addSection(array('lineNumbering' => array()));
// Method 2
$section = $phpWord->addSection();
$section->getStyle()->setLineNumbering(array());
Below are the properties of the line numbering style.
- ``start`` Line numbering starting value
- ``increment`` Line number increments
- ``distance`` Distance between text and line numbering in twip
2014-06-08 17:10:02 +07:00
- ``restart`` Line numbering restart setting
continuous\|newPage\|newSection
2014-03-19 23:08:44 +07:00
Headers
-------
Each section can have its own header reference. To create a header use
the ``addHeader`` method:
2014-03-19 23:08:44 +07:00
2014-03-21 10:04:32 +01:00
.. code-block:: php
2014-03-19 23:08:44 +07:00
$header = $section->addHeader();
2014-03-19 23:08:44 +07:00
Be sure to save the result in a local object. You can use all elements
that are available for the footer. See "Footer" section for detail.
2014-03-19 23:08:44 +07:00
Additionally, only inside of the header reference you can add watermarks
or background pictures. See "Watermarks" section.
2014-03-19 23:08:44 +07:00
You can pass an optional parameter to specify where the header/footer should be applied, it can be
- ``Footer::AUTO`` default, all pages except if overridden by first or even
- ``Footer::FIRST`` each first page of the section
- ``Footer::EVEN`` each even page of the section. Will only be applied if the evenAndOddHeaders is set to true in phpWord->settings
2014-03-19 23:08:44 +07:00
Footers
-------
Each section can have its own footer reference. To create a footer, use
the ``addFooter`` method:
2014-03-19 23:08:44 +07:00
2014-03-21 10:04:32 +01:00
.. code-block:: php
2014-03-19 23:08:44 +07:00
$footer = $section->addFooter();
2014-03-19 23:08:44 +07:00
Be sure to save the result in a local object to add elements to a
footer. You can add the following elements to footers:
- Texts ``addText`` and ``createTextrun``
- Text breaks
- Images
- Tables
- Preserve text
See the "Elements" section for the detail of each elements.
Other containers
----------------
Textruns, table cells, and footnotes are elements that can also act as
containers. See the corresponding "Elements" section for the detail of
each elements.