TOC content formatting

Hi Team,

I am inserting TOC using ASpose words.

I need some spacing between toc content line. Like SpaceAfter = 6,

But it is not reflected in TOC paragraph.

Please guide me how to achieve it.

Please find attached document and Style code below.

ProcessingDocumentBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;

ProcessingDocumentBuilder.Font.Name = FontCalibri;
ProcessingDocumentBuilder.Font.Size = 11;
paragraphFormat.StyleName = "Normal";

paragraphFormat.Style.Font.Name = FontCalibri;
paragraphFormat.Style.Font.Size = 11;
paragraphFormat.Style.Font.Italic = false;
paragraphFormat.Style.Font.Bold = false;
paragraphFormat.Style.Font.Color = Color.Black;

paragraphFormat.SpaceBefore = 0;
paragraphFormat.SpaceAfter = 6;

Thanks and Regards,
Nakul

Hi Nakul,

Please use the following code snippet to add space after the contents. Please also visit this documentation link for your reference.

Document doc = new Document(@"d:\out.doc");
doc.Styles[0].ParagraphFormat.SpaceAfter = 6;
doc.Save(@"d:\toc2.doc");

Hope this answers your query. Please let me know, If you have any further queries.

Hi Manzoor,

Thanks for reply,

This line applies SpaceAfter = 6 to all content of document.

I need to apply Spacing of size 6 to TOC content only.

I also tried to use the solution provided in link

Style toc1 = doc.Styles[StyleIdentifier.Toc1];
Style toc2 = doc.Styles[StyleIdentifier.Toc2];
Style toc3 = doc.Styles[StyleIdentifier.Toc3];
Style toc4 = doc.Styles[StyleIdentifier.Toc4];

Style[] tocStyles = { toc1, toc2, toc3, toc4 };

foreach (Style style in tocStyles)
{
    // Change font name and size.
    style.Font.Name = "Arial";
    style.Font.Size = 14;
}

But this is working only for first and last line of toc content.

Please let me know how to apply style to entire TOC only not to document

Please find attached output file.

Thanks and Regards,
Nakul

Hi Nakul,
Thanks for your inquiry. But is not clear for me why you are changing font in your code if you need to change spacing? I suppose, your code should look like this:

Document doc = new Document(@"Test001\FinalReport.docx");
Style toc1 = doc.Styles[StyleIdentifier.Toc1];
Style toc2 = doc.Styles[StyleIdentifier.Toc2];
Style toc3 = doc.Styles[StyleIdentifier.Toc3];
Style toc4 = doc.Styles[StyleIdentifier.Toc4];
Style toc5 = doc.Styles[StyleIdentifier.Toc5];
Style[] tocStyles = { toc1, toc2, toc3, toc4, toc5 };
foreach (Style style in tocStyles)
{
    // change spacing.
    style.ParagraphFormat.SpaceAfterAuto = false;
    style.ParagraphFormat.SpaceAfter = 6;
}
doc.Save(@"Test001\out.docx");

Best regards,

Hi Alexey,

In my code i am doing same as suggested by you,

I copied code from the example which I referred in my code.

I tried this example it gives me problem described in last post.

only first and last line of TOC is having Spacing = 6. In between lines have spacing = 0.

Please find attached document in last post.

Regards,
Nakul

Hi
Thanks for your request. This is because you are changing only first 4 levels of TOC. But in your case, TOC5 is also used. I would suggest you to change all 9 TOC styles.
Best regards,

It works,

Thanks a lot!!

Regards,
Nakul