Replies: 11   Views: 4941
Addtablecontents in a specific page
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  · 18-07-2016 - 18:09


I have a template for my docx but i need  the table of content appear in page number 3 not in last page like now 

  $legend = array('text' => 'Presiona para actualziar el indice', 
    'color' => 'B70000', 
    'bold' => true, 
    'fontSize' => 12,

$docx->addTableContents(array('autoUpdate' => true), $legend);


How Can I put the table of content in a specific page?

Posted by  · 19-07-2016 - 13:29

ok  in my template add a table of content from Word  but nothing happeng  But if I add $docx->addTableContents update the table of content  but is add another table of content at end of document

//variable is just a example

$docx = new Phpdocx\Create\CreateDocxFromTemplate('Informe.docx');

$docx->replaceVariableByHTML('clienteNombre', 'inline', $variable, $estiloHtml);

$docx->replaceVariableByHTML('Propuesta', 'inline', $variable , $estiloHtml);

$docx->replaceVariableByText(array('PortadaNombreProyecto' =>$variable), array());

$org  = $something;

if($org ){         
        $docx->replaceVariableByHTML('ORG', 'block', $variable, $estiloHtml);

$docx->addTableContents(array('autoUpdate' => true), $legend);


$legend = array( 
    'color' => 'B70000', 
    'bold' => true, 
    'fontSize' => 12,
    'pageBreakBefore' => true
$docx->addTableContents(array('autoUpdate' => true), $legend);
/* Le damos un nombre random  */
$nombreInforme = "Poch_".str_replace(" ", "_", $variable)."_".date("Y-m-d");

/* Obligamos a que descargue  */


Posted by admin  · 19-07-2016 - 14:29


Sorry, but what do you mean by 'but nothing happend'? If your template has a TOC and you add headings to the document, when you update the TOC in Word the headings must appear.

Another approach, maybe it's easier for your specific case:

1. Create the first and second pages standalone in a DOCX.

2. Create the table of content using addTableContents in another DOCX.

3. Create the other content of the document in another DOCX.

4. Merge all DOCX using mergeDocx (


Posted by  · 19-07-2016 - 14:42

sorry my english when i said  'but nothing happend' if i not put

 $docx->addTableContents(array('autoUpdate' => true), $legend);

The TOC of my Document Word never updating BUT when a I added the TOC is repeated  at end of document


Posted by admin  · 19-07-2016 - 14:53


If your template already has a TOC then addTableContents 'force' adding another TOC. Please try the second approach explained on our previous update, it should be easier for your specific case.


Posted by  · 19-07-2016 - 15:36

my teamplate have a TOC because if I only add the script $docx->addTableContents this add a TOC at end of document not in other page and I need it in the third page for that reason I add a Table of content in my own template like you told me. But if i not add the script $docx->addTableContents the first TOC never updating



Posted by admin  · 19-07-2016 - 16:04


Our apologies for misunderstanding. You can't add a TOC in your template and then use addTableContents to add a TOC. If you do this, you're adding two TOCs. We asked you to add a TOC to your template, but without using the method addTableContents (this method adds other TOC keeping any other existing TOCs).

Word isn't like a PDF (it's more like HTML), and you don't know where a content will be or how many pages fill until you 'paint' (open) the document.

About adding content to new pages and not immediately after the existing content, you should create a new section ( or do a page break (

Please follow these steps to acomplish what you need:

1. Create the first and second pages standalone in a DOCX (using phpdocx or Word).

2. Create the table of content using addTableContents in another DOCX.

3. Create the other contents of the document in another DOCX.

4. Merge all DOCX using mergeDocx (


Posted by  · 19-07-2016 - 16:26

ok I understand you....only a question about the second point 

2. Create the table of content using addTableContents in another DOCX

in the example sample_1.php

require_once '../../../Classes/Phpdocx/Create/';

$docx = new Phpdocx\Create\CreateDocx();
$docx->addText('Table of Contents', array('bold' => true, 'fontSize' => 14));
$legend = array('text' => 'Click here to update the TOC', 
    'color' => 'B70000', 
    'bold' => true, 
    'fontSize' => 12);
$docx->addTableContents(array('autoUpdate' => true), $legend);
//we add now some headings so tehy show up in the TOC
$docx->addText('Chapter 1', array('pStyle' => 'Heading1PHPDOCX'));
$docx->addText('Section', array('pStyle' => 'Heading2PHPDOCX'));
$docx->addText('Another TOC entry', array('pStyle' => 'Heading3PHPDOCX'));


 In this file create the table of content from 3$docx->addText and you told me that i only create the table of content and this content in other page

How I can create only the table of content  and not text then of this TOC ?? Because if i not add text or another thing the document put 

No se encontraron elementos de tabla de contenido.

Tabla de contenido
1.      INTRODUCCIÓN       4
2.      ANTECEDENTES    5
3.      ALCANCES        6




Posted by admin  · 19-07-2016 - 16:34


You don't fill the content of a TOC; Word does this task automatically (it may require pressing F9 to update it) from the entries marked with heading styles. After all documents have been merged, the TOC will be able to show all headings.


Posted by  · 19-07-2016 - 20:19

Sorry but This not work for my I try to do exaclty that you say 

          $docx1 = new Phpdocx\Create\CreateDocx();
                $docx1->addText('Chapter 1', array('pStyle' => 'Heading1PHPDOCX'));
                $docx1->addText('Section', array('pStyle' => 'Heading2PHPDOCX'));
                $docx1->addText('Another TOC entry', array('pStyle' => 'Heading3PHPDOCX'));

                $docx2 = new Phpdocx\Create\CreateDocx();

                $legend = array('text' => 'Click here to update the TOC', 
                    'color' => 'B70000', 
                    'bold' => true, 
                    'fontSize' => 12);
                $docx2->addTableContents(array('autoUpdate' => false), $legend);


                $docx3 = new Phpdocx\Create\CreateDocx();
                $docx3->addText('Chapter 2', array('pStyle' => 'Heading1PHPDOCX'));
                $docx3->addText('Section 2', array('pStyle' => 'Heading2PHPDOCX'));
                $docx3->addText('Another TOC entry 2', array('pStyle' => 'Heading3PHPDOCX'));

                $merge = new Phpdocx\Utilities\MultiMerge();

                $merge->mergeDocx('doc1.docx', array( 'indice.docx','doc2.docx'), 'example_merge_docx.docx', array('mergeType' => 1, 'numbering' => 'continue'));

                $docx = new Phpdocx\Create\CreateDocxFromTemplate('example_merge_docx.docx');
                $docxSettings = array('zoom'=>110);

But the tablecontent not update nothing is like the 3 docx was independence one of other  ... I need this work automatic not press f9 or another thing please


Posted by admin  · 20-07-2016 - 07:50


Please send to contact[at] these files: doc1.docx, doc2.docx and indice.docx 

About updating a TOC, you can't force Word to update it (Word security block this kind of actions). You can ask Word to update the TOC when you open the document using this line:

$docx2->addTableContents(array('autoUpdate' => true), $legend);

but it will work only on some versions/installations of Word. You can use a macro as well.