Hello,
How to set table of content (TOC) text font as bold & italic.
regards,
Rakesh
Document doc = new Document();
Section section = doc.AddSection();
//create a paragrah for adding TOC
Paragraph para = section.AddParagraph();
TableOfContent toc = para.AppendTOC(1, 2);
Paragraph para1 = section.AddParagraph();
para1.AppendText("Heading1");
para1.ApplyStyle(BuiltinStyle.Heading1);
Paragraph para2 = section.AddParagraph();
para2.AppendText("Heading2");
para2.ApplyStyle(BuiltinStyle.Heading1);
doc.UpdateTableOfContents();
//create a style
ParagraphStyle style = new ParagraphStyle(doc);
style.Name = "MyStyle";
style.CharacterFormat.Bold = true;
style.CharacterFormat.Italic = true;
doc.Styles.Add(style);
//apply the style for heading1
section.Paragraphs[0].ApplyStyle("MyStyle");
//apply the style for heading2
section.Paragraphs[1].ApplyStyle("MyStyle");
doc.SaveToFile("CreatedTocFile.docx", Spire.Doc.FileFormat.Docx);
Document doc = new Document(@"TOCsample.docx");
foreach (Section section in doc.Sections)
{
foreach (DocumentObject obj in section.Body.ChildObjects)
{
FieldCollection field = doc.Fields;
foreach (Field fd in field)
{
if (obj.ChildObjects.Contains(fd))
{
Paragraph para = obj as Paragraph;
foreach (TextRange textRange in para.ChildObjects.OfType<TextRange>())
{
//set text font of TOC paragraph
textRange.CharacterFormat.Italic = true;
textRange.CharacterFormat.Bold = true;
}
}
}
}
}
doc.SaveToFile("res2.docx", Spire.Doc.FileFormat.Docx2013);