Hello, I have some problems When I use ONLYOFFICE Document Builder.
Question: Use oDocument.UpdateAllTOC(false)
do not take effect, and the page numbers of TOC is incorrect.
DocumentBuilder version: 7.5
Installation method: download from ONLYOFFICE Api Documentation - Get ONLYOFFICE Document Builder
OS: Windows 11
The code is as follows:
builder.CreateFile("docx");
var oDocument = Api.GetDocument();
var oParagraph;
oParagraph = Api.CreateParagraph();
oParagraph.AddText("目录");
oDocument.InsertContent([oParagraph]);
var oTocPr = {"ShowPageNums": true, "RightAlgn": true, "LeaderType": "dot", "FormatAsLinks": true, "BuildFrom": {"OutlineLvls": 9}, "TocStyle": "standard"};
oDocument.AddTableOfContents(oTocPr);
oParagraph = Api.CreateParagraph();
oParagraph.AddPageBreak();
oDocument.InsertContent([oParagraph]);
var oNewDocumentStyle1 = oDocument.GetStyle("Heading 1");
var oNewDocumentStyle2 = oDocument.GetStyle("Heading 2");
for (i = 1; i <= 3; i++) {
oParagraph = Api.CreateParagraph();
oParagraph.SetStyle(oNewDocumentStyle1);
oParagraph.AddText("第 " + i + " 章 标题");
oDocument.InsertContent([oParagraph]);
for (j = 1; j <= 3; j++) {
oParagraph = Api.CreateParagraph();
oParagraph.SetStyle(oNewDocumentStyle2);
oParagraph.AddText("第 " + j + " 节 子标题");
oDocument.InsertContent([oParagraph]);
oParagraph = Api.CreateParagraph();
oParagraph.AddText("这是第 " + j + " 节的内容。");
oParagraph.AddText("这是加粗的文本。").SetBold(true);
oParagraph.AddLineBreak();
oParagraph.AddText("这是斜体文本。").SetItalic(true);
oDocument.InsertContent([oParagraph]);
oParagraph = Api.CreateParagraph();
oImage = Api.CreateImage("https://www.onlyoffice.com/images/landing/document-builder/document.jpg", 1556 * 3600, 1044 * 3600);
oParagraph.AddDrawing(oImage);
oDocument.InsertContent([oParagraph]);
oTable = Api.CreateTable(3, 3);
oTable.SetTableBorderTop("single", 32, 0, 51, 51, 51);
oTable.SetTableBorderBottom("single", 32, 0, 51, 51, 51);
oTable.SetTableBorderLeft("single", 32, 0, 51, 51, 51);
oTable.SetTableBorderRight("single", 32, 0, 51, 51, 51);
oTable.SetTableBorderInsideV("single", 32, 0, 255, 111, 61);
oTable.SetTableBorderInsideH("single", 32, 0, 255, 111, 61);
oTable.SetWidth("percent", 100);
for (var row = 0; row < 3; row++) {
oTableRow = oTable.GetRow(row);
for (var cell = 0; cell < 3; cell++) {
oCell = oTableRow.GetCell(cell);
oParagraph = oCell.GetContent().GetElement(0);
oParagraph.AddText("单元格 " + (row + 1) + "-" + (cell + 1));
// oCell.AddElement(oParagraph);
}
}
oDocument.InsertContent([oTable]);
if (j < 3) {
oParagraph = Api.CreateParagraph();
oParagraph.AddPageBreak();
oDocument.InsertContent([oParagraph]);
}
}
}
var oSection = oDocument.GetFinalSection();
var oFooter = oSection.GetFooter("default", true);
oParagraph = oFooter.GetElement(0);
oParagraph.AddPageNumber();
oParagraph.SetJc("center");
oDocument.UpdateAllTOC(false);
builder.SaveFile("docx", "TableOfContent.docx");
builder.CloseFile();
The screenshot of generated document is as follows:
I am looking forward to your reply, thanks!