Table of contents

Plumsail Documents DOCX templates support table of contents. It’s possible to tag template tokens as headings to dynamically generate new sections of a document, or hide some of them conditionally. A table of contents will be updated accordingly.

To better understand how it works, let’s take a look at a couple of examples.

How it works

You don’t need to declare any loops. The templating engine is smart enough to understand the structure of the source object applied to your document. If you refer to an object property inside a collection, it understands that we need to iterate it.

Let us say you’re working on a company report, and you need to dynamically create document sections for every department and its employees. JSON representation of the object is:

  "report": [
      "department": "Marketing",
      "employee": "Derek Clark"
      "department": "Sales",
      "employee": "Jessica Adams"
      "department": "Development",
      "employee": "Anil Mittal"

Tokens in the template will look like this:

{{report.department}} {{report.employee}}

Just tag them as H1 and H2:

tag headings

Add a table of contents:

table of contents in DOCX template

The template on the left side will result in the document on the right side:

result toc in DOCX

To start the first section on the separate page, just add a page break under the table of contents in the template.

To avoid appearing sections on separate pages, you can place the tokens inside the table. You can use invisible borders. We kept them for the picture to show how it works:

use table

Conditionally hide sections

You can use hide-block-if formatter to hide entire sections based on a particular condition. A table of contents will get updated accordingly.

For instance, you’d like to hide sections on the development department and not include them in the table of contents.

To achieve that, add {{report.department}:hide-block-if(development)} to the main token and tag as H1:

hide sections conditionally

As a result, the entire section for the development department will be hidden and not reflected in the table of contents:

toc with the hidden section Update TOC in the result document

Automatically update table of contents

There are 2 options to get a table of contents updated automatically.You need to download the template and open it in the Word desktop version to make the changes.

Download template
  1. You can configure Word setting in the template to update files before printing. This will update the Table of contents on printing.

Update TOC Word settings

If you select PDF as an output, this option is set automatically and the result PDF will contain an updated table of contents.

  1. Or you can add a macro to update the table of contents on open.

This is an example of a macro to update all fields of type ToC.

Sub TOCFieldUpdate()
Dim oField As Field
On Error Resume Next
For Each oField In ActiveDocument.Fields
If oField.Type = wdFieldTOC Then
End If
If oField.Type = wdFieldTOA Then
End If
Next oField
End Sub

It’s also possible to periodically execute the macro. The following macro updates the ToC every 5 minutes.

Public Sub ToCUpdate()
Call TOCFieldUpdate
Application.OnTime When:=Now + TimeValue("00:05:00"), name:="ToCUpdate"
End Sub