Error when creating multiple tables of contents

As the title suggests, when a table of contents, table list, and figure list exist simultaneously in a document, the error shown in the image occurs.
How can I resolve this issue?

code

// 創建一個新的文檔
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// 設置頁面大小為A4
builder.PageSetup.PaperSize = PaperSize.A4;
builder.Font.Name = "DFKai-sb";


// 設置邊距,1公分等於28.35點
Section currentSection = builder.CurrentSection;
PageSetup pageSetup = currentSection.PageSetup;

// 設置上、下、左、右邊距
pageSetup.TopMargin = 2.54 * 28.35;   // 上邊距:2.54cm
pageSetup.BottomMargin = 2.54 * 28.35; // 下邊距:2.54cm
pageSetup.LeftMargin = 1.91 * 28.35;   // 左邊距:1.91cm
pageSetup.RightMargin = 1.91 * 28.35;  // 右邊距:1.91cm

// 第二頁:目錄
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Font.Size = 16;
builder.Writeln("目錄");
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u"); // 插入標題目錄,基於標題層級
// 插入頁面分隔符
builder.InsertBreak(BreakType.PageBreak);

// 第三頁:插入表目錄
builder.Writeln("表目錄");
builder.InsertField("TOC \\h \\z \\c \"Table\" "); // 插入基於表格標題的表目錄
// 插入頁面分隔符
builder.InsertBreak(BreakType.PageBreak);

// 第四頁:插入圖目錄
builder.Writeln("圖目錄");
builder.InsertField("TOC \\h \\z \\c \"Figure\" "); // 插入基於表格標題的表目錄
// 插入頁面分隔符
builder.InsertBreak(BreakType.PageBreak);


// 插入標題
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("第一章: 報告簡介");
builder.InsertBreak(BreakType.ParagraphBreak);

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("1.1 報告背景");
builder.ParagraphFormat.ClearFormatting(); // 清除大綱階層1的樣式
// 插入頁面分隔符
builder.InsertBreak(BreakType.PageBreak);

// 插入表格和標題
Table table = builder.StartTable();
builder.InsertCell();
builder.Write("表格內容");
builder.EndTable();
builder.Write("表");
builder.InsertField("SEQ Table \\* ARABIC");
builder.Write(" - 示例表格");
builder.InsertBreak(BreakType.ParagraphBreak);

builder.InsertBreak(BreakType.PageBreak);

// 插入圖表和標題
builder.Write("圖");
builder.InsertField("SEQ Figure \\* ARABIC", "1");
builder.Write(" - 示例圖表");
builder.InsertBreak(BreakType.ParagraphBreak);

Shape chartShape = builder.InsertChart(ChartType.Pie, 432, 252);
builder.InsertBreak(BreakType.ParagraphBreak);

builder.InsertBreak(BreakType.PageBreak);

// 更新所有域
doc.UpdateFields();

// 保存文檔
doc.Save("sample.docx");

@weiyaochen Please put each TOC in the separate paragraph to avoid the problem. Please see the modified code:

// 創建一個新的文檔
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// 設置頁面大小為A4
builder.PageSetup.PaperSize = PaperSize.A4;
builder.Font.Name = "DFKai-sb";

// 設置邊距,1公分等於28.35點
Section currentSection = builder.CurrentSection;
PageSetup pageSetup = currentSection.PageSetup;

// 設置上、下、左、右邊距
pageSetup.TopMargin = ConvertUtil.MillimeterToPoint(25.4);  // 上邊距:2.54cm
pageSetup.BottomMargin = ConvertUtil.MillimeterToPoint(25.4); // 下邊距:2.54cm
pageSetup.LeftMargin = ConvertUtil.MillimeterToPoint(19.1);   // 左邊距:1.91cm
pageSetup.RightMargin = ConvertUtil.MillimeterToPoint(19.1);   // 右邊距:1.91cm

builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln();
builder.InsertField("TOC \\h \\z \\c \"Table\" ");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln();
builder.InsertField("TOC \\h \\z \\c \"Figure\" ");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln();

// 插入標題
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("第一章: 報告簡介");
builder.InsertBreak(BreakType.ParagraphBreak);

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("1.1 報告背景");
builder.ParagraphFormat.ClearFormatting(); // 清除大綱階層1的樣式
                                            // 插入頁面分隔符
builder.InsertBreak(BreakType.PageBreak);

// 插入表格和標題
Table table = builder.StartTable();
builder.InsertCell();
builder.Write("表格內容");
builder.EndTable();
builder.Write("表");
builder.InsertField("SEQ Table \\* ARABIC");
builder.Write(" - 示例表格");
builder.InsertBreak(BreakType.ParagraphBreak);

builder.InsertBreak(BreakType.PageBreak);

// 插入圖表和標題
builder.Write("圖");
builder.InsertField("SEQ Figure \\* ARABIC", "1");
builder.Write(" - 示例圖表");
builder.InsertBreak(BreakType.ParagraphBreak);

Shape chartShape = builder.InsertChart(ChartType.Pie, 432, 252);
builder.InsertBreak(BreakType.ParagraphBreak);

builder.InsertBreak(BreakType.PageBreak);

// 更新所有域
doc.UpdateFields();
doc.Save(@"C:\Temp\out.docx");
1 Like

The solution actually works.
Thanks for the help! :100:

1 Like