Free Support Forum - aspose.com

How to apply font style to table of contents

Hi,

My document is created with formate Arial but when I press the F9 key, the table of contents will apear with font TimesNewRoman.Can you please suggest me how to change the font of the table of content.

Here is the code of the table of content.

DocumentBuilder builder = new DocumentBuilder(newdoc1);

Aspose.Words.Style style1=builder.Document.Styles.Add(Aspose.Words.StyleType.Paragraph,"Table of content");

style1.Font.StyleName="Arial";// is it correct

builder.MoveToSection(1);

builder.ParagraphFormat.StyleName="Table of content";

builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");

testdoc =newdoc1;

testdoc.Range.UpdateFields();

testdoc.Save("AutoGenerated.doc", SaveFormat.Doc, SaveType.OpenInWord, this.Response)

Thanks,

Sailaja.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. In fact every node of TOC is formatted as “Hyperlink”. So you should just change this style. For example see the following code snippet.

builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");

builder.Writeln();

doc.Styles[StyleIdentifier.Hyperlink].Font.Name = "Arial";

I hope this could help you.

Best regards.

Hi,

In Microsoft word We can set the formate ans style of the TOC.Is there any possibility to set style from our program.

Thanks,Sailaja.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Have you tried modifying “Hyperlink” style? You can set paragraph format, font style, etc.

doc.Styles[StyleIdentifier.Hyperlink].Font.Name = "Arial";

doc.Styles[StyleIdentifier.Hyperlink].Font.Size = 14;

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

doc.Styles[StyleIdentifier.Hyperlink].ParagraphFormat.Alignment = ParagraphAlignment.Center;

//etc

Please let me know if you would like to know anything else. I will be happy to help you.

Best regards.

Hi,

Thanks for your responsePlease seee the attachment jpg file for reference..I would like to know how set these formate settings from our program.

Thanks,

Sailaja.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Yes you can edit TOC1, TOC2 etc styles. For example see the following code.

//Insert TOC, You can set format in the field code

builder.InsertTableOfContents("\\o \"1-3\" \\p \" \" \\h \\z \\u");

builder.Writeln();

//Edit TOC styles.

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

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

doc.Styles[StyleIdentifier.Toc3].Font.Color = Color.Yellow;

Hope this helps.

Best regards.

Hi

Thanks for response.I am getting images in table of contents while pressing F9 in newly created document.Please provide solution for this. Here I am attaching my document for reference.

Thanks,

Sailaja.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

This occurs because your image has Heading 2 style. You should clear formation of image.

If you use document builder for generating document then you should call builder.ParagraphFormat.ClearFormatting(); before inserting image.

Best regards.

Hi ,

Thanks for your response.Is there any option to prevent the images with heading 1,2,3 etc are not appearing in the table of contents.I mean through coding can we find out the images having heading 1,2,3 etc,If it is possible then we can remove image from TOC.Please suggest me is it possible are not.

Thanks,

Sailaja.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

I think you can try using the following code.

Document doc = new Document(@"Test066\in.doc");

//Get shapes collection

NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);

foreach (Shape shape in shapes)

{

//Get parent paragraph od shape

Paragraph parent = shape.ParentParagraph;

if (parent.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1 ||

parent.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading2 ||

parent.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading3 ||

parent.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading4)

{

parent.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;

}

}

//Save document

doc.Save(@"Test066\out.doc");

I hope this could help you.

Best regards.