Replies: 2   Views: 2681
Replacing a variable by wordfragment (pagenumber)
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 k.barendrecht  · 13-06-2017 - 12:14

I'm trying to replace a template variable with a pagenumber, but I'm getting a strange error (not what I would expect).


From the docs:

// create a Word fragment to insert in the default header
$numbering = new WordFragment($docx, 'defaultHeader');
// set some formatting options
$options = array('textAlign' => 'right',
'bold' => true,
'sz' => 14,
'color' => 'B70000',
$numbering->addPageNumber('numerical', $options);
$docx->addHeader(array('default' => $numbering));

My implementation: (where $this is an instance of CreateDocxFromTemplate)

  function addPageNumberingToDocument()
        // create a Word fragment
        $numbering = new WordFragment($this);
        $options = [
            'textAlign' => 'right',
            'bold' => false,
            'sz' => 14,
            'color' => '000000'
        $numbering->addPageNumber('numerical', $options);

        // Replace variables(pagenumber) in header and footer
        $this->replaceVariableByWordFragment(['pagenumber' => $numbering], ['target' => 'header']);
        $this->replaceVariableByWordFragment(['pagenumber' => $numbering], ['target' => 'footer']);


How should I go about replacing a variable with a pagenumber?