Snippets

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:

x
 
1
// a new document and a template with CreateDocxFromTemplate can be used
2
$docx = new CreateDocx();
3
4
$docx->addText('Content in the first section');
5
6
// add a new section
7
$docx->addSection();
8
9
$footerContent = new WordFragment($docx, 'firstFooter');
10
$footerContent->addText('Footer content');
11
12
// by default, addFooterSection adds the footers into the last section
13
$docx->addFooterSection(array('default' => $footerContent));
14
15
$docx->createDocx('output');
16

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:

38
 
1
// a new document and a template with CreateDocxFromTemplate can be used
2
$docx = new CreateDocx();
3
4
$text = 'Content in the first page.';
5
$docx->addText($text);
6
7
// page break to illustrate the code. Page breaks are not needed to insert floating tables
8
$docx->addBreak(array('type' => 'page'));
9
10
$text = 'Content in the second page.';
11
$docx->addText($text);
12
13
// add a floating table as last content
14
$valuesTable = array(
15
    array(11, 12, 13, 14),
16
    array(21, 22, 23, 24),
17
);
18
19
$paramsTable = array(
20
    'border' => 'single',
21
    'tableAlign' => 'center',
22
    'float' => array(
23
        'align' => 'center',
24
    ),
25
);
26
$docx->addTable($valuesTable, $paramsTable);
27
28
// 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
29
$referenceNode = array(
30
    'customQuery' => '//w:tblpPr[last()]',
31
);
32
// w:tblpY value is set as twips
33
$docx->customizeWordContent($referenceNode,
34
    array('customAttributes' => array('w:tblpYSpec' => 'bottom', 'w:vertAnchor' => 'margin'))
35
);
36
37
$docx->createDocx('output');
38

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.

­
­