Forum


Replies: 3   Views: 900
Toc update before save?
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 dev@ram  · 18-05-2017 - 19:19

I have a question regarding the addTableContents('autoUpdate' =>true).

When we add a TOC like this, it brings up a popup when word opens the doc saying links need to be updated. Is there a way to update the TOC before we save it $docx->createDocxAndDownload() so that this popup does not come up since TOC was updated? I tried adding the code in the end before the save but that does not help either

Posted by dev@ram  · 19-05-2017 - 13:41

Ok thanks,

I added the following code

$docx->addText('Contents',array('pStyle' => 'Heading1'));
    $docx->addText(' ',array('pStyle' => 'Heading1Line'));
    $legend = array(
        'text' => 'Click here to update the TOC', 
        'color' => 'B70000', 
        'bold' => true, 
        'fontSize' => 12,
    );    
    $docx->addTableContents(array('autoUpdate' => true,'displayLevels' => '2-4'),$legend);
    $docx->addBreak(array('type' => 'page')); 

and the TOC is generated following the other contents. But when I open the generated file and try to update the TOC, I get the following error 'Creating a table of contents? Start by applying a heading style from the Styles gallery to the selected text. Any idea guys what this could be ?

 

Posted by admin  · 19-05-2017 - 14:57

Hello,

A content appears in the TOC if it has an outlineLvl tag and its value is in the range of the allowed values by the TOC. Maybe your paragraph styles Heading1 and Heading1Line don't include that tag (a name such as Heading doesn't create automatically an outlineLvl tag); internally, in OOXML, a heading style is a style with an outlineLvl tag. Phpdocx includes heading styles you can use:

https://www.phpdocx.com/api-documentation/word-content/insert-heading-Word-document-with-PHP

an options to set heading levels dynamically (headingLevel):

https://www.phpdocx.com/api-documentation/word-content/add-paragraph-text-Word-document-with-PHP

This is explained on https://www.phpdocx.com/documentation/cookbook/tables-of-contents (Adding items to the TOC section). We recommend you to read the documentation available on this page step by step and the example Core/addTableContents to understand how to use a TOC.

You can add contents to the TOC using a template or creating the document from scratch.

Regards.