Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Jul 25, 2019 9:41 am

I am using custom listformat for my document so i wish the TOC part will have simliar format
following is how i add paragraph title to the TOC
Code: Select all
//set the paragraph to be able to add into TOC
paragraph.ApplyStyle(BuiltinStyle.Heading1);
//remove the styling
paragraph.GetStyle().CharacterFormat.Bold = false;
paragraph.GetStyle().CharacterFormat.Italic = false;

//apply my custom Listformat
paragraph.ListFormat.ApplyStyle("levelstyle");


I try to let TOC append the same liststyle format but will encounter an exception. Any suggestion to let TOC follow the same format?

I can open the result document then update the TOC which will automatically change the format to match the content.

dczephy
 
Posts: 6
Joined: Thu Jul 11, 2019 8:46 am
Location: Taiwan

Thu Jul 25, 2019 10:30 am

Hello,

Thanks for your inquiry.
Kindly note that the custom style can't use for TOC paragraph, you need to create a builtin style and apply it for TOC paragraph like the following sample code. If there is any question, just feel free to write back.
Code: Select all
Document doc = new Document();
Section section = doc.AddSection();
//create a style for the TOC
ParagraphStyle pToc1Style = Style.CreateBuiltinStyle(BuiltinStyle.Heading1, doc) as ParagraphStyle;
pToc1Style.Name = "tocstyle";
pToc1Style.CharacterFormat.Bold = false;
pToc1Style.CharacterFormat.Italic = false;
doc.Styles.Add(pToc1Style);
//create paragrahs for adding TOC
Paragraph para = section.AddParagraph();
TableOfContent toc = para.AppendTOC(1, 2);
Paragraph para1 = section.AddParagraph();
para1.AppendText("Test paragraph1");
para1.ApplyStyle(BuiltinStyle.Heading1);
Paragraph para2 = section.AddParagraph();
para2.AppendText("Test paragraph2");
para2.ApplyStyle(BuiltinStyle.Heading1);
doc.UpdateTableOfContents();
//apply the created TOC style
section.Paragraphs[0].ApplyStyle("tocstyle");
section.Paragraphs[1].ApplyStyle("tocstyle");
doc.SaveToFile("result.docx", Spire.Doc.FileFormat.Docx);

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Fri Jul 26, 2019 2:55 am

so if I wish to let the TOC part has the following format how to do it?

(1) titlea........................................................................pagenumber
[space ] a. subtitle............................................................pagenumber

or i just ask the client to open the doc then update the TOC?

dczephy
 
Posts: 6
Joined: Thu Jul 11, 2019 8:46 am
Location: Taiwan

Fri Jul 26, 2019 3:36 am

Hello,

Thanks for your feedback.
To achieve your requirement, you need to create the different builtin styles for TOC paragraphs like the following sample code. I also attached my output word document for your reference. If there is any question, just feel free to write back.
Code: Select all
 Document doc = new Document();
 Section section = doc.AddSection();
 ListStyle listStyle = new ListStyle(doc, ListType.Numbered);
 listStyle.Name = "levelstyle";
 //level 1
 listStyle.Levels[0].PatternType = ListPatternType.Arabic;
 listStyle.Levels[0].NumberPrefix = "(";
 listStyle.Levels[0].NumberSufix = ")";
 listStyle.Levels[0].ParagraphFormat.SetLeftIndent(0);
 listStyle.Levels[0].ParagraphFormat.SetFirstLineIndent(0);
 //level 2
 listStyle.Levels[1].PatternType = ListPatternType.LowLetter;
 listStyle.Levels[1].ParagraphFormat.ClearFormatting();
 listStyle.Levels[1].NumberPosition = 20;
 doc.ListStyles.Add(listStyle);
 //create TOC paragraph
 Paragraph para = section.AddParagraph();
 TableOfContent toc = para.AppendTOC(1, 2);
 //para1
 Paragraph paragraph = section.AddParagraph();
 TextRange tr = paragraph.AppendText("title");
 paragraph.ApplyStyle(BuiltinStyle.Heading1);
 paragraph.ListFormat.ListLevelNumber = 0;
 paragraph.ListFormat.ApplyStyle("levelstyle");
 //para2
 paragraph = section.AddParagraph();
 tr = paragraph.AppendText("subtitle");
 paragraph.ApplyStyle(BuiltinStyle.Heading2);
 paragraph.ListFormat.ListLevelNumber = 1;
 paragraph.ListFormat.ApplyStyle("levelstyle");
 //create first style for TOC
 ParagraphStyle TocStyle1 = Style.CreateBuiltinStyle(BuiltinStyle.Heading1, doc) as ParagraphStyle;
 TocStyle1.Name = "TocStyle1";
 TocStyle1.CharacterFormat.Bold = false;
 TocStyle1.CharacterFormat.Italic = false;
 TocStyle1.ParagraphFormat.SetLeftIndent(0);
 TocStyle1.ParagraphFormat.SetFirstLineIndent(0);
 doc.Styles.Add(TocStyle1);
 //create second style for TOC
 ParagraphStyle TocStyle2 = Style.CreateBuiltinStyle(BuiltinStyle.Heading2, doc) as ParagraphStyle;
 TocStyle2.Name = "TocStyle2";
 TocStyle2.CharacterFormat.Bold = false;
 TocStyle2.CharacterFormat.Italic = false;
 TocStyle2.ParagraphFormat.ClearFormatting();
 TocStyle2.ParagraphFormat.SetLeftIndent(20);
 doc.Styles.Add(TocStyle2);
 //update TOC
 doc.UpdateTableOfContents();
 //apply the TOC styles
 section.Paragraphs[0].ApplyStyle("TocStyle1");
 section.Paragraphs[1].ApplyStyle("TocStyle2");
 doc.SaveToFile(@"result.docx", Spire.Doc.FileFormat.Docx);

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Tue Jul 30, 2019 3:46 am

Hello,

Greetings from E-icbelue.
Did previous code resolve your problem? Any feedback will be greatly appreciated.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.Doc