Free Support Forum - aspose.com

Generate TOC using ASPOSE Word for Existing Documents

Hi,

We are using the evaluation version of Aspose Word right now and we need to prove one scenario in order to build a case to buy ASPOSE.

Scenario:

We should be able to generate TOC for an existing word document. Aspose should be able to scan the whole document and find all the headings and then generate TOC at the begining of the page!

We will add a "temp special character" to all the headings in the document so that Aspose can find all the headings in the document just by finding the special character!

Please help us ASAP so that we can proceed with the Licensed Version!

Thanks,

Hari

Hi Hari,


Thanks for your interest in Aspose.Words. Sure, you can achieve this by using the following code snippet:

// Load an existing Word document in memory

Document doc = new Document(@"C:\Temp\out.docx");

// 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.MoveToDocumentStart();

builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");

// Start the actual document content on the second page.

builder.InsertBreak(BreakType.PageBreak);

// Call the method below to update the TOC.

doc.UpdateFields();

// Resave the document to disk

doc.Save(@"C:\Temp\in.docx");


I hope, this helps.

Best regards,

Hi,

Thank you very much for your resposne! This approach works for us for the document which has the styles applied for all the headings.

However, we just noticed that we also have some documents that do not have the styles applied for the headings. we have an existing DOTX file which has the custom styles defined for all the headings in the document.

Our document will have a "Special Character" appended to all the text for which we need to apply the style as heading (read the style from the DOTX file)

Here is what Aspose should do:

Scan the document and find the "special character".
Where ever the special character is found, apply the custom style by reading from the DOTX file.
Then finally run the TOC. ( we know this step works)


I have also atatched a sample document with meets the above criteria! Can you please generate a TOC for the aatched document? You need to search for the special character "~" and apply the style finally generate the TOC.

I am sure this will be our final step to proceed with ASPOSE!

Thanks,

Hari

Hi,

Just to add to my previous reply:

Even if you have any other approach to generate TOC for the attached template other than "Find the special character and apply style" we are open to that ! Please let us know.

Thanks,

Hari

Hi Hari,


Thanks for the additional information. Here is the simple code to achieve this:

Document
doc = new Document(@“C:\Temp\xdo5746220056746859012.rtf”);

DocumentBuilder builder = new DocumentBuilder(doc);

foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))

{

if (para.ToString(SaveFormat.Text).Contains("~"))

para.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;

}

builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");

builder.InsertBreak(BreakType.PageBreak);

doc.UpdateFields();

doc.Save(@"C:\temp\out.rtf");


I hope, this helps.

Best regards,

Thank you!

Just wanted to make sure that we can also import custom styles using .DOTX file. Becasue we don't wanted to use the defauly style of "Heading 1".

Thanks,

Hari

Hi Hari,


Thanks for your inquiry. Yes, you can import user-defined styles from your template document into your main document by using the following code snippet:

Document
doc = new Document(@“C:\Temp\main.docx”);

string templatePath = doc.AttachedTemplate;

Document templateDoc = new Document(templatePath);

foreach (Style srcStyle in templateDoc.Styles)

if (!srcStyle.BuiltIn)

doc.Styles.AddCopy(srcStyle);


Best regards,

Great! Thank you very much for your assistance! I have passed all the above information to the team and we are now looking towards getting a commercial license.

Thanks,

Hari

Hi Hari,


Please let us know any time you have any further queries. We are always glad to help you.

Best regards,