A table of contents can be automatically inserted into a document to generate a list of the chapters or other important sections in the document. This feature is usually used together with cross references to add links to a table of contents. With the addition of counters, it can be complemented with the page numbers of the linked chapters.
A table of contents is created by using an XSL style sheet for the transformation of the document and a CSS style sheet to style the elements being added by the transformation.
The following sample of an XSL stylesheet generates a simple table of contents based on <h1> and <h2> tags in the document.
<xsl:template match="body">
<xsl:copy>
<h1><xsl:apply-templates select="//title//text()" /></h1>
<h2>Table of Contents</h2>
<table class="toc">
<xsl:for-each select=".//h1|.//h2">
<xsl:if test="name(.)='h1'">
<tr>
<td><xsl:apply-templates/></td>
</tr>
</xsl:if>
<xsl:if test="name(.)='h2'">
<tr>
<td><xsl:apply-templates/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
<xsl:apply-templates/>
<xsl:apply-templates select="@*"/>
</xsl:copy>
</xsl:template>In order to generate a table of contents using PDFreactor, there are two possibilities:
Apply the XSL stylesheet on the command line
Set it using an API method. In this case, you have to use the method addXSLTStyleSheet(String content, String uri). You can set the XSL content directly by using the first parameter of the method, or you set the path to an XSL stylesheet with the second.
If you use the command line, you can set an XSL stylesheet as follows:
java -jar pdfreactor.jar -a links bookmarks -s file:///C:/style.css -X file:///C:/xsl-style.xsl sample.html C:/sample.pdf
Here is an example using the PHP-API:
$pdfreactor->addXSLTStyleSheet(null, "style.xsl");
The table of contents added in the document can be styled using CSS:
table.toc {
border: hidden;
page-break-after: always;
}