Hello Everyone,
Greetings!
I am one of the user of Aspose. By using Aspose.Words I’m creating word file which created table of content automatically with hyperlink to title. But I want to insert some text between TOC items and
- this text will appears in TOC only not be appears in document and
- hyperlink is not applicable to this text
Example -
Table Of content
Topic 1…1
Topic 2…2
Topic 3…4
Topic 4…6
[Text to Insert]
Topic 5…7
Topic 6…8
Topic 7…9
I’m stuck into this. Please can anyone help me out from this.
Thanks!
Akshay.
Please need help for above scenario…
@akshayk,
Please see these sample input/output Word documents (SampleDocs.zip (11.6 KB)) and try running the following code.
C# Code to Edit Table Of Content & Insert Paragraph between TOC Entries in Word Document
Document doc = new Document("D:\\Temp\\sample-input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
BookmarkStart bmStart = null;
foreach (FieldStart field in doc.GetChildNodes(NodeType.FieldStart, true))
{
if (field.FieldType.Equals(FieldType.FieldHyperlink))
{
FieldHyperlink hyperlink = (FieldHyperlink)field.GetField();
if (hyperlink.SubAddress != null && hyperlink.SubAddress.StartsWith("_Toc"))
{
Paragraph tocItem = (Paragraph)field.GetAncestor(NodeType.Paragraph);
if (tocItem != null && tocItem.ToString(SaveFormat.Text).Trim().StartsWith("Heading 3.1.2"))
{
builder.MoveTo(tocItem.FirstChild);
bmStart = builder.StartBookmark("bm");
builder.EndBookmark("bm");
}
}
}
}
Paragraph prevPara = (Paragraph)bmStart.GetAncestor(NodeType.Paragraph);
Paragraph para = (Paragraph) prevPara.Clone(false);
Run run = new Run(doc, "NEW LINE");
run.Font.Bold = true;
para.Runs.Add(run);
prevPara.ParentNode.InsertAfter(para, prevPara);
doc.Save("D:\\Temp\\18.8.docx");