Hi,
I am building a phone book type of Word doc with Aspose.Words and need help constructing specialized indexes. For simplicity, lets just say the phone book will be an alphabetical listing of restaurants with their contact details, hours of operation, location, description, etc.
Then I want to have an index (preferably at the beginning of the doc) that lists alphabetically just the restaurants that are open 24 hours a day and the page number each one is on so you can look each one up for more details.
I want another index (again, preferably at the beginning of the doc) that lists alphabetically just the restaurants that serve vegetarian food, again with the page number for each next to it.
And so on…
How would I accomplish this with Aspose.Words? I presume it would start with adding an invisible marker (Bookmark?) of some kind as the restaurants are added to indicate if it is 24-hour and/or vegetarian. What would that “marker” be and how do I add it?
Then how would I access those “markers” after the restaurant listing has been built so that I can construct the indexes, including the actual page numbers for each restaurant in the index?
Thanks for your inquiry. In your case, you can use TOC (table of contents) for your requirement. Please use DocumentBuilder.InsertTableOfContents Method to create table of contents. This method inserts a TOC (table of contents) field into the document at the current position.
For example, after creating a table of contents, the following field is inserted into the document: { TOC \o "1-3" \h \z \u }. You can copy \o "1-3" \h \z \u and use it as the switches parameter.
Following code example demonstrates how to insert a Table of contents (TOC) into a document using heading styles as entries.
// Use a blank document
Document doc = new Document();
// Create a document builder to insert content with into document.
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a table of contents at the beginning of the document.
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
// Start the actual document content on the second page.
builder.InsertBreak(BreakType.PageBreak);
// Build a document with complex structure by applying different heading styles thus creating TOC entries.
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Heading 1");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("Heading 1.1");
builder.Writeln("Heading 1.2");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Heading 2");
builder.Writeln("Heading 3");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("Heading 3.1");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3;
builder.Writeln("Heading 3.1.1");
builder.Writeln("Heading 3.1.2");
builder.Writeln("Heading 3.1.3");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("Heading 3.2");
builder.Writeln("Heading 3.3");
// Call the method below to update the TOC.
doc.UpdateFields();
doc.Save(MyDir + "AsposeOut.docx");