There could be several reasons to divide a document into sections, the most common ones being:
- to change the page layout/size in the middle of a document from, for example, portrait to landscape or A4 to A3 to accomodate a particular content,
- to include a different set of headers and footers.
In order to be able to do that Docxpresso offers the section method.
Its public API, for obvious reasons, is quite similar to the one of the pageLayout method (see Document Layout) with the difference that this one allows us to create new sections while the first one only allow for the modification of pre-existing ones.
Signature
public section ([$options])
Parameters
- $options (type: array, optional).
This array has the following available keys and values:- orientation (type: string, default: portrait). The possible values are: portrait and landscape.
- paperSize (type: string, default: A4). The possible values are: A4, A3, letter and legal. These values are overriden, if set, by the width and height style options.
- style (type: string). A string of styles in CSS format.
The relevant CSS style type styles that one may apply to the new section include ((where ‘*’ in this context stands for top, right, bottom or left)).
- Size (if given, it will override the values of the paperSize option if any):
- width: specifies the total width of the new section page.
- height: specifies the total width of the new section page.
- Background:
- background: shorthand for background properties.
- background-color: sets the document background color.
- background-image: sets the document background image.
- background-position: sets the position of the background image.
- background-repeat: specifies if the background image will repeat or not.
- Borders:
- border: shorthand for border properties.
- border-color: sets the border color.
- border-style: sets the border line style.
- border-width: sets the border width.
- border-*-color: sets only the border color of one side.
- border-*-style: sets only the border line style of one side.
- border-*-width: sets only the border width of one side.
- Columns:
- column-count: sets the number of columns.
- column-gap: sets the gap between columns.
- column-rule: shorthand property for the column-rule-* properties.
- column-rule-color: sets the color of the rule between columns.
- column-rule-style: sets the style of the rule between columns.
- column-rule-width: sets the width of the rule between columns.
- columns: shorthand property for column-width and column-count.
- Margins and paddings:
- margin: specifies all margin widths in on shot.
- margin-*: specifies one margin at a time.
- padding: specifies all padding widths in on shot.
- padding-*: specifies one padding at a time.
- Other:
- direction or writing-mode: sets the text direction.
Let us start by inserting a new section in landscape mode:
<?php /** * This sample script inserts a new section in landscape mode */ require_once 'pathToDOCXPRESSO/CreateDocument.inc'; $doc = new Docxpresso\CreateDocument(); $format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf //insert some text in the first section $doc->paragraph() ->text(array('text' => 'This document has two sections. ')) ->text(array('text' => 'This is the first. ')); //insert the new section in landscape mode $doc->section(array('orientation' => 'landscape')); //insert some text in the second section $doc->paragraph() ->text(array('text' => 'This text belongs to the second section in landscape mode.')); //include in the render method the path where you want your document to be saved $doc->render('simple_section' . $format); //echo a link to the generated document echo 'You may download the generated document from the link below:<br/>'; echo '<a href="' . 'simple_section' . $format . '">Download document</a>';
Another typical case that requires the insertion of a new section is when one wants to change headers and footers in the middle of a document.
The following example illustrate this case:
<?php /** * This sample script inserts a new section with a different header */ require_once 'pathToDOCXPRESSO/CreateDocument.inc'; $doc = new Docxpresso\CreateDocument(); $format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf //insert a very simple header in the first section $doc->header()->paragraph(array('text' => 'SECTION 1 HEADER', 'style' => 'color:red')); //insert some text in the first section $doc->paragraph() ->text(array('text' => 'This document has two sections. ')) ->text(array('text' => 'This is the first. ')); //insert the new section $doc->section(); //insert a very simple header in the second section $doc->header()->paragraph(array('text' => 'SECTION 2 HEADER', 'style' => 'color:green')); //insert some text in the second section $doc->paragraph() ->text(array('text' => 'This text belongs to the second section with a different header.')); //include in the render method the path where you want your document to be saved $doc->render('section_header' . $format); //echo a link to the generated document echo 'You may download the generated document from the link below:<br/>'; echo '<a href="' . 'section_header' . $format . '">Download document</a>';
Notice that because of the different document format defaults the headers layout may vary from one to the other. If one looks for a consistent rendering in all formats one should play with the document layout and header styling.