Snippets
- Learn phpdocx in 5 minutes
- Tutorial
- API quick guide
- HTML to Word
- HTML Extended
- Conversion plugin
- Word to HTML
- DOCXPath
- Bulk processing
- DOCXCustomizer
- Digital signature
- Cryptophpdocx
- Right to left languages
- phpdocx CLI command
- Tracking
- Artificial Intelligence
- Blockchain for documents
- JavaScript API
- Compiled mode
Adding contents at the bottom of the last page
MS Word doesn't support adding content only in the footer of the last page. Only default, first, and even targets are available in headers and footers.
To add a content at the bottom of the last page, a new section with a new footer can be generated using addSection and addFooterSection:
// a new document and a template with CreateDocxFromTemplate can be used
$docx = new CreateDocx();
$docx->addText('Content in the first section');
// add a new section
$docx->addSection();
$footerContent = new WordFragment($docx, 'firstFooter');
$footerContent->addText('Footer content');
// by default, addFooterSection adds the footers into the last section
$docx->addFooterSection(array('default' => $footerContent));
$docx->createDocx('output');
This code generates a new section with a new page where the new footer is added, so it may not be adequate.
Instead of generating a new section with a new footer, a floating table can be used to add content at the bottom of the last page in existing sections:
// a new document and a template with CreateDocxFromTemplate can be used
$docx = new CreateDocx();
$text = 'Content in the first page.';
$docx->addText($text);
// page break to illustrate the code. Page breaks are not needed to insert floating tables
$docx->addBreak(array('type' => 'page'));
$text = 'Content in the second page.';
$docx->addText($text);
// add a floating table as last content
$valuesTable = array(
array(11, 12, 13, 14),
array(21, 22, 23, 24),
);
$paramsTable = array(
'border' => 'single',
'tableAlign' => 'center',
'float' => array(
'align' => 'center',
),
);
$docx->addTable($valuesTable, $paramsTable);
// change only the last tblPr node to remove the default floating position and set the relative vertical position (w:tblpYSpec) as bottom and the vertical anchor (w:vertAnchor) as margin
$referenceNode = array(
'customQuery' => '//w:tblpPr[last()]',
);
// w:tblpY value is set as twips
$docx->customizeWordContent($referenceNode,
array('customAttributes' => array('w:tblpYSpec' => 'bottom', 'w:vertAnchor' => 'margin'))
);
$docx->createDocx('output');
Supported tblpYSpec values: center, inside, bottom, outside, inline and top.
Supported vertAnchor values: margin, page and text.
Table styles can be applied to hide borders, set margins...
DOCXCustomizer, available in Premium licenses, is required to run this code.
These snippets work with DOCX documents created from scratch and using DOCX templates.
DOCXPath allows inserting floating tables in specific sections.
LibreOffice supports floating tables since LibreOffice 7.6.