Different Styles for Table of Contents

Hi Team,

There are different styles used for Table of Contents in word that we can choose from. Can we get use of these different styles through program ?
Please find below the screenshot - the option I am looking for
image.png (835 Bytes)

Thanks,
Prasanna Shanmuganathan

@pshanmuganathan

Thanks for your inquiry. Yes, Aspose.Words mimics the same behavior as MS Word does.

You can achieve desired results by using following code:

Document doc = new Document("D:\\Temp\\TOC.docx");
doc.Styles[StyleIdentifier.Toc1].BaseStyleName = "Heading 8";
doc.Styles[StyleIdentifier.Toc1].ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading8;
doc.Styles[StyleIdentifier.Toc1].Font.Color = Color.Red;

doc.UpdateFields();
doc.Save("D:\\Temp\\TOC_18.11.docx");

Hope, this helps.

Hi Mannan,

Thanks for the reply. Can you please give me the Java version of the code ?

Thanks,
Prasanna Shanmuganathan

@pshanmuganathan

Thank you for patience. Please use the following code to get desired results in Java. Hope, this helps.

Document doc = new Document("D:\\Temp\\TOC.docx");

doc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_1).setBaseStyleName("Heading 1");
doc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_1).getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);
doc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_1).getFont().setColor(Color.RED);

doc.updateFields();
doc.save("D:\\Temp\\TOC_18.11.docx");