Free Support Forum - aspose.com

Page Index

Hi I am using Aspose.word. I need to create a Index page which will have topic heading and total number pages under that topic, Please suggest me how I can do this ??

Hi,


Thanks for your inquiry. In your case, I suggest to use DocumentBuilder.InsertTableOfContents method to create TOC (table of contents). This method inserts a TOC (table of contents) field into the document at the current position. After creating a table of contents, the following field is inserted into the document: { TOC \o “1-3” \h \z \u }.

Please use the following code snippet to create TOC in document.

<span lang=“EN-GB” style=“font-size:10.0pt;font-family:“Courier New”;color:gray;mso-font-kerning:
0pt;mso-no-proof:yes”>///<span lang=“EN-GB” style=“font-size:10.0pt;
font-family:“Courier New”;color:green;mso-font-kerning:0pt;mso-no-proof:yes”>
Use a blank document<o:p></o:p>

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");


You can insert Fields in MS Word document by using DocumentBuilder.InsertField Method. I suggest to read following documentation links for your kind reference.

http://www.aspose.com/docs/display/wordsnet/DocumentBuilder+Overview
http://www.aspose.com/docs/display/wordsnet/DocumentBuilder+Class
http://www.aspose.com/docs/display/wordsnet/DocumentBuilder+Members

Please use following code snippet to add NUMPAGES field to show total number pages in document.


Document doc = new Document(MyDir + "in.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToDocumentEnd();

builder.Write(" Total Page ");

builder.InsertField("NUMPAGES", "");

doc.UpdateFields();

doc.Save(MyDir + "AsposeOut.docx");