Forum


Replies: 3   Views: 34
Force header "different first page" option to false

Posted by sdespont  · 01-07-2026 - 14:49

Hello,

From an imported Word template, I need to force the option of the first page header "Different First Page" to FALSE.

How can I do this ?

Thanks

Posted by admin  · 01-07-2026 - 15:51

Hello,

You can disable the w:titlePg tag from the sections using customizeWordContent available in Premium licenses. As w:titlePg is not one of the predefined styles available in customizeWordContent, you need to use a custom attribute:

$docx = new CreateDocxFromTemplate('template.docx');

$customAttributes = array(
    'customAttributes' => array(
        'w:val' => '0',
    ),
);

// update the document sections (all but the last section)
$referenceNode = array(
    'customQuery' => '//w:sectPr/w:titlePg',
);
$docx->customizeWordContent($referenceNode, $customAttributes);

// update the last section
$referenceNode = array(
    'target' => 'lastSection',
    'customQuery' => '//w:sectPr/w:titlePg',
);
$docx->customizeWordContent($referenceNode, $customAttributes);

$docx->createDocx('output');

If you send to contact[at]phpdocx.com a specific DOCX, we'll generate a custom sample script using it.

Regards.

Posted by sdespont  · 02-07-2026 - 08:30

Hello,

thanks for your reply, I thought about this solution :

$this->docx = new CreateDocxFromTemplate($templatePathPrefix.$filePath);   
     
// Retrieve template variables
$this->wordTemplateVariablesArray = $this->docx->getTemplateVariables();

// Re-import the headers and footers with setDefault as TRUE
$this->docx->importHeadersAndFooters($templatePathPrefix.$filePath, 'headerAndFooter', ['setDefault' => true]);

This works correctly and is simpler than the complex call to `customizeWordContent`. However, a small problem remains: when `importHeadersAndFooters` is used, the variables of the imported header and footer are not loaded for replacement. Could you check if it would be possible to load the new variables when loading the headers and footers via `importHeadersAndFooters`?

Thank you.

Posted by admin  · 02-07-2026 - 10:07

Hello,

Please note that the importHeadersAndFooters method may not generate the exact output in your case because the setDefault option changes all targets, including the "even" position in headers/footers.
For this specific case, we recommend using the DOCXCustomizer approach detailed in our previous reply.

Regarding the importHeadersAndFooters method, please note that this is a CreateDocx method (although CreateDocxFromTemplate inherits it).
In this case, the imported headers and footers are not available unless you load the DOCX containing the imported headers and footers as a new template:

$docx = new CreateDocxFromTemplate('sample_1_upd.docx');
$wordTemplateVariablesArray = $docx->getTemplateVariables();

$docx->importHeadersAndFooters('sample_1_upd.docx', 'headerAndFooter', ['setDefault' => true]);

$docx->createDocx('sample_1_upd_importHeadersAndFooters');

$docx = new CreateDocxFromTemplate('sample_1_upd_importHeadersAndFooters.docx');
$wordTemplateVariablesArray = $docx->getTemplateVariables();
$docx->createDocx('output');

You could also use in-memory DOCX (https://www.phpdocx.com/documentation/cookbook/in-memory-docx-documents) to avoid generating temp files:

$docx = new CreateDocxFromTemplate('sample_1_upd.docx');
CreateDocx::$returnDocxStructure = true;
$wordTemplateVariablesArray = $docx->getTemplateVariables();

$docx->importHeadersAndFooters('sample_1_upd.docx', 'headerAndFooter', ['setDefault' => true]);
$docxStructureTemplate = $docx->createDocx();
CreateDocx::$returnDocxStructure = false;

$docx = new CreateDocxFromTemplate($docxStructureTemplate);
$wordTemplateVariablesArray = $docx->getTemplateVariables();
$docx->createDocx('output');

We have forwarded this task to the dev team to improve this workflow in future releases of phpdocx.

Regards.