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.
Every visible element in word is placed inside of a section. To create a section, use the following code:
$section = $phpWord->addSection($sectionStyle);
$sectionStyle is an optional associative array that sets the
$sectionStyle = array( 'orientation' => 'landscape', 'marginTop' => 600, 'colsNum' => 2, );
You can change a section page number by using the
style of the section.
// Method 1 $section = $phpWord->addSection(array('pageNumberingStart' => 1)); // Method 2 $section = $phpWord->addSection(); $section->getStyle()->setPageNumberingStart(1);
You can change a section layout to multicolumn (like in a newspaper) by
colsNum style of the section.
// Method 1 $section = $phpWord->addSection(array('breakType' => 'continuous', 'colsNum' => 2)); // Method 2 $section = $phpWord->addSection(); $section->getStyle()->setBreakType('continuous'); $section->getStyle()->setColsNum(2);
You can apply line numbering to a section by using the
style of the section.
// 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.
startLine numbering starting value
incrementLine number increments
distanceDistance between text and line numbering in twip
restartLine numbering restart setting continuous|newPage|newSection
Each section can have its own header reference. To create a header use
$header = $section->addHeader();
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. Additionally, only inside of the header reference you can add watermarks or background pictures. See “Watermarks” section.
You can pass an optional parameter to specify where the header/footer should be applied, it can be
Footer::AUTOdefault, all pages except if overridden by first or even
Footer::FIRSTeach first page of the section
Footer::EVENeach even page of the section. Will only be applied if the evenAndOddHeaders is set to true in phpWord->settings
To change the evenAndOddHeaders use the
getSettings method to return the Settings object, and then call the
Textruns, table cells, and footnotes are elements that can also act as containers. See the corresponding “Elements” section for the detail of each elements.