public class FieldNoteRef extends Field
To learn more, visit the Working with Fields documentation article.
Remarks:
Inserts the mark of the footnote or endnote that is marked by the specified bookmark.
Examples:
Shows how to cross-reference footnotes with the NOTEREF field.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("CrossReference: ");
FieldNoteRef field = (FieldNoteRef)builder.insertField(FieldType.FIELD_NOTE_REF, false); // <--- don't update field
field.setBookmarkName("CrossRefBookmark");
field.setInsertHyperlink(true);
field.setInsertReferenceMark(true);
field.setInsertRelativePosition(false);
builder.writeln();
builder.startBookmark("CrossRefBookmark");
builder.write("Hello world!");
builder.insertFootnote(FootnoteType.FOOTNOTE, "Cross referenced footnote.");
builder.endBookmark("CrossRefBookmark");
builder.writeln();
doc.updateFields();
// This field works only in older versions of Microsoft Word.
doc.save(getArtifactsDir() + "Field.NOTEREF.doc");
Shows to insert NOTEREF fields, and modify their appearance.
public void fieldNoteRef() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a bookmark with a footnote that the NOTEREF field will reference.
insertBookmarkWithFootnote(builder, "MyBookmark1", "Contents of MyBookmark1", "Footnote from MyBookmark1");
// This NOTEREF field will display the number of the footnote inside the referenced bookmark.
// Setting the InsertHyperlink property lets us jump to the bookmark by Ctrl + clicking the field in Microsoft Word.
Assert.assertEquals(" NOTEREF MyBookmark2 \\h",
insertFieldNoteRef(builder, "MyBookmark2", true, false, false, "Hyperlink to Bookmark2, with footnote number ").getFieldCode());
// When using the \p flag, after the footnote number, the field also displays the bookmark's position relative to the field.
// Bookmark1 is above this field and contains footnote number 1, so the result will be "1 above" on update.
Assert.assertEquals(" NOTEREF MyBookmark1 \\h \\p",
insertFieldNoteRef(builder, "MyBookmark1", true, true, false, "Bookmark1, with footnote number ").getFieldCode());
// Bookmark2 is below this field and contains footnote number 2, so the field will display "2 below".
// The \f flag makes the number 2 appear in the same format as the footnote number label in the actual text.
Assert.assertEquals(" NOTEREF MyBookmark2 \\h \\p \\f",
insertFieldNoteRef(builder, "MyBookmark2", true, true, true, "Bookmark2, with footnote number ").getFieldCode());
builder.insertBreak(BreakType.PAGE_BREAK);
insertBookmarkWithFootnote(builder, "MyBookmark2", "Contents of MyBookmark2", "Footnote from MyBookmark2");
doc.updatePageLayout();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.NOTEREF.docx");
}
/// <summary>
/// Uses a document builder to insert a NOTEREF field with specified properties.
/// </summary>
private static FieldNoteRef insertFieldNoteRef(DocumentBuilder builder, String bookmarkName, boolean insertHyperlink, boolean insertRelativePosition, boolean insertReferenceMark, String textBefore) throws Exception {
builder.write(textBefore);
FieldNoteRef field = (FieldNoteRef) builder.insertField(FieldType.FIELD_NOTE_REF, true);
field.setBookmarkName(bookmarkName);
field.setInsertHyperlink(insertHyperlink);
field.setInsertRelativePosition(insertRelativePosition);
field.setInsertReferenceMark(insertReferenceMark);
builder.writeln();
return field;
}
/// <summary>
/// Uses a document builder to insert a named bookmark with a footnote at the end.
/// </summary>
private void insertBookmarkWithFootnote(final DocumentBuilder builder, final String bookmarkName,
final String bookmarkText, final String footnoteText) {
builder.startBookmark(bookmarkName);
builder.write(bookmarkText);
builder.insertFootnote(FootnoteType.FOOTNOTE, footnoteText);
builder.endBookmark(bookmarkName);
builder.writeln();
}
| Modifier and Type | Method and Description |
|---|---|
java.lang.String |
getBookmarkName()
Gets the name of the bookmark.
|
boolean |
getInsertHyperlink()
Gets whether to insert a hyperlink to the bookmarked paragraph.
|
boolean |
getInsertReferenceMark()
Inserts the reference mark with the same character formatting as the Footnote Reference or Endnote Reference style.
|
boolean |
getInsertRelativePosition()
Gets whether to insert a relative position of the bookmarked paragraph.
|
int |
getSwitchType(java.lang.String switchName) |
void |
setBookmarkName(java.lang.String value)
Sets the name of the bookmark.
|
void |
setInsertHyperlink(boolean value)
Sets whether to insert a hyperlink to the bookmarked paragraph.
|
void |
setInsertReferenceMark(boolean value)
Inserts the reference mark with the same character formatting as the Footnote Reference or Endnote Reference style.
|
void |
setInsertRelativePosition(boolean value)
Sets whether to insert a relative position of the bookmarked paragraph.
|
getDisplayResult, getEnd, getFieldCode, getFieldCode, getFormat, getLocaleId, getResult, getSeparator, getStart, getType, isDirty, isDirty, isLocked, isLocked, needStoreOldResultNodes, remove, setLocaleId, setResult, unlink, update, updatepublic java.lang.String getBookmarkName()
Examples:
Shows to insert NOTEREF fields, and modify their appearance.
public void fieldNoteRef() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a bookmark with a footnote that the NOTEREF field will reference.
insertBookmarkWithFootnote(builder, "MyBookmark1", "Contents of MyBookmark1", "Footnote from MyBookmark1");
// This NOTEREF field will display the number of the footnote inside the referenced bookmark.
// Setting the InsertHyperlink property lets us jump to the bookmark by Ctrl + clicking the field in Microsoft Word.
Assert.assertEquals(" NOTEREF MyBookmark2 \\h",
insertFieldNoteRef(builder, "MyBookmark2", true, false, false, "Hyperlink to Bookmark2, with footnote number ").getFieldCode());
// When using the \p flag, after the footnote number, the field also displays the bookmark's position relative to the field.
// Bookmark1 is above this field and contains footnote number 1, so the result will be "1 above" on update.
Assert.assertEquals(" NOTEREF MyBookmark1 \\h \\p",
insertFieldNoteRef(builder, "MyBookmark1", true, true, false, "Bookmark1, with footnote number ").getFieldCode());
// Bookmark2 is below this field and contains footnote number 2, so the field will display "2 below".
// The \f flag makes the number 2 appear in the same format as the footnote number label in the actual text.
Assert.assertEquals(" NOTEREF MyBookmark2 \\h \\p \\f",
insertFieldNoteRef(builder, "MyBookmark2", true, true, true, "Bookmark2, with footnote number ").getFieldCode());
builder.insertBreak(BreakType.PAGE_BREAK);
insertBookmarkWithFootnote(builder, "MyBookmark2", "Contents of MyBookmark2", "Footnote from MyBookmark2");
doc.updatePageLayout();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.NOTEREF.docx");
}
/// <summary>
/// Uses a document builder to insert a NOTEREF field with specified properties.
/// </summary>
private static FieldNoteRef insertFieldNoteRef(DocumentBuilder builder, String bookmarkName, boolean insertHyperlink, boolean insertRelativePosition, boolean insertReferenceMark, String textBefore) throws Exception {
builder.write(textBefore);
FieldNoteRef field = (FieldNoteRef) builder.insertField(FieldType.FIELD_NOTE_REF, true);
field.setBookmarkName(bookmarkName);
field.setInsertHyperlink(insertHyperlink);
field.setInsertRelativePosition(insertRelativePosition);
field.setInsertReferenceMark(insertReferenceMark);
builder.writeln();
return field;
}
/// <summary>
/// Uses a document builder to insert a named bookmark with a footnote at the end.
/// </summary>
private void insertBookmarkWithFootnote(final DocumentBuilder builder, final String bookmarkName,
final String bookmarkText, final String footnoteText) {
builder.startBookmark(bookmarkName);
builder.write(bookmarkText);
builder.insertFootnote(FootnoteType.FOOTNOTE, footnoteText);
builder.endBookmark(bookmarkName);
builder.writeln();
}
public void setBookmarkName(java.lang.String value)
throws java.lang.Exception
Examples:
Shows to insert NOTEREF fields, and modify their appearance.
public void fieldNoteRef() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a bookmark with a footnote that the NOTEREF field will reference.
insertBookmarkWithFootnote(builder, "MyBookmark1", "Contents of MyBookmark1", "Footnote from MyBookmark1");
// This NOTEREF field will display the number of the footnote inside the referenced bookmark.
// Setting the InsertHyperlink property lets us jump to the bookmark by Ctrl + clicking the field in Microsoft Word.
Assert.assertEquals(" NOTEREF MyBookmark2 \\h",
insertFieldNoteRef(builder, "MyBookmark2", true, false, false, "Hyperlink to Bookmark2, with footnote number ").getFieldCode());
// When using the \p flag, after the footnote number, the field also displays the bookmark's position relative to the field.
// Bookmark1 is above this field and contains footnote number 1, so the result will be "1 above" on update.
Assert.assertEquals(" NOTEREF MyBookmark1 \\h \\p",
insertFieldNoteRef(builder, "MyBookmark1", true, true, false, "Bookmark1, with footnote number ").getFieldCode());
// Bookmark2 is below this field and contains footnote number 2, so the field will display "2 below".
// The \f flag makes the number 2 appear in the same format as the footnote number label in the actual text.
Assert.assertEquals(" NOTEREF MyBookmark2 \\h \\p \\f",
insertFieldNoteRef(builder, "MyBookmark2", true, true, true, "Bookmark2, with footnote number ").getFieldCode());
builder.insertBreak(BreakType.PAGE_BREAK);
insertBookmarkWithFootnote(builder, "MyBookmark2", "Contents of MyBookmark2", "Footnote from MyBookmark2");
doc.updatePageLayout();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.NOTEREF.docx");
}
/// <summary>
/// Uses a document builder to insert a NOTEREF field with specified properties.
/// </summary>
private static FieldNoteRef insertFieldNoteRef(DocumentBuilder builder, String bookmarkName, boolean insertHyperlink, boolean insertRelativePosition, boolean insertReferenceMark, String textBefore) throws Exception {
builder.write(textBefore);
FieldNoteRef field = (FieldNoteRef) builder.insertField(FieldType.FIELD_NOTE_REF, true);
field.setBookmarkName(bookmarkName);
field.setInsertHyperlink(insertHyperlink);
field.setInsertRelativePosition(insertRelativePosition);
field.setInsertReferenceMark(insertReferenceMark);
builder.writeln();
return field;
}
/// <summary>
/// Uses a document builder to insert a named bookmark with a footnote at the end.
/// </summary>
private void insertBookmarkWithFootnote(final DocumentBuilder builder, final String bookmarkName,
final String bookmarkText, final String footnoteText) {
builder.startBookmark(bookmarkName);
builder.write(bookmarkText);
builder.insertFootnote(FootnoteType.FOOTNOTE, footnoteText);
builder.endBookmark(bookmarkName);
builder.writeln();
}
value - The name of the bookmark.java.lang.Exceptionpublic boolean getInsertReferenceMark()
Examples:
Shows to insert NOTEREF fields, and modify their appearance.
public void fieldNoteRef() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a bookmark with a footnote that the NOTEREF field will reference.
insertBookmarkWithFootnote(builder, "MyBookmark1", "Contents of MyBookmark1", "Footnote from MyBookmark1");
// This NOTEREF field will display the number of the footnote inside the referenced bookmark.
// Setting the InsertHyperlink property lets us jump to the bookmark by Ctrl + clicking the field in Microsoft Word.
Assert.assertEquals(" NOTEREF MyBookmark2 \\h",
insertFieldNoteRef(builder, "MyBookmark2", true, false, false, "Hyperlink to Bookmark2, with footnote number ").getFieldCode());
// When using the \p flag, after the footnote number, the field also displays the bookmark's position relative to the field.
// Bookmark1 is above this field and contains footnote number 1, so the result will be "1 above" on update.
Assert.assertEquals(" NOTEREF MyBookmark1 \\h \\p",
insertFieldNoteRef(builder, "MyBookmark1", true, true, false, "Bookmark1, with footnote number ").getFieldCode());
// Bookmark2 is below this field and contains footnote number 2, so the field will display "2 below".
// The \f flag makes the number 2 appear in the same format as the footnote number label in the actual text.
Assert.assertEquals(" NOTEREF MyBookmark2 \\h \\p \\f",
insertFieldNoteRef(builder, "MyBookmark2", true, true, true, "Bookmark2, with footnote number ").getFieldCode());
builder.insertBreak(BreakType.PAGE_BREAK);
insertBookmarkWithFootnote(builder, "MyBookmark2", "Contents of MyBookmark2", "Footnote from MyBookmark2");
doc.updatePageLayout();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.NOTEREF.docx");
}
/// <summary>
/// Uses a document builder to insert a NOTEREF field with specified properties.
/// </summary>
private static FieldNoteRef insertFieldNoteRef(DocumentBuilder builder, String bookmarkName, boolean insertHyperlink, boolean insertRelativePosition, boolean insertReferenceMark, String textBefore) throws Exception {
builder.write(textBefore);
FieldNoteRef field = (FieldNoteRef) builder.insertField(FieldType.FIELD_NOTE_REF, true);
field.setBookmarkName(bookmarkName);
field.setInsertHyperlink(insertHyperlink);
field.setInsertRelativePosition(insertRelativePosition);
field.setInsertReferenceMark(insertReferenceMark);
builder.writeln();
return field;
}
/// <summary>
/// Uses a document builder to insert a named bookmark with a footnote at the end.
/// </summary>
private void insertBookmarkWithFootnote(final DocumentBuilder builder, final String bookmarkName,
final String bookmarkText, final String footnoteText) {
builder.startBookmark(bookmarkName);
builder.write(bookmarkText);
builder.insertFootnote(FootnoteType.FOOTNOTE, footnoteText);
builder.endBookmark(bookmarkName);
builder.writeln();
}
boolean value.public void setInsertReferenceMark(boolean value)
throws java.lang.Exception
Examples:
Shows to insert NOTEREF fields, and modify their appearance.
public void fieldNoteRef() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a bookmark with a footnote that the NOTEREF field will reference.
insertBookmarkWithFootnote(builder, "MyBookmark1", "Contents of MyBookmark1", "Footnote from MyBookmark1");
// This NOTEREF field will display the number of the footnote inside the referenced bookmark.
// Setting the InsertHyperlink property lets us jump to the bookmark by Ctrl + clicking the field in Microsoft Word.
Assert.assertEquals(" NOTEREF MyBookmark2 \\h",
insertFieldNoteRef(builder, "MyBookmark2", true, false, false, "Hyperlink to Bookmark2, with footnote number ").getFieldCode());
// When using the \p flag, after the footnote number, the field also displays the bookmark's position relative to the field.
// Bookmark1 is above this field and contains footnote number 1, so the result will be "1 above" on update.
Assert.assertEquals(" NOTEREF MyBookmark1 \\h \\p",
insertFieldNoteRef(builder, "MyBookmark1", true, true, false, "Bookmark1, with footnote number ").getFieldCode());
// Bookmark2 is below this field and contains footnote number 2, so the field will display "2 below".
// The \f flag makes the number 2 appear in the same format as the footnote number label in the actual text.
Assert.assertEquals(" NOTEREF MyBookmark2 \\h \\p \\f",
insertFieldNoteRef(builder, "MyBookmark2", true, true, true, "Bookmark2, with footnote number ").getFieldCode());
builder.insertBreak(BreakType.PAGE_BREAK);
insertBookmarkWithFootnote(builder, "MyBookmark2", "Contents of MyBookmark2", "Footnote from MyBookmark2");
doc.updatePageLayout();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.NOTEREF.docx");
}
/// <summary>
/// Uses a document builder to insert a NOTEREF field with specified properties.
/// </summary>
private static FieldNoteRef insertFieldNoteRef(DocumentBuilder builder, String bookmarkName, boolean insertHyperlink, boolean insertRelativePosition, boolean insertReferenceMark, String textBefore) throws Exception {
builder.write(textBefore);
FieldNoteRef field = (FieldNoteRef) builder.insertField(FieldType.FIELD_NOTE_REF, true);
field.setBookmarkName(bookmarkName);
field.setInsertHyperlink(insertHyperlink);
field.setInsertRelativePosition(insertRelativePosition);
field.setInsertReferenceMark(insertReferenceMark);
builder.writeln();
return field;
}
/// <summary>
/// Uses a document builder to insert a named bookmark with a footnote at the end.
/// </summary>
private void insertBookmarkWithFootnote(final DocumentBuilder builder, final String bookmarkName,
final String bookmarkText, final String footnoteText) {
builder.startBookmark(bookmarkName);
builder.write(bookmarkText);
builder.insertFootnote(FootnoteType.FOOTNOTE, footnoteText);
builder.endBookmark(bookmarkName);
builder.writeln();
}
value - The corresponding boolean value.java.lang.Exceptionpublic boolean getInsertHyperlink()
Examples:
Shows to insert NOTEREF fields, and modify their appearance.
public void fieldNoteRef() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a bookmark with a footnote that the NOTEREF field will reference.
insertBookmarkWithFootnote(builder, "MyBookmark1", "Contents of MyBookmark1", "Footnote from MyBookmark1");
// This NOTEREF field will display the number of the footnote inside the referenced bookmark.
// Setting the InsertHyperlink property lets us jump to the bookmark by Ctrl + clicking the field in Microsoft Word.
Assert.assertEquals(" NOTEREF MyBookmark2 \\h",
insertFieldNoteRef(builder, "MyBookmark2", true, false, false, "Hyperlink to Bookmark2, with footnote number ").getFieldCode());
// When using the \p flag, after the footnote number, the field also displays the bookmark's position relative to the field.
// Bookmark1 is above this field and contains footnote number 1, so the result will be "1 above" on update.
Assert.assertEquals(" NOTEREF MyBookmark1 \\h \\p",
insertFieldNoteRef(builder, "MyBookmark1", true, true, false, "Bookmark1, with footnote number ").getFieldCode());
// Bookmark2 is below this field and contains footnote number 2, so the field will display "2 below".
// The \f flag makes the number 2 appear in the same format as the footnote number label in the actual text.
Assert.assertEquals(" NOTEREF MyBookmark2 \\h \\p \\f",
insertFieldNoteRef(builder, "MyBookmark2", true, true, true, "Bookmark2, with footnote number ").getFieldCode());
builder.insertBreak(BreakType.PAGE_BREAK);
insertBookmarkWithFootnote(builder, "MyBookmark2", "Contents of MyBookmark2", "Footnote from MyBookmark2");
doc.updatePageLayout();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.NOTEREF.docx");
}
/// <summary>
/// Uses a document builder to insert a NOTEREF field with specified properties.
/// </summary>
private static FieldNoteRef insertFieldNoteRef(DocumentBuilder builder, String bookmarkName, boolean insertHyperlink, boolean insertRelativePosition, boolean insertReferenceMark, String textBefore) throws Exception {
builder.write(textBefore);
FieldNoteRef field = (FieldNoteRef) builder.insertField(FieldType.FIELD_NOTE_REF, true);
field.setBookmarkName(bookmarkName);
field.setInsertHyperlink(insertHyperlink);
field.setInsertRelativePosition(insertRelativePosition);
field.setInsertReferenceMark(insertReferenceMark);
builder.writeln();
return field;
}
/// <summary>
/// Uses a document builder to insert a named bookmark with a footnote at the end.
/// </summary>
private void insertBookmarkWithFootnote(final DocumentBuilder builder, final String bookmarkName,
final String bookmarkText, final String footnoteText) {
builder.startBookmark(bookmarkName);
builder.write(bookmarkText);
builder.insertFootnote(FootnoteType.FOOTNOTE, footnoteText);
builder.endBookmark(bookmarkName);
builder.writeln();
}
public void setInsertHyperlink(boolean value)
throws java.lang.Exception
Examples:
Shows to insert NOTEREF fields, and modify their appearance.
public void fieldNoteRef() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a bookmark with a footnote that the NOTEREF field will reference.
insertBookmarkWithFootnote(builder, "MyBookmark1", "Contents of MyBookmark1", "Footnote from MyBookmark1");
// This NOTEREF field will display the number of the footnote inside the referenced bookmark.
// Setting the InsertHyperlink property lets us jump to the bookmark by Ctrl + clicking the field in Microsoft Word.
Assert.assertEquals(" NOTEREF MyBookmark2 \\h",
insertFieldNoteRef(builder, "MyBookmark2", true, false, false, "Hyperlink to Bookmark2, with footnote number ").getFieldCode());
// When using the \p flag, after the footnote number, the field also displays the bookmark's position relative to the field.
// Bookmark1 is above this field and contains footnote number 1, so the result will be "1 above" on update.
Assert.assertEquals(" NOTEREF MyBookmark1 \\h \\p",
insertFieldNoteRef(builder, "MyBookmark1", true, true, false, "Bookmark1, with footnote number ").getFieldCode());
// Bookmark2 is below this field and contains footnote number 2, so the field will display "2 below".
// The \f flag makes the number 2 appear in the same format as the footnote number label in the actual text.
Assert.assertEquals(" NOTEREF MyBookmark2 \\h \\p \\f",
insertFieldNoteRef(builder, "MyBookmark2", true, true, true, "Bookmark2, with footnote number ").getFieldCode());
builder.insertBreak(BreakType.PAGE_BREAK);
insertBookmarkWithFootnote(builder, "MyBookmark2", "Contents of MyBookmark2", "Footnote from MyBookmark2");
doc.updatePageLayout();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.NOTEREF.docx");
}
/// <summary>
/// Uses a document builder to insert a NOTEREF field with specified properties.
/// </summary>
private static FieldNoteRef insertFieldNoteRef(DocumentBuilder builder, String bookmarkName, boolean insertHyperlink, boolean insertRelativePosition, boolean insertReferenceMark, String textBefore) throws Exception {
builder.write(textBefore);
FieldNoteRef field = (FieldNoteRef) builder.insertField(FieldType.FIELD_NOTE_REF, true);
field.setBookmarkName(bookmarkName);
field.setInsertHyperlink(insertHyperlink);
field.setInsertRelativePosition(insertRelativePosition);
field.setInsertReferenceMark(insertReferenceMark);
builder.writeln();
return field;
}
/// <summary>
/// Uses a document builder to insert a named bookmark with a footnote at the end.
/// </summary>
private void insertBookmarkWithFootnote(final DocumentBuilder builder, final String bookmarkName,
final String bookmarkText, final String footnoteText) {
builder.startBookmark(bookmarkName);
builder.write(bookmarkText);
builder.insertFootnote(FootnoteType.FOOTNOTE, footnoteText);
builder.endBookmark(bookmarkName);
builder.writeln();
}
value - Whether to insert a hyperlink to the bookmarked paragraph.java.lang.Exceptionpublic boolean getInsertRelativePosition()
Examples:
Shows to insert NOTEREF fields, and modify their appearance.
public void fieldNoteRef() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a bookmark with a footnote that the NOTEREF field will reference.
insertBookmarkWithFootnote(builder, "MyBookmark1", "Contents of MyBookmark1", "Footnote from MyBookmark1");
// This NOTEREF field will display the number of the footnote inside the referenced bookmark.
// Setting the InsertHyperlink property lets us jump to the bookmark by Ctrl + clicking the field in Microsoft Word.
Assert.assertEquals(" NOTEREF MyBookmark2 \\h",
insertFieldNoteRef(builder, "MyBookmark2", true, false, false, "Hyperlink to Bookmark2, with footnote number ").getFieldCode());
// When using the \p flag, after the footnote number, the field also displays the bookmark's position relative to the field.
// Bookmark1 is above this field and contains footnote number 1, so the result will be "1 above" on update.
Assert.assertEquals(" NOTEREF MyBookmark1 \\h \\p",
insertFieldNoteRef(builder, "MyBookmark1", true, true, false, "Bookmark1, with footnote number ").getFieldCode());
// Bookmark2 is below this field and contains footnote number 2, so the field will display "2 below".
// The \f flag makes the number 2 appear in the same format as the footnote number label in the actual text.
Assert.assertEquals(" NOTEREF MyBookmark2 \\h \\p \\f",
insertFieldNoteRef(builder, "MyBookmark2", true, true, true, "Bookmark2, with footnote number ").getFieldCode());
builder.insertBreak(BreakType.PAGE_BREAK);
insertBookmarkWithFootnote(builder, "MyBookmark2", "Contents of MyBookmark2", "Footnote from MyBookmark2");
doc.updatePageLayout();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.NOTEREF.docx");
}
/// <summary>
/// Uses a document builder to insert a NOTEREF field with specified properties.
/// </summary>
private static FieldNoteRef insertFieldNoteRef(DocumentBuilder builder, String bookmarkName, boolean insertHyperlink, boolean insertRelativePosition, boolean insertReferenceMark, String textBefore) throws Exception {
builder.write(textBefore);
FieldNoteRef field = (FieldNoteRef) builder.insertField(FieldType.FIELD_NOTE_REF, true);
field.setBookmarkName(bookmarkName);
field.setInsertHyperlink(insertHyperlink);
field.setInsertRelativePosition(insertRelativePosition);
field.setInsertReferenceMark(insertReferenceMark);
builder.writeln();
return field;
}
/// <summary>
/// Uses a document builder to insert a named bookmark with a footnote at the end.
/// </summary>
private void insertBookmarkWithFootnote(final DocumentBuilder builder, final String bookmarkName,
final String bookmarkText, final String footnoteText) {
builder.startBookmark(bookmarkName);
builder.write(bookmarkText);
builder.insertFootnote(FootnoteType.FOOTNOTE, footnoteText);
builder.endBookmark(bookmarkName);
builder.writeln();
}
public void setInsertRelativePosition(boolean value)
throws java.lang.Exception
Examples:
Shows to insert NOTEREF fields, and modify their appearance.
public void fieldNoteRef() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a bookmark with a footnote that the NOTEREF field will reference.
insertBookmarkWithFootnote(builder, "MyBookmark1", "Contents of MyBookmark1", "Footnote from MyBookmark1");
// This NOTEREF field will display the number of the footnote inside the referenced bookmark.
// Setting the InsertHyperlink property lets us jump to the bookmark by Ctrl + clicking the field in Microsoft Word.
Assert.assertEquals(" NOTEREF MyBookmark2 \\h",
insertFieldNoteRef(builder, "MyBookmark2", true, false, false, "Hyperlink to Bookmark2, with footnote number ").getFieldCode());
// When using the \p flag, after the footnote number, the field also displays the bookmark's position relative to the field.
// Bookmark1 is above this field and contains footnote number 1, so the result will be "1 above" on update.
Assert.assertEquals(" NOTEREF MyBookmark1 \\h \\p",
insertFieldNoteRef(builder, "MyBookmark1", true, true, false, "Bookmark1, with footnote number ").getFieldCode());
// Bookmark2 is below this field and contains footnote number 2, so the field will display "2 below".
// The \f flag makes the number 2 appear in the same format as the footnote number label in the actual text.
Assert.assertEquals(" NOTEREF MyBookmark2 \\h \\p \\f",
insertFieldNoteRef(builder, "MyBookmark2", true, true, true, "Bookmark2, with footnote number ").getFieldCode());
builder.insertBreak(BreakType.PAGE_BREAK);
insertBookmarkWithFootnote(builder, "MyBookmark2", "Contents of MyBookmark2", "Footnote from MyBookmark2");
doc.updatePageLayout();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.NOTEREF.docx");
}
/// <summary>
/// Uses a document builder to insert a NOTEREF field with specified properties.
/// </summary>
private static FieldNoteRef insertFieldNoteRef(DocumentBuilder builder, String bookmarkName, boolean insertHyperlink, boolean insertRelativePosition, boolean insertReferenceMark, String textBefore) throws Exception {
builder.write(textBefore);
FieldNoteRef field = (FieldNoteRef) builder.insertField(FieldType.FIELD_NOTE_REF, true);
field.setBookmarkName(bookmarkName);
field.setInsertHyperlink(insertHyperlink);
field.setInsertRelativePosition(insertRelativePosition);
field.setInsertReferenceMark(insertReferenceMark);
builder.writeln();
return field;
}
/// <summary>
/// Uses a document builder to insert a named bookmark with a footnote at the end.
/// </summary>
private void insertBookmarkWithFootnote(final DocumentBuilder builder, final String bookmarkName,
final String bookmarkText, final String footnoteText) {
builder.startBookmark(bookmarkName);
builder.write(bookmarkText);
builder.insertFootnote(FootnoteType.FOOTNOTE, footnoteText);
builder.endBookmark(bookmarkName);
builder.writeln();
}
value - Whether to insert a relative position of the bookmarked paragraph.java.lang.Exceptionpublic int getSwitchType(java.lang.String switchName)