We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Separate formatting of 2 TOCs in a document

Dear Sir or Madam,

We have a document in which we insert 2 TOC. Currently to change formatting of TOCs we retrieve TOC1 and TOC2 style (as the first TOC has 2 levels) and apply formatting properties to both these style. We have a request to have different formatting for the first and the second TOC (different tab leads, different spacing). Our approach doesn’t work as any change in style applies to both TOC. How can we achieve different TOCs formatting?
The code looks like this:

List<Style> tocs = new List<Style> { mOutputDocument.Styles[StyleIdentifier.Toc1], mOutputDocument.Styles[StyleIdentifier.Toc2]};
foreach (Style tocStyle in tocs)
{
    tocStyle.ParagraphFormat.TabStops.Clear();
    TabStop tabStop = new TabStop(ConvertUtil.MillimeterToPoint(206 - mRoService.ReportingOptions.LeftMarginIdent - mRoService.ReportingOptions.RightMarginIdent), TabAlignment.Right, tabLeader);
    tocStyle.ParagraphFormat.TabStops.Add(tabStop);
    tocStyle.ParagraphFormat.LineSpacing = spacing;
    // Sets the default space between TOC lines.
    tocStyle.ParagraphFormat.SpaceAfter = 0;
    // Right indent in paragraphs of TOC. Otherwise long lines will go to page numbers area.
    tocStyle.ParagraphFormat.RightIndent = ConvertUtil.MillimeterToPoint(10);

A sample document is attached, first TOC is in p 6, second on p 11._Alex test-Annual Report-_Alex test-AlexPara-Suffix (6).docx (334.1 KB)

Thank you in advance,
Alex

@AlexanderNovickov Unfortunately, there is no way to apply different styles to TOC items. You can apply explicit formatting to the paragraphs in the TOC after updating, but the formatting will be overridden once the TOC is updated again.

Hi Alexey,

Thank you for a prompt reply. If I have two TOCs in the document, how can I get all the paragraphs containing one of them to apply formatting?

Alex

@AlexanderNovickov You can use code like the following:

Document doc = new Document(@"C:\Temp\in.docx");

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

// Create styles for the second TOC
Style customToc1 = doc.Styles.Add(toc1.Type, "TOC1_custom");
customToc1.BaseStyleName = toc1.Name;
customToc1.Font.Color = Color.Green;
Style customToc2 = doc.Styles.Add(toc2.Type, "TOC2_custom");
customToc2.BaseStyleName = toc2.Name;
customToc2.Font.Color = Color.Green;
Style customToc3 = doc.Styles.Add(toc3.Type, "TOC3_custom");
customToc3.BaseStyleName = toc3.Name;
customToc3.Font.Color = Color.Green;

ArrayList tocs = new ArrayList();
foreach (Field f in doc.Range.Fields)
{
    if (f.Start.FieldType == FieldType.FieldTOC)
        tocs.Add(f);
}

// Edit the second TOC
Field secondToc = (Field)tocs[1];
// Find the first and last paragraph
Paragraph firstPara = secondToc.Start.ParentParagraph;
Paragraph lastPara = secondToc.End.ParentParagraph;

// Change the applied style
Paragraph currentPara = firstPara;
do
{
    switch (currentPara.ParagraphFormat.StyleIdentifier)
    {
        case StyleIdentifier.Toc1:
            currentPara.ParagraphFormat.Style = customToc1;
            break;
        case StyleIdentifier.Toc2:
            currentPara.ParagraphFormat.Style = customToc2;
            break;
        case StyleIdentifier.Toc3:
            currentPara.ParagraphFormat.Style = customToc2;
            break;
    }

    currentPara = (Paragraph)currentPara.NextSibling;
} while (currentPara != lastPara);

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

Hi Alexey,

Thanks a lot, works like a charm!

Alex

1 Like