Forum


Replies: 7   Views: 1166
Add pagination and contents to the footers replacing them in an existing docx
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by admin  · 24-12-2021 - 11:10

Hello,

UPDATE: addHeaderSection and addFooterSection allow adding headers and footers per section. These methods include the removeOthers option to keep or remove existing headers and footers when adding new ones.

As explained in our previous reply, addFooter and addHeader methods work only with documents with one section (to generate DOCX documents from scratch with multiple sections and headers/footers the mergeDocx method must be used [https://www.phpdocx.com/documentation/cookbook/headers-and-footers-for-sections]).

To replace all headers and footers in an existing DOCX document with one or more sections you need to use the importHeadersAndFooters method.

You can generate a DOCX with headers/footers:

$docx = new CreateDocx();

$footer_html = '<p>Footer HTML</p>';

$numbering = new WordFragment($docx, 'defaultFooter');
$numbering->addPageNumber('numerical',array(
        'textAlign'=>'right',
        'bold'=>true,
        'sz'=>14,
        'color'=>'65a9ef',
));
$numbering->embedHTML($footer_html);
$docx->addFooter(array('default' => $numbering, 'first' => $numbering, 'even' => $numbering));

$docx->createDocx('docx_footers');

and then use it with importHeadersAndFooters to replace headers/footers in a DOCX template with one or more sections:

$docxNew = new CreateDocxFromTemplate('document_with_header_footer_multiple_sections.docx');
$docx->addBreak(array('type'=>'page'));
$docx->embedHTML($add_text);
$docx->importHeadersAndFooters('docx_footers.docx');
$docx->createDocx('new_document.docx');

Also note that break (addBreak method and 'type'=>'break' in DOCXPath) don't work with sections but with break tags. If you need to work with sections you need to use the addSection method and 'type' => 'section' in DOCXPath. The removeWordContent method included in DOCXPath removes contents; in the package you can find a lot of samples using it.

If you open a ticket (https://www.phpdocx.com/support) attaching a DOCX sample with multiple sections you are using, the dev team will generate a custom script using it.

Regards.