Forum


Replies: 7   Views: 4318
Table of contents : how to ?
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 davelavinsky  · 26-04-2016 - 11:25

Im looking at the sample code at http://www.phpdocx.com/api-documentation/word-content/add-table-of-contents-TOC-Word-document-with-PHP

What is crazyTOC.docx suppossed to be ?

Is there a full blown example of this ?

Thanks

Posted by admin  · 26-04-2016 - 12:05

Hello,

The example available on that page is complete. As you can read on this same page, the third parameter allows to use a TOC style from an existing DOCX; this parameter is optional.

Regards.

Posted by davelavinsky  · 26-04-2016 - 12:13

Im adding html to the docx using embedHTML.

How do I insert legend to it ?

$html = "";
$html .= "
<h1>Section 1</h1>
...
<h1>Section 2</h1>
    <h2>Success Factors</h2>
...
"
$docx->embedHTML($html);

 

Posted by davelavinsky  · 27-04-2016 - 06:48

I can't seem to get the Table of Contents to work with CreateDocxFromTemplate.

Posted by admin  · 27-04-2016 - 13:43

Hello,

If you need to add TOC in a template you need to add it when you create the document or use the MultiMerge class to merge it into the document.

Regards.

Posted by davelavinsky  · 27-04-2016 - 16:24

I did everything in the example mentioned in the doc. Only line that was changed was :

$docx = new CreateDocxFromTemplate("template.docx");

<?php
require_once 'path/to/phpdocx/phpdocx-corporate-5.5/classes/CreateDocx.inc';

$docx = new CreateDocxFromTemplate("template.docx");

$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'));

$docx->createDocx('example_addTableContents_1');    
?>

And the Table of Contents did not get generated. Only the text as it is.

Posted by admin  · 27-04-2016 - 17:07

Hello,

The addTableContents method allows to add a TOC in documents created from scratch. If you need a TOC when you use a template, you just need to add it in the DOCX when you create the template using Word, LibreOffice, OpenOffice...

Or as explained previously, you can use the MutiMerge class to merge a template with a TOC create from scratch.

Regards.