C#/VB.NET – How to Create a Cross-Reference in Word

A cross-reference allows you to link to other parts of the same document. You can create a cross-reference to link to a title, a footnote, a bookmark, an endnote, and so on. After the user clicks the cross-reference link, the document will automatically jump to the specified location. In this article, you will learn how to create a cross-reference using Free Spire.Doc for .NET.

INSTALLING Free SPIRE.DOC

First of all, you need to download the latest version of Free Spire.Doc from this link, and manually add the DLL files in your .NET application as references. Or, you can install it directly via NuGet.

Using the code

Cross-references are inserted into your document as fields. The field code inside a cross-reference field refers to a bookmark that points out the target. A bookmark in Word is a named location or a named block of text or other content in a document. Therefore, the following code snippet will show you how to insert a hidden bookmark and how to create a cross-reference that refers to this bookmark.

[C#]

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace CreateCrossReference
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object, and add a section to it
            Document doc = new Document();
            Section section = doc.AddSection();

            //Add a paragraph, based on which the cross reference will be created 
            Paragraph firstPara = section.AddParagraph();
            firstPara.ApplyStyle(BuiltinStyle.BodyText);

            for (int i = 0; i < 3; i++)
            {
                //Add a paragraph and apply a built-in style "Heading2" 
                Paragraph paragraph = section.AddParagraph();
                string headingText = string.Format("Chapter {0}", i + 1);
                paragraph.AppendText(headingText);
                paragraph.ApplyStyle(BuiltinStyle.Heading2);

                //Add a paragraph and apply a built-in style "BodyText"
                paragraph = section.AddParagraph();
                paragraph.AppendText("Here is the content...");
                paragraph.ApplyStyle(BuiltinStyle.BodyText);
            }

            //Add a hidden bookmark for the specified paragraph (A hidden bookmark name starts with a "_")
            section.Paragraphs[5].AppendBookmarkStart("_chapter3");
            section.Paragraphs[5].AppendBookmarkEnd("_chapter3");

            //Create a cross referecen filed for the bookmark "_chapter3"               
            Field field = new Field(doc);
            field.Type = FieldType.FieldRef;
            field.Code = @"REF _chapter3 \p \h";

            //Add the field to the first paragraph
            firstPara.ChildObjects.Add(field);

            //Add a field separator to the first paragraph      
            FieldMark fieldSeparator = new FieldMark(doc, FieldMarkType.FieldSeparator);
            firstPara.ChildObjects.Add(fieldSeparator);

            //Set the text of field
            TextRange tr = new TextRange(doc);
            tr.Text = "Jump to Chapter 3";
            tr.CharacterFormat.UnderlineStyle = UnderlineStyle.Single;
            firstPara.ChildObjects.Add(tr);

            //Add a field end to the first paragraph
            FieldMark fieldEnd = new FieldMark(doc, FieldMarkType.FieldEnd);
            firstPara.ChildObjects.Add(fieldEnd);

            //Save to file
            doc.SaveToFile("CrossReference.docx", FileFormat.Docx2013);
        }
    }
}

[VB.NET]

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
 
Namespace CreateCrossReference
    Class Program
        Shared  Sub Main(ByVal args() As String)
            'Create a Document object, and add a section to it
            Document doc  =  New Document()
            Dim section As Section =  doc.AddSection() 
 
            'Add a paragraph, based on which the cross reference will be created 
            Dim firstPara As Paragraph =  section.AddParagraph() 
            firstPara.ApplyStyle(BuiltinStyle.BodyText)
 
            Dim i As Integer
            For  i = 0 To  3- 1  Step  i + 1
                'Add a paragraph and apply a built-in style "Heading2" 
                Dim paragraph As Paragraph =  section.AddParagraph() 
                Dim headingText As String =  String.Format("Chapter {0}",i + 1) 
                paragraph.AppendText(headingText)
                paragraph.ApplyStyle(BuiltinStyle.Heading2)
 
                'Add a paragraph and apply a built-in style "BodyText"
                paragraph = section.AddParagraph()
                paragraph.AppendText("Here is the content...")
                paragraph.ApplyStyle(BuiltinStyle.BodyText)
            Next
 
            'Add a hidden bookmark for the specified paragraph (A hidden bookmark name starts with a "_")
            section.Paragraphs(5).AppendBookmarkStart("_chapter3")
            section.Paragraphs(5).AppendBookmarkEnd("_chapter3")
 
            'Create a cross referecen filed for the bookmark "_chapter3"               
            Dim field As Field =  New Field(doc) 
            field.Type = FieldType.FieldRef
            field.Code = "REF _chapter3 \p \h"
 
            'Add the field to the first paragraph
            firstPara.ChildObjects.Add(field)
 
            'Add a field separator to the first paragraph      
            Dim fieldSeparator As FieldMark =  New FieldMark(doc,FieldMarkType.FieldSeparator) 
            firstPara.ChildObjects.Add(fieldSeparator)
 
            'Set the text of field
            Dim tr As TextRange =  New TextRange(doc) 
            tr.Text = "Jump to Chapter 3"
            tr.CharacterFormat.UnderlineStyle = UnderlineStyle.Single
            firstPara.ChildObjects.Add(tr)
 
            'Add a field end to the first paragraph
            Dim fieldEnd As FieldMark =  New FieldMark(doc,FieldMarkType.FieldEnd) 
            firstPara.ChildObjects.Add(fieldEnd)
 
            'Save to file
            doc.SaveToFile("CrossReference.docx", FileFormat.Docx2013)
        End Sub
    End Class
End Namespace

OUTPUT

Leave a comment