Creating a Table of Contents in a docx document with PHPdocx

  • May 04, 2011

This information is outdated, please, refer to the addTableContents documentation for up to date info.
A table of contents, or TOC, is usually an order list of chapters and subchapters that simplify the navigation trough the document. The TOC usually includes the titles or descriptions of the first-level headers, such as chapter titles in longer works, and often includes second-level or section titles (A-heads) within the chapters as well, and occasionally even third-level titles (subsections or B-heads). The depth of detail in tables of contents depends on the length of the work, with longer works having less. Formal reports (ten or more pages and being too long to put into a memo or letter) also have tables of contents. Documents of fewer than ten pages do not require tables of contents, but often have a short list of contents at the beginning.
In PHPdocx a TOC needs this code:

require_once '../../classes/';

$objDocx = new CreateDocx();

//This line define the Table of Contents for the document

//You need to add titles to populate the TOC
$paramsTitle = array(
'val' => 1,
'u' => 'single',
'font' => 'Blackadder ITC',
'sz' => 22

$docx->addTitle('Lorem ipsum dolor sit amet.', $paramsTitle);

//Remember to Refresh the TOC when you open de docx document with your word processor