How can I insert a cross reference to the paragraph text and not to its number, with and without DocumentBuilder?
Hi Virgilio,
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.
Please see the attached file with examples of what I need.
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).
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?
FieldRef Reffield = (FieldRef)builder.insertField(FieldType.FIELD_REF, false);
Reffield.setBookmarkName("bm_heading");
Reffield.setInsertParagraphNumber(true);
Reffield.setInsertHyperlink(true);