We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Insert a cross reference to the paragraph text

How can I insert a cross reference to the paragraph text and not to its number, with and without DocumentBuilder?

Hi Virgilio,


Thanks for your inquiry. Please use the following code example to insert footnote into the document without using DocumentBuilder.


Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.write(“Some text is added.”);

Footnote footnote = new Footnote(doc, FootnoteType.FOOTNOTE);

builder.getCurrentParagraph().appendChild(footnote);

footnote.getParagraphs().add(new Paragraph(doc));

Run run = new Run(doc, “” + (char)0x2);

run.getFont().setPosition(5);

run.getFont().setSize( 6);

footnote.getFirstParagraph().getRuns().add(run);

run = new Run(doc, “Footnote text.”);

run.getFont().setSize( 8);

footnote.getFirstParagraph().getRuns().add(run);

footnote.getFont().setSize(8);

footnote.getFont().setSuperscript(true);

doc.save(MyDir + “Out.docx”);



Please use DocumentBuilder.InsertFootnote method insert a footnote or endnote into the document using DocumentBuilder.


<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write(“Some text is added.”);

builder.insertFootnote(FootnoteType.FOOTNOTE, “Footer Note.”);
doc.save(“output.docx”);

Thank you. I mean a cross reference to a heading. For example:

1. This is the paragraph whose text I want to refer to.
(…)
This is the reference: This is the paragraph whose text I want to refer to.



Hi Virgilio,

Thanks for your inquiry. Could you please share your input and expected output documents here for our reference? We will then provide you more information about your query along with code.

Please see the attached file with examples of what I need.

Hi Virgilio,

Thanks for sharing the detail. We are working over your query and will get back to you soon.

Thanks. To be clearer, attached is an image of Word’s insert cross reference dialog where the user does what I need. The reference type is the selected value in the combo “Inserir referência para” (Insert reference to), “Texto do parágrafo” (Paragraph text).

Hi Virgilio,

Thanks for sharing the detail. The Ref field inserts the text or graphics represented by the specified bookmark. The bookmark must be defined in the active document. The Ref field syntax is { [REF] Bookmark [Switches ] }.

You need to bookmark the paragraph and use the Ref field. Please use following code example to achieve your requirements. Hope this helps you.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);

builder.startBookmark("bm_heading");
builder.writeln("This is the paragraph whose text I want to refer to.");
builder.endBookmark("bm_heading");
builder.getParagraphFormat().clearFormatting();
builder.writeln();
builder.writeln();

FieldRef Reffield = (FieldRef)builder.insertField(FieldType.FIELD_REF, false);
Reffield.setBookmarkName("bm_heading");
Reffield.update();

doc.save(MyDir + "output.docx");

Thank you for your response. How in the insertField method I inform that I want the reference to be to the paragraph number or text? The default is to the paragraph number and the FieldRef class has no method to set the text option.

Anyway, it’s more important to me to know the switches. For example, I already know this:

FieldRef f = (FieldRef) par.appendField(“REF " + bookmark + " \h \n”);

Where can I find documentation on all possible switches?

Hi Virgilio,

Thanks for your inquiry. We suggest you please read the members of Ref field class and following article:
Introduction to Fields

MS Word creates the Ref field of type "Heading" with hidden bookmark. This bookmark is started with an underscore character (_). Please check the hidden bookmarks in the document after creating the cross reference.

You can create the same reference feild using Aspose.Words. E.g. To create a cross reference of type heading, you need to bookmark the heading text and use that bookmark's name in reference field.

FieldRef.InsertHyperlink property is used to get or set whether to create a hyperlink to the bookmarked paragraph. FieldRef.InsertParagraphNumber property is used to get or set whether to insert the paragraph number of the referenced paragraph exactly as it appears in the document. Please use these properties to set the field switch as "\n \h".

FieldRef Reffield = (FieldRef)builder.insertField(FieldType.FIELD_REF, false);
Reffield.setBookmarkName("bm_heading");
Reffield.setInsertParagraphNumber(true);
Reffield.setInsertHyperlink(true);
Reffield.update();