Free Support Forum - aspose.com

Header styles for table of contents

Hi



I’ve been trawling through the forums for an answer to this, but there
doesn’t seem to be one. I can’t figure out how to set styles on my
section headers so that a table of contents will use them. I don’t want
to use the styles built into Word, such as Heading 1 or Heading 2,
because they don’t fit the formatting of the document, and I can’t see
a way to programatically change these default styles, because all the
properties of the Style class are read only. If I have to define a new
style, I assume I use the Styles class Add method, but how do I then
set all the properties of that style, and how do I make the table of
contents look at those styles?



Thanks

Andrew

Hi,

Thank you for considering Aspose.

You are right, adding styles is not very obvious, but we wanted to make it similar to MS Word. We will probably improve it in the future.

Here's the pattern. It defines a new style based on Heading 1 whose font is italic.

DocumentBuilder builder = new DocumentBuilder();

Style style = builder.Document.Styles.Add(StyleType.Paragraph, "Custom Heading 1");

style.BaseStyle = "Heading 1";

style.Font.Italic = true;

builder.ParagraphFormat.StyleName = "Custom Heading 1";

builder.Writeln("My Heading");

Thanks Dmitry. That worked
really well, but now I can’t figure out how to set styles on the
table of contents itself. For example, I use this string in the
InsertTableOfContents method: \h \z \t “Custom Heading 1,1,Custom
Heading 2,2”, which is what I get when I create a table manually in
word and then show the formatting using Alt-F9. But this string doesn’t
take into account indentation for sub headings, or font styles, or
spacing between elements of the TOC. How do I include that stuff in the
string for InsertTableOfContents?



Thanks

Andrew

I tried to use the regular TOC \o “1-3” \h \z \u string and the indentations have been set properly. If you need to set styles on the table of contents itself, you should modify the “TOC x” styles where x is the appropriate level of TOC.