TOC with tab stops in .Net

Hi,
I’m looking for a solution for adding a table of content with tab stops between description and page number of a document. I’m currently using the switches “\o “1-4” \h \z \u” in my code but this doesn’t provide tab stops in TOC.
I want something like below attached image style in TOC

Any help related to the solution would be helpful.

Thanks in advance.

@Amruthajo can you please attach your input document and the complete code that you are using?

Hi @Eduardo_Canal,

Please find below code repo for tyour reference.
https://github.com/Amruthajo/TocAspose.git

I have used the trail version (21.4.0) in this POC. I’m able to get TOC with builder.Writeln() but the same is not generated with builder.InsertHtml().
Unfortunately it is necessary to use insert HTML in the project I’m working on.
Also I have observed if we apply any custom styles to TOC (eg: bold, line width) the tabs stops are disappearing from the document.

Thanks & Regards,
Amrutha

@Amruthajo thanks for the information, we are going to review it.

@Amruthajo I’m not able to reproduce the issue that you mention. I’m using the Package with version 23.1.0 (so, please try updating the version) and the following code:

// Initialize document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Writeln("TABLE OF CONTENTS");

// Insert a table of contents at the beginning of the document.
builder.InsertTableOfContents("\\o \"1-2\" \\h \\z \\u");

// Build a document with complex structure by applying different heading styles thus creating TOC entries.
for (int i = 1; i <= 100; i++)
{
    // Inserting data which is a HTML
    if(i%2 == 0)
    {
        builder.InsertHtml($"<h2>HEADER 2 COUNT {i}</h2>");
    }
    else if(i%3 == 0) 
    {
        builder.InsertHtml($"<h3>HEADER 3 COUNT {i}</h3>");
    }
    else
    {
        builder.InsertBreak(BreakType.PageBreak);
        builder.InsertHtml($"<h1>HEADER 1 COUNT {i}</h1>");
    }
                
    builder.InsertHtml("<p>LOREM IPSUM DOLOR SIT AMET</p>");
}

doc.UpdateFields();
doc.UpdatePageLayout();
doc.Save("C:\\Temp\\output.pdf");

output.pdf (71.7 KB)

Thank you for the response @eduardo.canal. We are able to generate TOC with tab stops and all with the sample code provided by you, but unfortunately this fix is not working for our code since we have some custom styles for TOC headings.
eg: For heading 1 & 2 we need to display the TOC content as bold others we don’t need this style.
Is there anyway we can appluy custom styles to TOC?!
Happy to create another thread if needed.

@Amruthajo can you please provide an example of the desired output?

@Amruthajo to define custom styles for TOC items, you can do it in 2 ways:

  1. Using the CustomStyles property of the FieldToc (learn more here)
  2. Using the available field codes, in example:
    { TOC \h \z \t “Style1,1,Style2,2” } Being “Style 1” and “Style 2” custom styles. (learn more here)

Thanks for the quick response @eduardo.canal. Attaching the sample TOC.

@Amruthajo TOC items formatting in MS Word is defined in the built-in TOC1…TOCN styles. So you need simply change formatting of the certain TOC style. For example:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Modify TOC styles.
doc.Styles[StyleIdentifier.Toc1].Font.Bold = true;

doc.Styles[StyleIdentifier.Toc2].Font.Color = Color.Red;

doc.Styles[StyleIdentifier.Toc3].Font.Color = Color.Green;
doc.Styles[StyleIdentifier.Toc3].Font.Italic = true;

// Put some dummy TOC
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
builder.Writeln();
builder.InsertBreak(BreakType.PageBreak);
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Heading 1 will be shown as bold in the TOC");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("Heading 2 will be red in the TOC");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3;
builder.Write("Heading 3 will be green and italic in the TOC");

doc.UpdateFields();
doc.UpdatePageLayout();

doc.Save(@"C:\Temp\out.docx");