Title for table of contents help

Hi
I am succesfully building a table of contents using the documentbuilder, however, I can’t figure out how to add a title to the table of contents, my input is in html and it has a title.
HOw can I add a titile to the table of contents?
thanks
this is my code to add the Table of Contents,
thanks

MemoryStream mySt = new MemoryStream();
Document doc = new Document(docStream);
DocumentBuilder builder = new DocumentBuilder(doc);

builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
builder.InsertBreak(BreakType.PageBreak);
doc.UpdateFields();
doc.UpdatePageLayout();

Hi Herb,

Thanks for your inquiry. In case you want the title to appear before TOC, you could simply use the Writeln method of DocumentBuilder class as can be seen in the following code snippet:

MemoryStream mySt = new MemoryStream();
Document doc = new Document(docStream);
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Writeln("Title for Table of Contents");

builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
builder.InsertBreak(BreakType.PageBreak);
doc.UpdateFields();
doc.UpdatePageLayout();

I hope, this will help.

Best Regards,

Thanks Awais,
I have another problem that haven’t been able to solve, when I open the document (saved as epub), the table of contents does not have a title in the side pane, it appears as “untitled”. My input docuemtn is in html format and it does have a title, I’m not sure where can I assign a value to the table of contents appearing in the left pane.
I would also like to place the title right before chapter 1. According to my research so far, one has to place a bookmark in the exact place in the document and then using the documentbuilder I have to navigate the cursor to that place and then render the table of contents. I was wondering if this is the simplest way to do so.
Thank you

Hi there,

Thanks for your inquires.

The “untitled” entry you are seeing in the navigation pane is the title of the document. You can either choose to include a metadata tag in your input html or set the document title manually by changing the BuiltInDocumentProperties.Title property.

Regarding your second query, entries in the navigation pane are populated using paragraphs styled with heading styles in the document. The easiest way to have an entry appear in there would be to style it with a heading. Please see the code below.

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("TOC Title");

Also note that you can control how many levels are exported by using the HtmlSaveOptions class. Please see here for details: https://reference.aspose.com/words/net/aspose.words.saving/htmlsaveoptions/navigationmaplevel/

If we can help with anything else, please feel free to ask.

Thanks,

Thanks,
What I meant is that I would like to place the table of contents in a specific place in the document. I want to place it right before a heading 1 with specific attribute-value combinations, the way I am trying to find the fid that first node is by using the following code:
This code is not printing anything, please help, thank you

Node node = doc.SelectSingleNode("/Document/Section/Body/Paragraph");
if (node != null)
    Console.WriteLine(((Run) node).Text);

But I

Hi Herb,

Thanks for your request.

The point here is, ‘Title of TOC’ is not actually the part of ‘Table of Contents’ itself. You have to designate a place somewhere in your Word document to be able to insert heading for TOC. For this I would suggest you to simply insert a Bookmark at a place where you would like to

insert a heading in Word document. Then by usingMoveToBookmark method of DocumentBuilder*
class move the cursor to that location and finally insert some paragraph (rich
content) there. Please see the following code snippet for clarification:

Document doc = new Document(@"c:\test\TitleTOC.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToBookmark("myBookMark");
builder.Write("Your TOC title goes here");
Bookmark bookmark = doc.Range.Bookmarks["myBookMark"];
// You could optionally delete this bookmark. The bookmarked text is not deleted.
bookmark.Remove();
doc.Save(@"c:\test\TitleTOC_out.docx");

I hope, this will help.

Best Regards,

Understood,
But instead of a bookmark, I would like to use the existing tags. What I would like to do is find a specific node, and navigate to it. The follwoing code is not working for me:
It is my understanding that the selectsinglenode method gets the first method for which the xpath expression is true, thank you

Node node = doc.SelectSingleNode("/Document/Section/Body/Paragraph");
if (node != null)
    Console.WriteLine(((Run) node).Text);

Hi,

Thanks for your request. You can reach the first paragraph of your document by using the following code snippet:

Node node = doc.SelectSingleNode(@"//Body/Paragraph");
if (node != null)
{
    Paragraph paragraph = (Paragraph) node;
    Console.WriteLine(((Paragraph) node).ToTxt());
}

Moreover, please see the following link for a description of CompositeNode.SelectSingleNode method:
https://reference.aspose.com/words/net/aspose.words/compositenode/selectsinglenode/

Aspose.Words represents a document as a tree of nodes; to explore and navigate the document tree in Aspose.Words, please see the following link:
https://docs.aspose.com/words/net/aspose-words-document-object-model/

Also, I would suggest you the following methods to be able to navigate through the DOM (Document Object Modal) of Aspose.Words and modify some specific node:

CompositeNode.GetChildNodes Method
DocumentBuilder.MoveToParagraph Method

I hope, this will help.

Best Regards,

Thanks again,
It works , however, can’t I identify a specific element? Since I don’t know where chapter 1 will be located exactly. I do know that a tag with a specific style format is Chapter 1. Thus I’ve been trying to run the following code and it does not return anything, am I doing something wrong or Aspose does not support attributes?
this is the html tag I would like to identify
<h1 style="font-size:large">test text</h1>

Node node = doc.SelectSingleNode(@"//Body/Paragraph/h1[@style='font-size:large'] ");
if (node != null)
    Console.WriteLine(node.ToTxt());

Hi
Thanks for your request. No, you cannot access html attributes of the source file from Aspose.Words DOM. In your case your title is converted to a paragraph with Heading 1 style. You can use this to identify it:
https://docs.aspose.com/words/net/how-to-extract-selected-content-between-nodes-in-a-document/
Best regards,