Replies: 6   Views: 3806
Add different header to each section
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 khosro  · 07-03-2016 - 07:35


I wan to add different header to each section.In word you can add different header to each section if "Link to previouse" is disbled.

I wrote the following code in order to create different header for each section,but it only insert header for last section :


for ($i = 0; $i < 5; $i++) {

    if ($i !== 0) {
        $docx->addSection('nextPage', 'A4');

    $textOptions = array('fontSize' => 13, 'b' => 'on', 'color' => '567899',);

    $widthTableCols = array(700);

    $paramsTable = array('border' => 'nil', 'columnWidths' => $widthTableCols);

    $defaultHeader = 'defaultHeader' . $i;
    $default = 'default' . $i;

    $headerText = new WordFragment($docx, $defaultHeader);
    $headerText->addText('PHPDocX Header Title' . $i, $textOptions);

    $valuesTable = array(array(array('value' => $headerText, 'vAlign' => 'center')));

    $headerTable = new WordFragment($docx, $defaultHeader);
    $headerTable->addTable($valuesTable, $paramsTable);

    $docx->addHeader(array($default => $headerTable));
    $docx->addText('This document has a header'.$i);


Any idea to add different header to each section?



Posted by admin  · 07-03-2016 - 08:11


What license and version of phpdocx are you using? The easiest approach is using the MultiMerge class included in Corporate and Enterprise licenses that allow to merge DOCX keeping their own headers and footers.


Posted by khosro  · 07-03-2016 - 08:27

I think MultiMerge class merge two different DOCX documents.But i want to create a one DOCX from dynamic contents(Fetch from database).The for loop in my previous code iterates diffrent records from database and for each of them ,i want to apply different headers and put all of the in one DOCX document.

Posted by admin  · 07-03-2016 - 08:40


What license and version of phpdocx are you using? Using MultiMerge class you can create two documents dynamically (with different headers and footers) and then merge them to get a single DOCX keeping their sections, headers and footers.


Posted by khosro  · 07-03-2016 - 08:52

Thanks for your reply.Unfortunately we do not use Corporate and Enterprise licenses.

Posted by khosro  · 07-03-2016 - 09:26

According to ,MultiMerge class accpet DOCX document not phpdocx class which created by CreateDocx class.

In order to use MultiMerge,i must first save all dynamically created docx document and then mrege them.

I hope MultiMerge accept phpdocx class which created by CreateDocx class.

Posted by admin  · 07-03-2016 - 11:33


To merge documents using the Multimerge class you need to use files.