Create a Cross-Reference to Bookmark in Word in C#, VB.NET

A cross-reference refers to related information elsewhere in the same document. You can create cross-references to any existing items such as headings, footnotes, bookmarks, captions, and numbered paragraphs. This article will show you how to create a cross-reference to bookmark using Spire.Doc with C# and VB.NET.

Step 1: Create a Document instance.

Document doc = new Document();
Section section = doc.AddSection();

Step 2: Insert a bookmark.

Paragraph paragraph = section.AddParagraph();
paragraph.AppendBookmarkStart("MyBookmark");
paragraph.AppendText("Text inside a bookmark");
paragraph.AppendBookmarkEnd("MyBookmark");

Step 3: Create a cross-reference field, and link it to the bookmark through bookmark name.

Field field = new Field(doc);
field.Type = FieldType.FieldRef;
field.Code = @"REF MyBookmark \p \h";

Step 4: Add a paragraph, and insert the field to the paragraph.

paragraph = section.AddParagraph();
paragraph.AppendText("For more information, see ");
paragraph.ChildObjects.Add(field);

Step 5: Insert a FieldSeparator object to the paragraph, which works as separator in a field.

FieldMark fieldSeparator= new FieldMark(doc, FieldMarkType.FieldSeparator);
paragraph.ChildObjects.Add(fieldSeparator);

Step 6: Set the display text of the cross-reference field.

TextRange tr = new TextRange(doc);
tr.Text = "above";
paragraph.ChildObjects.Add(tr);

Step 7: Insert a FieldEnd object to the paragraph, which is used to mark the end of a field.

FieldMark fieldEnd = new FieldMark(doc, FieldMarkType.FieldEnd);
paragraph.ChildObjects.Add(fieldEnd);

Step 8: Save to file.

doc.SaveToFile("output.docx", FileFormat.Docx2013);

Output:

The cross-reference appears as a link that takes the reader to the referenced item.

Create a Cross-Reference to Bookmark in Word in C#, VB.NET

Full Code:

[C#]
Document doc = new Document();
Section section = doc.AddSection();
//create a bookmark
Paragraph paragraph = section.AddParagraph();
paragraph.AppendBookmarkStart("MyBookmark");
paragraph.AppendText("Text inside a bookmark");
paragraph.AppendBookmarkEnd("MyBookmark");
//insert line breaks
for (int i = 0; i < 4; i++)
{
    paragraph.AppendBreak(BreakType.LineBreak);
}        
//create a cross-reference field, and link it to bookmark                    
Field field = new Field(doc);
field.Type = FieldType.FieldRef;
field.Code = @"REF MyBookmark \p \h";
//insert field to paragraph
paragraph = section.AddParagraph();
paragraph.AppendText("For more information, see ");
paragraph.ChildObjects.Add(field);
//insert FieldSeparator object
FieldMark fieldSeparator = new FieldMark(doc, FieldMarkType.FieldSeparator);
paragraph.ChildObjects.Add(fieldSeparator);
//set display text of the field
TextRange tr = new TextRange(doc);
tr.Text = "above";
paragraph.ChildObjects.Add(tr);
//insert FieldEnd object to mark the end of the field
FieldMark fieldEnd = new FieldMark(doc, FieldMarkType.FieldEnd);
paragraph.ChildObjects.Add(fieldEnd);
//save file
doc.SaveToFile("output.docx", FileFormat.Docx2013);
[VB.NET]
Document doc = New Document()
Dim section As Section = doc.AddSection()
'create a bookmark
Dim paragraph As Paragraph = section.AddParagraph()
paragraph.AppendBookmarkStart("MyBookmark")
paragraph.AppendText("Text inside a bookmark")
paragraph.AppendBookmarkEnd("MyBookmark")
'insert line breaks
Dim i As Integer
For i = 0 To  4- 1  Step i + 1
    paragraph.AppendBreak(BreakType.LineBreak)
Next
'create a cross-reference field, and link it to bookmark                    
Dim field As Field = New Field(doc)
field.Type = FieldType.FieldRef
field.Code = "REF MyBookmark \p \h"
'insert field to paragraph
paragraph = section.AddParagraph()
paragraph.AppendText("For more information, see ")
paragraph.ChildObjects.Add(field)
'insert FieldSeparator object
Dim fieldSeparator As FieldMark = New FieldMark(doc, FieldMarkType.FieldSeparator)
paragraph.ChildObjects.Add(fieldSeparator)
'set display text of the field
Dim tr As TextRange = New TextRange(doc)
tr.Text = "above"
paragraph.ChildObjects.Add(tr)
'insert FieldEnd object to mark the end of the field
Dim fieldEnd As FieldMark = New FieldMark(doc, FieldMarkType.FieldEnd)
paragraph.ChildObjects.Add(fieldEnd)
'save file
doc.SaveToFile("output.docx", FileFormat.Docx2013)