Custom Heading Styling with Numbering

Do you want to: Report a bug / Ask a how-to question

I attempet making the suggestiong made by @Constantine in this post, to create a custom Heading styling with a custom Numbering.

I attempted to create a custom Numbering and apply it to the Heading 1, Heading 2 and Heading 3. However, I noticed that while Heading 1 works as expected, the Heading 2 and Heading 3 styles are being created without the Numbering for some reason. Am I missing something?

In the previous image, you can see that the headings 1, 1.1, 1.1.1, 2, and 2.1 were generated using the document builder. However, when I try to use the newly created styles in the document editor, the numbering in Heading 2 and Heading 3 does not appear.

Interestingly, in the document editor’s styles menu, the numbering is visible in the name of the Heading 1 Style, barely visible although. This behavior, however, does not apply to the other headings for some reason.

I’ve noticed that in the document editor, you can simply select the text and update the style using the selection, as shown in the first image:

This action effectively updates the style with the numbering, as demonstrated in the second image:

If there are no errors in my document builder script, and if it not possible to create the Heading 2and Heading 3 with Numbering in a custom Style, is it possible to perform this style update programmatically using the document builder?

Below, you’ll find a document builder sample along with the generated document:

// create a text document file in the .docx format with ONLYOFFICE Document Builder
builder.CreateFile("docx")

var oDocument = Api.GetDocument();

//---------------- 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");

var oNumLvl2 = oNumbering.GetLevel(2);
var sFormatString2 = "%1.%2.%3";
oNumLvl2.SetCustomType("decimal", sFormatString2, "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);

// Heading 3
var oHeadingLvl3Style = oDocument.CreateStyle("Heading 3", "paragraph");
var oTextPr = oHeadingLvl3Style.GetTextPr();
oTextPr.SetBold(true);
oTextPr.SetColor(0, 0, 0, false);
oTextPr.SetFontSize(22);
oTextPr.SetFontFamily("FreeSans");

var oParaPr = oHeadingLvl3Style.GetParaPr();
oParaPr.SetOutlineLvl(2);
oParaPr.SetNumPr(oNumbering, 2);

// Adding sections

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("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);

var oParagraph = Api.CreateParagraph();
oParagraph.AddText("Heading 3 title (generated)");
oParagraph.SetStyle(oHeadingLvl3Style)
oParagraph.SetNumbering(oNumLvl2);
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 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);

// 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()

HeadingExample.docx (26.3 KB)

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

Hello @mrmikept
Do I understand the situation correctly, that the issue is that Heading 2 and Heading 3 don’t include the numbering feature while you’re trying to use it manually in the editor (click the Heading > type a text)? Please confirm that.

Hi @Alexandre

Exactly. I generated the styles and numbering objects using the document builder. When I open the document, the styles are created, but the numbering is not applied automatically. The generated heading text includes the numbering object only because I explicitly assigned it to the paragraph using oParagraph.SetNumbering(oNumLvl1);.

Hello @mrmikept
Thank you for the clarification, we are checking the situation.

Hello @mrmikept
We are going to implement additional API method in v.8.3 to resolve this situation. We will contact you once we release v.8.3, so you can double-check the resolution.

1 Like

Hi @Alexandre

Thank you for letting me know! I’ll wait for it then :wink:

Do you have any idea when version 8.3 will be released?

Hello @mrmikept
We plan to release v8.3 in February.