Now I need to apply styles to my TOC. I’ve made a function based on suggestions of previous threads:
private void SetTocStylingForWord(Document doc)
{
Aspose.Words.Style toc1 = doc.Styles[StyleIdentifier.Toc1];
Aspose.Words.Style toc2 = doc.Styles[StyleIdentifier.Toc2];
Aspose.Words.Style toc3 = doc.Styles[StyleIdentifier.Toc3];
Aspose.Words.Style toc4 = doc.Styles[StyleIdentifier.Toc4];
Aspose.Words.Style[] tocStyles = {
toc1,
toc2,
toc3,
toc4
};
foreach(Aspose.Words.Style style in tocStyles)
{
style.Font.Name = "Times New Roman";
style.Font.Size = 12;
style.Font.Bold = false;
}
toc3.Font.Size = 10;
}
I’ve inserted this function in several locations without avail. I have placed it in each TOC section and even right before I call the update function. The stylings I have created are NOT applied. HOWEVER, if I manually do an “Update entire table” in the produced Word document, my stylings take affect. Go figure.
Any suggestions, please?
Thanks.
// Passing no document to the DocumentBuilder will cause a blank document to be created.
DocumentBuilder builder1 = new DocumentBuilder();
// Get the document used by the DocumentBuilder.
Document doc1 = builder1.Document;
// TOC Section Index and List of these indices
int index = -1;
List tocSectionIndices = new List();
// Insert a table of contents at the beginning of the document.
builder1.InsertTableOfContents("\\o \"1-2\" \\t \"Heading4,1\" \\x");
index = doc.Sections.IndexOf(builder.CurrentSection);
tocSectionIndices.Add(index);
SetTocStylingForWord(doc);
builder1.InsertBreak(BreakType.SectionBreakNewPage);
builder1.InsertTableOfContents("\\t \"Heading4,1\" \\x");
index = doc.Sections.IndexOf(builder.CurrentSection);
tocSectionIndices.Add(index);
SetTocStylingForWord(doc);
builder1.InsertBreak(BreakType.SectionBreakNewPage);
builder1.InsertTableOfContents("\\t \"Heading3,1\"");
index = doc.Sections.IndexOf(builder.CurrentSection);
tocSectionIndices.Add(index);
SetTocStylingForWord(doc);
builder1.InsertBreak(BreakType.SectionBreakNewPage);
// Build a document with complex structure by applying different heading styles thus creating TOC entries.
builder1.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder1.Writeln("Heading 1");
builder1.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder1.Writeln("Heading 1.1");
builder1.Writeln("Heading 1.2");
builder1.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder1.Writeln("Heading 2");
builder1.Writeln("Heading 3");
builder1.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder1.Writeln("Heading 3.1");
builder1.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3;
builder1.Writeln("Heading 3.1.1");
builder1.Writeln("Heading 3.1.2");
builder1.Writeln("Heading 3.1.3");
builder1.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading4;
builder1.Writeln("Heading 3.1.3.1");
builder1.InsertBreak(BreakType.PageBreak);
builder1.Writeln("Heading 3.1.3.2");
builder1.Writeln("Heading 3.1.3.3");
builder1.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder1.Writeln("Heading 3.2");
builder1.Writeln("Heading 3.3");
// If one has to add content to the document before generating the TOC,
// then its best to pass the List of TOC section indices. I have to call
// UpdateFields () in another function.
foreach (int index in tocSectionIndices)
{
doc1.Sections[index].Range.UpdateFields();
}
SetTocStylingForWord(doc);
doc1.UpdatePageLayout();
doc1.Save("test.doc", SaveFormat.Doc);