Table of Contents Placement in DocumentBuilder

Issue Report: Table of Contents Placement in DocumentBuilder

Description of the Issue:
When adding a Table of Contents (TOC) to a document using DocumentBuilder, it is always placed on the first page, above all other content. This behavior occurs regardless of the intended page placement. I would like to place the TOC on a specific page of the document, but I have not been able to achieve this.

Steps to Reproduce:
1. Use the provided script to create a document with content and a TOC.
2. Add content and a page break before adding the TOC.
3. Generate and save the document as a PDF.

Example Script:

builder.CreateFile("docx");
var oDocument = Api.GetDocument();
var oParagraph = Api.CreateParagraph();

oParagraph.AddText("Content");
oParagraph.SetBold(true);
oParagraph.SetFontSize(32);
oDocument.Push(oParagraph);

oParagraph = Api.CreateParagraph();
oParagraph.AddText("Some text here!");
oParagraph.AddPageBreak();
oDocument.Push(oParagraph);

var oTocPr = {
  "ShowPageNums": true, 
  "RightAlgn": true, 
  "LeaderType": "dot", 
  "FormatAsLinks": true, 
  "BuildFrom": {"OutlineLvls": 9}, 
  "TocStyle": "standard"
};
oDocument.AddTableOfContents(oTocPr);

oParagraph = Api.CreateParagraph();
oParagraph.SetStyle(oDocument.GetStyle("Heading 1"));
oParagraph.AddText("First Heading");
oDocument.Push(oParagraph);

builder.SaveFile("pdf", "TOC_test.pdf");
builder.Close();

Observed Behavior:

  • The generated Table of Contents is always placed on the first page.
  • The Table of Contents does not include any references to the headings, which is understandable given the content order, but its placement on the first page is not desired.

Expected Behavior:

The Table of Contents should be placed on a specific page of the document, as intended. For instance, after a cover page.

Screenshots:

Generated document preview:

Additional Information:

  • DocumentBuilder Version: Latest
  • Installation Method:
    Downloaded the package from the official page and installed it using sudo dnf install <package name>.
  • Operating System: Fedora 40

Attachments:


Looking forward to guidance on how to properly place the TOC on a specific page. Thank you!

Hello @mrmikept

We will take a closer look at this behavior. Please await for the feedback.

1 Like

I was able to find out some info on TOC placement: it is actually placed to the cursor position. You can utilize MoveCursorToPos method to set required position for the cursor and call AddTableOfContents to insert TOC. For instance:

(function () {
    var oDocument = Api.GetDocument();
    var oParagraph = Api.CreateParagraph();
    oParagraph.AddText("Content");
    oParagraph.SetBold(true);
    oParagraph.SetFontSize(32);
    oDocument.Push(oParagraph);

    oParagraph = Api.CreateParagraph();
    oParagraph.AddText("Some text here!");
    oParagraph.AddPageBreak();
    oDocument.Push(oParagraph);

    oParagraph = Api.CreateParagraph();
    oParagraph.SetStyle(oDocument.GetStyle("Heading 1"));
    var oRun = oParagraph.AddText("First Heading");
    oDocument.Push(oParagraph);

    oRun.MoveCursorToPos(0);
    var oTocPr = {
        "ShowPageNums": true,
        "RightAlgn": true,
        "LeaderType": "dot",
        "FormatAsLinks": true,
        "BuildFrom": { "OutlineLvls": 9 },
        "TocStyle": "standard"
    };
    oDocument.AddTableOfContents(oTocPr);
})();
1 Like

Hello @Constantine

Thank you, it worked!
I suggest adding that information to the documentation of the Table of Contents for reference :wink:

1 Like

We’ll surely consider adding it.

1 Like