Forum


Replies: 2   Views: 676
Replace variable with page number as merge field
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 ceff  · 06-07-2022 - 07:50

Hello,

We are trying to replace a variable inside a DOCX template with the page number of the document.
We tried the following code:

$pageNumberFragment = new WordFragment($this->docx, "defaultFooter");
$pageNumberFragment->addPageNumber();
$this->docx->replaceVariableByWordFragment([
    "pageNumber" => $pageNumberFragment
], [
    "target" => "footer",
    "type" => "inline"
]);

But the resulting DOCX does not have a merge field, ony a "1" simple text.

Is this a limitation of our phpdocx version, or are we doing something wrong?
We have the advanced license for 7.5.

Thank you in advance.

Posted by admin  · 06-07-2022 - 08:22

Hello,

Yes, it's a limitation of the old version you are using. We recommend you upgrade to the latest release; you can test this same method using the trial package (https://www.phpdocx.com/download_trial) before upgrading.

Using phpdocx 7, to keep the field tags included in addPageNumber you need to do a block type replacement instead on the inline type replacement your code is using:

"type" => "block"

Regards.

Posted by ceff  · 06-07-2022 - 09:26

I see. Thank you for your anwer, I will look into this.