Forum


Replies: 3   Views: 1236
Section headers
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 adiXelp  · 10-06-2021 - 16:56

I have divided my content into sections where some section has header and some doesn't.

My problem over here is for sections that have headers I want to skip the first page and add headers only from the second page till the end of the section, how is this possible? please suggest.

Posted by admin  · 10-06-2021 - 18:08

Hello,

UPDATE: phpdocx 14 includes addHeaderSection and addFooterSection to accomplish this task. https://www.phpdocx.com/documentation/cookbook/headers-and-footers-for-sections

Are you generating the sections from scratch using phpdocx? Or do you need to add headers or footers to existing sections in a template?

If you are creating a DOCX with a single section using phpdocx you can use addHeader/addFotter methods setting default, even and first as array keys to insert each header/footer target of that section.

If you are creating a DOCX with multiple sections with their own headers and footers, then you need to use mergeDocx. Please check the documentation available on https://www.phpdocx.com/documentation/cookbook/headers-and-footers-for-sections.

There's no method to add new headers or footers to sections in existing documents. Using the importHeadersAndFooters method you can overwrite headers and footers of all sections importing the headers and footers from an external document. If you want to modify headers and footers from a template, they must exist in the template, so they can be changed using template methods or DOCXPath; there's no method to generate and add a header or footer to a section in a template.

Regards.

Posted by adiXelp  · 10-06-2021 - 20:24

I get my content in HTML format and use embedHTML() to add the contents to docx, I donot use any templates, I create sections by creating 3 seprate docx files for each section and then use mergeDocx to compile to one docx file.

In one of the docx files (section) I need to add headers, the header now is appearing for all the pages but my requirement is the header should skip the first page and start from the second page till the end of the section.

Please suggest.

Posted by admin  · 11-06-2021 - 05:16

Hello,

Using addHeader and addFooter methods you can set first, default and even header and footer contents when creating a single section in the DOCX.

For example, to add to the existing section an empty header in the first page and a default header in the next pages:

$docx = new CreateDocx();

$headerFragment_first = new WordFragment($docx, 'firstHeader');
$headerFragment_first->addText('');

$html = '<p>HTML content</p>';
$headerFragment_default = new WordFragment($docx, 'defaultHeader');
$headerFragment_default->embedHTML($html);

$docx->addHeader(
        array(
                'first' => $headerFragment_first,
                'default' => $headerFragment_default,
        )
);

$docx->addText('Text first page');
$docx->addBreak(array('type' => 'page'));
$docx->addText('Text second page');

$docx->createDocx('output');

This works for documents with a single section. As you are doing now, to generate documents with multiple sections with their own headers and footers, you need to create a DOCX for each section and them merge them using mergeDocx.

Regards.