News

Creating sections in docx documents with PHPdocx

  • May 04, 2011

Word processors uses section breaks to specify parts of a document that have different page orientation, columns, or headers and footers. Section breaks allow the user to specify where the different formatting will begin and end. You might use section breaks in the following circumstances:


Different headers and footers. If the document you are working on needs to have different headers and footers on various pages, you would use section breaks to achieve this.
Different numbering schemes. If you are working in a document where the Table of Contents needs lower case Roman numerals, the contract needs Arabic numerals, and the Appendices need alphabetic numerals, you can achieve all of these with section breaks.
Different paper sizes. If you want a document to contain one portrait page and one landscape page, you'll need a section break between the pages.
Different margins. If the first page of a letter needs a two-inch margin, and the following pages need a different margin, you'll need a section break in the document.

If you need to create a section using PHPdocx, you must use this kind of code:


require_once '../../classes/CreateDocx.inc';

$docx = new CreateDocx();

// Some text to populate the section
$text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, ' .
'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut ' .
'enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut' .
'aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit ' .
'in voluptate velit esse cillum dolore eu fugiat nulla pariatur. ' .
'Excepteur sint occaecat cupidatat non proident, sunt in culpa qui ' .
'officia deserunt mollit anim id est laborum.';

$docx->addText($text);

$paramsText = array(
'b' => 'single'
);

$docx->addText($text, $paramsText);

// Parameters for the Section
$paramsSection = array(
'orient' => 'landscape',
'top' => 4000,
'bottom' => 4000,
'right' => 4000,
'left' => 4000
);
// Start the section here
$docx->addSection($paramsSection);

$docx->addText($text);

$paramsText = array(
'b' => 'single'
);

$docx->addText($text, $paramsText);

// You need to create another section to close the current section and continue with the contents of the document. If you don't close the section, will be closed when you create the final document
$docx->createDocx('example_section');