Forum


Replies: 3   Views: 72
Remove left and right margins in header / footer

Posted by InsaneMe  · 17-04-2025 - 14:40

Hello,

I'm having an issue.

I'd like to create a DOCX file but remove the margins of the header and footer.

The goal is to be able to modify them dynamically — for example, 5px on the right, 2px on the left, or no margins at all (no top, bottom, or right margins, etc.).

That way, I could, for example, position an image at the very left and right edges, allowing me to add an image that appears in the corner of the page.

I've gone through the documentation, spent a lot of time on this, and haven't gotten any results.

At the moment, I'm testing with an image since I also need that functionality, but I haven't had any success.

Here's a simple code example:

 

      $docx = new CreateDocx();

        $docx->modifyPageLayout('A4', [
            'headerMargin' => 0,
            'topMargin' => 0,
            'rightMargin' => 0,
            'leftMargin' => 0,
            'bottomMargin' => 0,
        ]);

        $header = new WordFragment($docx, 'defaultHeader');

        $emptyFragment = new WordFragment($docx, 'defaultHeader');
        $emptyFragment->addText('', ['fontSize' => 1]);

        $imageFragment = new WordFragment($docx, 'defaultHeader');
        $imageFragment->addImage([
            'src' => 'data:image/jpeg;base64,' . self::BASE64,
            'scaling' => 30,
            'textWrap' => 0,
        ]);

        $header->addTable(
            [[$emptyFragment, $imageFragment]],
            [
                'border' => 'none',
                'columnWidths' => [9900, 100],
                'tableAlign' => 'right',
                'tableWidth' => ['type' => 'pct', 'value' => 100],
                'cellMargin' => 0,
                'cellPadding' => 0,
                'indent' => 0,
                'offset' => 0,
                'cellSpacing' => 0,
                'borderWidth' => 0,
            ]
        );

        $docx->addHeader(['default' => $header]);

This code is just a hardcoded test, because I've tried so many things, whether using a table in the header or embedHTML,and I still don’t know how to make it work. Or even if it’s actually possible to do.

Thank you in advance for your help! Feel free to ask if you need more information.

Posted by admin  · 17-04-2025 - 17:03

Hello,

What version and license of phpdocx are you using?

Your username doesn't have a license. Please send to contact[at]phpdocx.com the username or email address of the user that purchased the license you are using.

There are the following approaches to accomplish the requested task:

  • Setting custom header and footer margins and contents with custom indentations.
  • Adding floating images or tables.
  • Use custom templates.

Also, please note that modifyPageLayout includes the following options to set margins: marginTop, marginRight, marginBottom, marginLeft, marginHeader, and marginFooter as detailed on the API documentation page (https://www.phpdocx.com/api-documentation/layout-and-general/modify-Word-page-layout-with-PHP). In your code, you are using: headerMargin, topMargin,... that aren't correct.

Please send the information about the license you are using to contact[at]phpdocx.com.

Regards.

Posted by legalpilot  · 18-04-2025 - 15:25

I am the author of this post using the account that has the premium license. 

I’ve already tried using modifyPageLayout with, for exemple : 

marginHeader : 0

marginFooter : 0

marginLeft : 200

marginRight : 200

 

If I set marginLeft to 200, it applies to the whole document, including the header, but what I need is to control the margins left and right specifically for the header and the footer.

As I mentioned, I’d like to be able to place an image or some text in the header right at the edge of the document, so with 0 margin top and left, but i want to have 200 marginLeft for the rest of the document

 

Posted by admin  · 18-04-2025 - 18:43

Hello,

Thanks for sending the information about the license you are using.

Please note that a DOCX document doesn't have specific left and right margins for headers or footers. Left and right margins are common to the whole document.
Top and bottom margins have specific styles you can change by using marginTop, marginBottom, marginHeader, and marginFooter options available in modifyPageLayout and addSection methods.

In this case, after setting the top/header/bottom/footer margins, the easiest approach is to set a negative left indentation for the content of the header/footer to get the needed output. addTable, addText, and other phpdocx methods include options to set custom indentations.

 

Also, please note the support included in each license as explained at https://www.phpdocx.com/support#howMuchSupport (What support includes my license? section).

The license you are using was purchased nearly one year ago and doesn't include LUS, so support is limited to general questions and doubts about using phpdocx. For technical support without opening support tickets (https://www.phpdocx.com/support), LUS must be active in your license or a license/upgrade purchased in the last six months.

To get technical support, please:

  • Upgrade your license to the latest version of phpdocx. We also recommend that you include LUS when you purchase the upgrade. You can upgrade your license on "MY PHPDOCX" page after login.
  • Or open a support ticket: https://www.phpdocx.com/support

After you have upgraded your license or opened a support ticket, we can provide further technical support. For example, generating a whole custom sample script to illustrate how to accomplish the requested task by adding tables and images to headers and footers.

Regards.