We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Populating TOC from Hidden text

Hi,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

I use Aspose.Words 4.4.0.0 to generate Word doc. Generally I specify “Heading 1” style to get a particular Header text in Table of contents. Its working fine.

I have a requirement where in my Header text differs from text to be displayed in TOC. So, I created a Hidden text with “Heading 1” style but it is not getting displayed in TOC.

Ex.

Table of Contents

My Header Text

Data1.................................................................................................................................... 3

Data2.................................................................................................................................... 3

Data3.................................................................................................................................... 3

(Actual Header text)

Custom Header Name

If this will not work in Aspose. Can you give me work around for this Scenario.

Thanks in advance.

-- Nithiyanandam

Hi

Thanks for your inquiry. It seems that TOC field doesn’t support hidden text. As workaround you can use white text. For example see the following code.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Insrt a table of contents at the beginning of the document.

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

// Build a document with complex structure by applying different heading styles thus creating TOC entries.

builder.Writeln("Heading 1");

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;

builder.PushFont();

builder.Font.Color = Color.White;

builder.Writeln("Heading 1 in TOC");

builder.PopFont();

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;

builder.Writeln("Heading 2");

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;

builder.PushFont();

builder.Font.Color = Color.White;

builder.Writeln("Heading 2 in TOC");

builder.PopFont();

doc.Save(@"449_108412_kannithyan\out.doc");

Best regards.

Thanks for your response.

But if the user select the entire Document and changes the colour to Blue or Red, the hidden text will be visible to him. Do we have some other way of doing it.

Thanks,

Nithiyanandam

Hi

I think that there is no other way. But if you can do this using MS Word then you should send me this document for investigating.

Best regards.

Hi,

Please find the attached document in which another application created TOC using the hidden text. I need to simulate the same in my application. It would be better if i can also do the same. Can you investigate and let me know how they have done it and How can we achieve the functionality?

Thanks,

Nithiyanandam

Hi

Thank you for additional information. Sorry, I missed that you can build TOC using TC field. For example see the following code.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Insrt a table of contents at the beginning of the document.

builder.InsertTableOfContents("\\f \\h");

builder.InsertBreak(BreakType.PageBreak);

// Insert TC field that represents TOC element

builder.InsertField("TC \"Fierst elemrnt in TOC\"", "");

builder.Writeln("First element in document");

builder.InsertField("TC \"Second element in TOC\"", "");

builder.Writeln("Second element in document");

doc.Save(@"out.doc");

I believe that this will help you.

Best regards.

Hi

The given code populates the TOC.

1. A small vertical line appears in the place of Field code although the text is hidden. Please find the attached document for your reference.

2. Also I need to display the TOC in levels as below,

Tracker Panels - TRACKER (Item A1) (Level 1)

Tag Data..................................... 2 (Level 2)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Product Data................................... 2

My Code to achieve the above TOC is,

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Insrt a table of contents at the beginning of the document.

builder.InsertTableOfContents("\\f \\h");

builder.InsertBreak(BreakType.PageBreak);

// Insert TC field that represents TOC element

builder.InsertField("TC \"Tracker Panels(Item 1)\" \\n \\I 1", "");

builder.Writeln("Tracker Panels");

builder.InsertField("TC \"Tag Data\" \\I 2", "");

builder.Writeln("Tag Data");

builder.InsertField("TC \"Product Data\" \\I 2", "");

builder.Writeln("Product Data");

doc.Save(@"out.doc");

But I am not getting the TOC in levels. I am getting output as shown below.

Tracker Panels - TRACKER (Item A1) (Level 1)

Tag Data..................................... 2 (Level 1)

Product Data..................................2

Can you investigate and let me know its implementation.

Thanks,

Nithiyanandam

Hi

1. This vertical line is field separator. This issue was fixed few releases ago (v 4.4.1.0). Please try using the latest version of Aspose.Words.

2. I modified your code.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Insrt a table of contents at the beginning of the document.

builder.InsertTableOfContents("\\f \\h");

builder.InsertBreak(BreakType.PageBreak);

// Insert TC field that represents TOC element

builder.InsertField("TC \"Tracker Panels(Item 1)\" \\n \\l 1", "");

builder.Writeln("Tracker Panels");

builder.InsertField("TC \"Tag Data\" \\l 2", "");

builder.Writeln("Tag Data");

builder.InsertField("TC \"Product Data\" \\l 2", "");

builder.Writeln("Product Data");

doc.Save(@"out.doc");

Please let me know if you would like to know anything else.

Best regards.

Hi

Awesome. This really works. Thanks for your continous support to all my queries.

-- Nithiyanandam