Hi,
Hi
Thanks for your request. Sure you can achieve this. You should simply insert a bookmark before the TOC and insert headings with link to this bookmark. For instance see the following code:
// 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 bookmark before a TOC.
builder.StartBookmark("toc");
// Insert a table of contents at the beginning of the document.
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
builder.EndBookmark("toc");
// 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;
// Insert heading as a hyperlink to bookmark.
builder.InsertHyperlink("Heading 1", "toc", true);
builder.Writeln();
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.InsertHyperlink("Heading 1.1", "toc", true);
builder.Writeln();
builder.InsertHyperlink("Heading 1.2", "toc", true);
builder.Writeln();
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.InsertHyperlink("Heading 2", "toc", true);
builder.Writeln();
builder.InsertHyperlink("Heading 3", "toc", true);
builder.Writeln();
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.InsertHyperlink("Heading 3.1", "toc", true);
builder.Writeln();
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3;
builder.InsertHyperlink("Heading 3.1.1", "toc", true);
builder.Writeln();
builder.InsertHyperlink("Heading 3.1.2", "toc", true);
builder.Writeln();
builder.InsertHyperlink("Heading 3.1.3", "toc", true);
builder.Writeln();
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.InsertHyperlink("Heading 3.2", "toc", true);
builder.Writeln();
builder.InsertHyperlink("Heading 3.3", "toc", true);
builder.Writeln();
// Call the method below to update the TOC.
doc.UpdateFields();
doc.Save(@"Test001\out.doc");
Hope this helps.
Best regards,
Hi,
Hi
Thanks for your request. I created a simple code for you:
[Test]
public void Test001()
{
// Open document.
Document doc = new Document(@"Test001\in.docx");
// Create DocumentBuilder and insert a bookmark at the begginign of the document.
// This bookmark will be the point our hyperlinks will follow to.
DocumentBuilder builder = new DocumentBuilder(doc);
const string bookmarkName = "start";
builder.StartBookmark(bookmarkName);
builder.EndBookmark(bookmarkName);
// Now get all paragraphs and replace heading paragraphs with hyperlinks to the inserted bookmark.
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph paragraph in paragraphs)
{
if(paragraph.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1 ||
paragraph.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading2 ||
paragraph.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading3)
{
ParagraphToHyperlink(paragraph, bookmarkName, true);
}
}
// Save output
doc.Save(@"Test001\out.docx");
}
///
/// Replaces paragraph's content. Paragraph's text is used as displayed text of hyperlink.
///
///
private void ParagraphToHyperlink(Paragraph paragraph, string hyperlink, bool isBookmark)
{
// Create DocumentBuilder that will allow us to insert Hyperlink.
DocumentBuilder builder =new DocumentBuilder((Document)paragraph.Document);
// Move DocumentBuilder's cursor to the paragraph.
builder.MoveTo(paragraph);
// Insert Hyperlink field.
Field hyperlinkField = builder.InsertField(string.Format("HYPERLINK {1} \"{0}\"", hyperlink, isBookmark ? "\\l" : ""), "");
// Move all content of the paragraph into the fireld values (between field separator and field end).
while (!paragraph.FirstChild.Equals(hyperlinkField.Start))
{
paragraph.InsertAfter(paragraph.FirstChild, hyperlinkField.Separator);
}
}
Hope it helps.
Best regards,