News Category

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

2017-12-21 07:54:20 Written by  support iceblue
Rate this item
(0 votes)

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#]
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace CreatCR
{
    class Program
    {
        static void Main(string[] args)
        {
            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]
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Namespace CreatCR
	Class Program
		Private Shared Sub Main(args As String())
			Dim doc As 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
			For i As Integer = 0 To 3
				paragraph.AppendBreak(BreakType.LineBreak)
			Next
			'create a cross-reference field, and link it to bookmark                    
			Dim field As 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 New FieldMark(doc, FieldMarkType.FieldSeparator)
			paragraph.ChildObjects.Add(fieldSeparator)
			'set display text of the field
			Dim tr As New TextRange(doc)
			tr.Text = "above"
			paragraph.ChildObjects.Add(tr)
			'insert FieldEnd object to mark the end of the field
			Dim fieldEnd As New FieldMark(doc, FieldMarkType.FieldEnd)
			paragraph.ChildObjects.Add(fieldEnd)
			'save file
			doc.SaveToFile("output.docx", FileFormat.Docx2013)

		End Sub
	End Class
End Namespace

Additional Info

  • tutorial_title:
Last modified on Friday, 03 September 2021 03:29