Forum


Replies: 1   Views: 276
Adding footers in specific sections
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 mybuh  · 27-02-2025 - 09:21

Hello.

I create a document with library (CreateDocx()) . I create a footer in it. Then I insert content from the template. After saving the document, the footer is not showed. I also tried loading a document from a template and inserting a footer in it. But the footer is not showed. I did all these actions through method addFooterSection and addFooter. The result is negative. The code is taken from the example. I can attach the docx file, let me know where.

require_once 'phpdocx/classes/CreateDocx.php';

$docx = new \CreateDocxFromTemplate('agreement.docx');
$docx->removeFooters();
$footerTextFirstSection = new WordFragment($docx, 'firstFooter');
$footerTextFirstSection->addText('Footer in the first section');

// create a Word fragment with an image to be inserted in the footer of the document
$imageOptions = array(
        'src' => 'tops.jpg',
        'dpi' => 300,
);
$footerImageFirstSection = new WordFragment($docx, 'defaultFooter');
$footerImageFirstSection->addImage($imageOptions);
// by default, addFooterSection adds footers into the last section
$docx->addFooterSection(array('first' => $footerTextFirstSection, 'default' => $footerImageFirstSection));


// create a new section
$docx->addSection();

// add footers into the new section

// contents with dependencies such as images and links share the same internal header/footer scope for all sections in phpdocx.
// These contents must be created and added in order when adding headers and footers per section
$footerTextSecondSection = new WordFragment($docx, 'firstFooter');
$footerTextSecondSection->addText('Footer in the second section');

$footerContentSecondSection = new WordFragment($docx, 'defaultFooter');
$imageOptions = array(
        'src' => 'tops.jpg',
        'dpi' => 300,
    'textWrap' => 1,
);
$footerContentSecondSection->addImage($imageOptions);

// add a page numbering
$style = array(
    'bold' => true,
    'color' => 'B70000',
    'fontSize' => 24,
);
// create a custom style
$docx->createParagraphStyle('pgStyle', $style);
// set some formatting options
$options = array(
    'textAlign' => 'right',
    'pStyle' => 'pgStyle',
);
$footerContentSecondSection->addPageNumber('numerical', $options);
$footerContentSecondSection->addBreak();

// by default, addFooterSection adds the footers into the last section
$docx->addFooterSection(array('first' => $footerTextSecondSection, 'default' => $footerContentSecondSection));


$docx->createDocx('output');

 

Posted by admin  · 27-02-2025 - 11:00

Hello,

We have checked your DOCX and script. Please note the following:

  • If we run your code, the new footers appear in the chosen sections (the last section as default using addFooterSection). MS Word displays them correctly.
  • The last section from your template (not the new section added with addSection, but the last existing section in your "agreement.docx" file), uses a continuous section type. First page target in headers and footers in MS Word works with Next Page Breaks, not continuous type. If instead of a continuous section type, you set a Next Page type, the first page footer (and other footer targets) of the section appears as needed. Using a template, you need to set the correct section styles/types (layouts) in the template to generate the needed output. If you want to change these styles on the fly, for example changing a section from continuous to next, you can use DOCXCustomizer (https://www.phpdocx.com/api-documentation/docxcustomizer/customize-docx-Word-documents-PHP).

We have checked that everything is working correctly and following the MS Word requirements to get all possible outputs.

Regards