The generation of TOCs does require of a full Docxpresso installation.

The tables of contents, or TOCs in the following, are a common and useful way to simplify the browsing and navigation through the contents of a long document.

Docxpresso allows for the generation of TOCs trough the toc method.

The user may specify:

  • Its title.
  • The depth level.
  • If the entries allow direct navigation to the referred contents.
  • The character (usually a dot) that joins each entry with its corresponding page in the document.
  • If the TOC will be updated automatically on openning.

Its public API is summarized as follows:

Signature

public section ([$options])

Parameters

  • $options (type: array, optional). This array has the following available keys and values:

    • auto-update (type: boolean, default: true). This parameter determines if the TOC will be automatically updated or not. This option is ignored unless the target document is in Open Document Format (.odt).
    • leader-char (type: string, default: ‘.’). The character that will join the TOC entry to its corresponding page number.
    • linked (type: boolean, default: true). If true the TOC will allow direct navigation to the document contents.
    • outline-level (type: integer, default: 3). The depth of the TOC, i.e. if set, for example, to 6 the TOC will only contain headings up to that level.
    • title (type: string). The TOC title, if any.
    • style (type: array). An array of styles in CSS format. styles[0] will be applied to the title while styles[n] will be applied to the n-th level TOC entry. Beware that you do not need to style all levels if the default styling fits your need. If you only set, for example, style[2] the other levels will show the default formatting while the entries with heading level of 2 will include the required styling options.

The relevant CSS style type styles that may apply to the different TOC levels are the ones use for paragraphs so we refer to its documentation for detailed information (see Document Paragraphs).

Let us start by a simple example that illustrates the basic functionality of this method:

<?php
/**
 * This sample script inserts a default TOC
 */
require_once 'pathToDOCXPRESSO/CreateDocument.inc';
$doc = new Docxpresso\createDocument();
$format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf
$doc->paragraph(array('text' => 'Let us add a TOC to the document with just the default options:'));
//insert the TOC
$doc->toc();
//insert some headings and content so we can populate the TOC
for ($j=1; $j<5; $j++) {
    $doc->heading(array('level' => 1, 'text' => 'The Title number:' . $j, 'style' => 'page-break-before: always'));
    $doc->heading(array('level' => 2, 'text' => 'A subtitle of the title number: ' . $j));
    $doc->paragraph(array('text' => 'Some random text.'));
}
//include in the render method the path where you want your document to be saved
$doc->render('simple_toc' . $format); 
//echo a link to the generated document
echo 'You may download the generated document from the link below:<br/>';
echo '<a href="' . 'simple_toc' . $format . '">Download document</a>';

DOWNLOAD:download pdfdownload docdownload docxdownload odtdownload rtf

The TOCs are shown in Libre and Open Office with an ugly grey background that is not printed and its only purpose is to notify the user that that content has been automatically generated. The user may choose to deactivate that option in the interface but it is not an styling option and as such can not be deactivated beforehand.

Let us now work a somehow more sophisticated example where we introduce a little more of formatting:

<?php
/**
 * This sample script inserts a default TOC
 */
require_once 'pathToDOCXPRESSO/CreateDocument.inc';
$doc = new Docxpresso\createDocument();
$format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf
$doc->paragraph(array('text' => 'Let us add a TOC with some formatting options:'));
//insert the TOC
$TOCStyle = array();
$TOCStyle[0] = 'font-family: Cambria; color: #b70000; font-size: 16pt';
$TOCStyle[1] = 'font-family: Arial; color: #333333; font-size: 12pt; margin-top: 3pt';
$TOCStyle[2] = 'font-family: Arial; color: #555555; font-size: 11pt; font-style: italic';
$doc->toc(array('title' => 'Table of Contents', 'style' => $TOCStyle));
//insert some headings and content so we can populate the TOC
for ($j=1; $j<5; $j++) {
    $doc->heading(array('level' => 1, 'text' => 'The Title number:' . $j, 'style' => 'page-break-before: always'));
    $doc->heading(array('level' => 2, 'text' => 'A subtitle of the title number: ' . $j));
    $doc->paragraph(array('text' => 'Some random text.'));
}
//include in the render method the path where you want your document to be saved
$doc->render('toc' . $format); 
//echo a link to the generated document
echo 'You may download the generated document from the link below:<br/>';
echo '<a href="' . 'toc' . $format . '">Download document</a>';

DOWNLOAD:download pdfdownload docdownload docxdownload odtdownload rtf