Do you want to: Report a bug / Ask a how-to question
For feature suggestions, describe the result you would like to achieve in detail:
1. This question arises from my tests with the Custom Heading described in this post. (I’m using the same styles generated by the document builder script provided in that post.)
How can I apply custom styling to the Table of Contents? I haven’t found any guidance on this in the documentation. My goal is to achieve a style similar to the one shown below:
I’ve observed that the Table of Contents doesn’t inherit the custom styles used in the header paragraphs.
2. Additionally, while reviewing the documentation and testing this method, I noticed that the TOC settings do not recognize the custom styles. Instead, they appear as “undefined.” For example:
It seems the TOC does not automatically apply the styles defined for the headings.
Script used:
// create a text document file in the .docx format with ONLYOFFICE Document Builder
builder.CreateFile("docx")
var oDocument = Api.GetDocument();
var oDocument = Api.GetDocument();
var oParagraph = Api.CreateParagraph();
var oRun = oParagraph.AddText("Conteúdo");
oParagraph.SetBold(true);
oParagraph.SetFontSize(32);
oDocument.Push(oParagraph);
var aTocStyleLvl = [{Name: "Heading 1", Lvl: 1}, {Name: "Heading 2", Lvl: 2}];
var oTocPr = {"ShowPageNums": true, "RightAlgn": true, "LeaderType": "dot", "FormatAsLinks": true, "BuildFrom": {"StylesLvls": aTocStyleLvl}, "TocStyle": "current"};
oDocument.AddTableOfContents(oTocPr);
oParagraph = Api.CreateParagraph();
oDocument.Push(oParagraph);
//---------------- Create the custom style to use in sections titles ----------------//
var oNumbering = oDocument.CreateNumbering("numbered"); // This need to be created here for the use of the section components
var oNumLvl0 = oNumbering.GetLevel(0);
var sFormatString = "%1";
oNumLvl0.SetCustomType("decimal", sFormatString, "right");
var oNumLvl1 = oNumbering.GetLevel(1);
var sFormatString1 = "%1.%2";
oNumLvl1.SetCustomType("decimal", sFormatString1, "right");
// Heading 1
var oHeadingLvl1Style = oDocument.CreateStyle("Heading 1", "paragraph");
var oTextPr = oHeadingLvl1Style.GetTextPr();
oTextPr.SetBold(true);
oTextPr.SetColor(0, 0, 0, false);
oTextPr.SetFontSize(28);
oTextPr.SetFontFamily("FreeSans");
var oParaPr = oHeadingLvl1Style.GetParaPr();
oParaPr.SetOutlineLvl(0);
oParaPr.SetNumPr(oNumbering, 0);
// Heading 2
var oHeadingLvl2Style = oDocument.CreateStyle("Heading 2", "paragraph");
var oTextPr = oHeadingLvl2Style.GetTextPr();
oTextPr.SetBold(true);
oTextPr.SetColor(0, 0, 0, false);
oTextPr.SetFontSize(24);
oTextPr.SetFontFamily("FreeSans");
var oParaPr = oHeadingLvl2Style.GetParaPr();
oParaPr.SetOutlineLvl(0);
oParaPr.SetNumPr(oNumbering, 1);
// ------------------------------------------
var oParagraph = Api.CreateParagraph();
oParagraph.AddText("Heading 1 title (generated)");
oParagraph.SetStyle(oHeadingLvl1Style)
oParagraph.SetNumbering(oNumLvl0);
oParagraph.SetIndLeft(550);
oDocument.Push(oParagraph);
var oParagraph = Api.CreateParagraph();
oParagraph.AddText("This is some demonstrative paragraph just to fill out the page.");
oDocument.Push(oParagraph);
var oParagraph = Api.CreateParagraph();
oParagraph.AddText("Heading 2 title (generated)");
oParagraph.SetStyle(oHeadingLvl2Style)
oParagraph.SetNumbering(oNumLvl1);
oParagraph.SetIndLeft(550);
oDocument.Push(oParagraph);
var oParagraph = Api.CreateParagraph();
oParagraph.AddText("This is some demonstrative paragraph just to fill out the page.");
oDocument.Push(oParagraph);
oDocument.UpdateAllTOC(true);
// save the resulting text document as a file in the .docx format with the 'example.docx' name
builder.SaveFile("docx", "example.docx")
// close the text document file and finish work with ONLYOFFICE Document Builder
builder.CloseFile()
DocumentBuilder version: latest
Installation method: Downloaded the package from the official page and installed it using sudo dnf install <package name>
.
OS: Rocky Linux 9 in a docker container