Is it possible to reduce the width taken by table of contents?


Is it possible to reduce the width taken by table of contents. By default it takes 100% width of the document. Can I manage the width of table of contents so that I can show table of contents in 50% width of the document.In the rest 50% width, I have to show some more info.

Please find the attached for sample.


Hi Pradeep,

Thanks for your inquiry. You can achieve this by adjusting the tab stop positions of each Paragraph of TOC field items. Please try running the following code:

// Use a blank document
Document doc = new Document();
// Create a document builder to insert content with into document.
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a table of contents at the beginning of the document.
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
// Start the actual document content on the second page.
// Build a document with complex structure by applying different heading styles thus creating TOC entries.
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Heading 1");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("Heading 1.1");
builder.Writeln("Heading 1.2");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Heading 2");
builder.Writeln("Heading 3");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("Heading 3.1");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3;
builder.Writeln("Heading 3.1.1");
builder.Writeln("Heading 3.1.2");
builder.Writeln("Heading 3.1.3");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("Heading 3.2");
builder.Writeln("Heading 3.3");
// Call the method below to update the TOC.
foreach(Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
    para.ParagraphFormat.TabStops.Add(new TabStop(72 * 2.5));
doc.Save(MyDir + @"out.docx");

Best regards,