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

There’s no need to manually declare loops; the templating engine intelligently recognizes the structure of the source object in your document. When you reference a property within a collection, it understands that iteration is required.

In this example, we’ll create a company report that dynamically generates sections for each department and its employees.

We’ll use the following JSON data:

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

Let’s add the following tokens to our template:

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

Next, change them to H1 and H2 headings:

tag headings

Finally, add a table of contents:

table of contents in DOCX template

Here’s the result:

result toc in DOCX

To start the first section on a new page, add a page break beneath the table of contents in your template.

To keep sections from starting on new pages, you can wrap the tokens in a table and make the table borders invisible. We kept the borders visible for the picture to show how it works:

use table

Conditionally hiding sections

You can use the #hide-if logical operator to hide entire sections based on specific conditions. This will also update the table of contents accordingly.

For example, if you want to hide sections related to the ‘Development’ department from the table of contents, you can do so by adding the following code next to your {{report.department}} token and tagging it as H1:

{{#hide-if report.department == "Development"}}

Conditionally hide sections

This way, the full section for the development department will be hidden and will not appear in the table of contents:

Update TOC in the result document

Automatically update table of contents for printing

If PDF is your chosen output format for the DOCX template, the table of contents will update automatically.

PDF output

For other output types, you’ll need to download the template and open it in Microsoft Word to update the table of contents manually.

Download template

Make sure to enable the ‘Update fields before printing’ option under ‘Display → Printing options’ for the table of contents to refresh automatically upon printing.

Update TOC Word settings

Automatically update table of contents upon opening the document

You can add a macro to your template to refresh the table of contents automatically. Follow these steps:

  • Open your DOCX template in Microsoft Word and navigate to the Visual Basic Editor.

Update TOC Macros
  • Copy and paste the following code into the editor:

Sub TOCUpdate()
Dim TOC As TableOfContents
For Each TOC In ActiveDocument.TablesOfContents
TOC.Update
Next
End Sub

Private Sub Document_Open()
Call TOCUpdate
End Sub
Update TOC Macros
  • Save the template as a .docm file to enable macro functionality:

Update TOC Macros Update TOC Macros
  • After generating a DOCX from the template in Power Automate, make sure you save it with the .docm extension:

Update TOC Macros

When you open the result file, Word will prompt you to enable editing and macros. Click ‘Enable content’ to update the table of contents.

Note

You may need to unblock the macros in the DOCX file settings after downloading. See this article for more information.

Update TOC Macros