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.

Tue Dec 30, 2014 6:46 pm

Dear support,

I need to have two separate TOC (table of contents) at the start of my document.
There is a total of six heading styles, 3 builtin and 3 custom.
1 Heading1 (builtin)
1.1 Heading2
1.1.1 Heading3
This works as expected with
//Paragraph para = section.AddParagraph();
//para.AppendTOC(1, 3);
//document.UpdateTableOfContents();
and
A HeadingA (custom)
A.1 HeadingB (custom)
A.1.1 HeadingC (custom)

AppendTOC(1, 3) accepts two ints (lowerLevel and upperLevel)
In full word I can set a custom TOC and refer to the styles and levels by name.

Is there any way to do like a custom TOC field and set the styles to be used?
Or maybe modify the builtin styles and use 1,2,3 for one, and 4,5,6 for the other.

Thank you,

Bart

bart
 
Posts: 4
Joined: Mon Dec 22, 2014 9:00 pm

Wed Dec 31, 2014 8:26 am

Dear bart,

Thanks for your inquiry.

For you requirement, please refer to the code below:
Code: Select all
//Create custom paragraph style
ParagraphStyle ps = new ParagraphStyle(doc);
string style = "MyStyle";
ps.Name = style;
ps.ApplyBaseStyle(BuiltinStyle.Heading1);
ps.CharacterFormat.FontName = "Consolas";
doc.Styles.Add(ps);

//TOC 2 using custom style
p = section.AddParagraph();
p.AppendTOC(1, 3);

p = section.AddParagraph();
p.AppendText("A HeadingA");
p.ApplyStyle(style);

p = section.AddParagraph();
p.AppendText("A.1 HeadingB");
p.ApplyStyle(style);

p = section.AddParagraph();
p.AppendText("A.1.1 HeadingC");
p.ApplyStyle(style);


doc.UpdateTableOfContents();
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Wed Dec 31, 2014 10:16 am

A glimmer of hope.
You answered how to modify an existing builtin style, awesome.

However, your answer somewhat misses the mark.
I need two distinct and separate tocs.
The items on the second toc cannot appear also on the the first.

I was thinking maybe another approach is to add a field.
With switches.
That way I can use the style names.

I can add {TOC "HeadingA, 1, HeadingB, 2"}
How to you add some \h \z \t?

Bart

bart
 
Posts: 4
Joined: Mon Dec 22, 2014 9:00 pm

Thu Jan 01, 2015 7:01 am

Hello,

Thanks for your response. We will do some investigation and let you know immediately.

Thanks,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Mon Jan 05, 2015 6:30 am

Dear bart,

After some investigation, we found out that you can create TOC via TableOfContent constructor:
Code: Select all
TableOfContent(IDocument doc, string switches)

But there is a problem with the solution, and our dev team are working to fix it, We will inform if there is any update about this issue.
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Wed Jan 28, 2015 7:40 am

Hello bart,

Sorry to keep you waiting.

We just released a new version of Spire.Doc (Spire.Doc v5.3.38) to fix this problem, please download it via link below and have a try:
http://www.e-iceblue.com/Download/downl ... t-now.html

Sample code:
Code: Select all
Document doc = new Document();
doc.LoadFromFile("Sample.docx", FileFormat.Docx2010);
TableOfContent toc = new TableOfContent(doc, "{\\o \"1-3\" \\h \\z \\u}");
Paragraph p = doc.LastSection.Paragraphs[0];
p.Items.Add(toc);
p.AppendFieldMark(FieldMarkType.FieldSeparator);
p.AppendText("TOC");
p.AppendFieldMark(FieldMarkType.FieldEnd);
doc.TOC = toc;
doc.UpdateTableOfContents();
doc.SaveToFile("Result.docx", FileFormat.Docx);
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Return to Spire.Doc