public class FieldPageRef extends Field
To learn more, visit the Working with Fields documentation article.
Remarks:
Inserts the number of the page containing the specified bookmark for a cross-reference.
Examples:
Shows to insert PAGEREF fields to display the relative location of bookmarks.
public void fieldPageRef() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
insertAndNameBookmark(builder, "MyBookmark1");
// Insert a PAGEREF field that displays what page a bookmark is on.
// Set the InsertHyperlink flag to make the field also function as a clickable link to the bookmark.
Assert.assertEquals(" PAGEREF MyBookmark3 \\h",
insertFieldPageRef(builder, "MyBookmark3", true, false, "Hyperlink to Bookmark3, on page: ").getFieldCode());
// We can use the \p flag to get the PAGEREF field to display
// the bookmark's position relative to the position of the field.
// Bookmark1 is on the same page and above this field, so this field's displayed result will be "above".
Assert.assertEquals(" PAGEREF MyBookmark1 \\h \\p",
insertFieldPageRef(builder, "MyBookmark1", true, true, "Bookmark1 is ").getFieldCode());
// Bookmark2 will be on the same page and below this field, so this field's displayed result will be "below".
Assert.assertEquals(" PAGEREF MyBookmark2 \\h \\p",
insertFieldPageRef(builder, "MyBookmark2", true, true, "Bookmark2 is ").getFieldCode());
// Bookmark3 will be on a different page, so the field will display "on page 2".
Assert.assertEquals(" PAGEREF MyBookmark3 \\h \\p",
insertFieldPageRef(builder, "MyBookmark3", true, true, "Bookmark3 is ").getFieldCode());
insertAndNameBookmark(builder, "MyBookmark2");
builder.insertBreak(BreakType.PAGE_BREAK);
insertAndNameBookmark(builder, "MyBookmark3");
doc.updatePageLayout();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.PAGEREF.docx");
}
/// <summary>
/// Uses a document builder to insert a PAGEREF field and sets its properties.
/// </summary>
private FieldPageRef insertFieldPageRef(final DocumentBuilder builder, final String bookmarkName, final boolean insertHyperlink,
final boolean insertRelativePosition, final String textBefore) throws Exception {
builder.write(textBefore);
FieldPageRef field = (FieldPageRef) builder.insertField(FieldType.FIELD_PAGE_REF, true);
field.setBookmarkName(bookmarkName);
field.setInsertHyperlink(insertHyperlink);
field.setInsertRelativePosition(insertRelativePosition);
builder.writeln();
return field;
}
/// <summary>
/// Uses a document builder to insert a named bookmark.
/// </summary>
private void insertAndNameBookmark(final DocumentBuilder builder, final String bookmarkName) {
builder.startBookmark(bookmarkName);
builder.writeln(MessageFormat.format("Contents of bookmark \"{0}\".", bookmarkName));
builder.endBookmark(bookmarkName);
}
| 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 |
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 |
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 PAGEREF fields to display the relative location of bookmarks.
public void fieldPageRef() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
insertAndNameBookmark(builder, "MyBookmark1");
// Insert a PAGEREF field that displays what page a bookmark is on.
// Set the InsertHyperlink flag to make the field also function as a clickable link to the bookmark.
Assert.assertEquals(" PAGEREF MyBookmark3 \\h",
insertFieldPageRef(builder, "MyBookmark3", true, false, "Hyperlink to Bookmark3, on page: ").getFieldCode());
// We can use the \p flag to get the PAGEREF field to display
// the bookmark's position relative to the position of the field.
// Bookmark1 is on the same page and above this field, so this field's displayed result will be "above".
Assert.assertEquals(" PAGEREF MyBookmark1 \\h \\p",
insertFieldPageRef(builder, "MyBookmark1", true, true, "Bookmark1 is ").getFieldCode());
// Bookmark2 will be on the same page and below this field, so this field's displayed result will be "below".
Assert.assertEquals(" PAGEREF MyBookmark2 \\h \\p",
insertFieldPageRef(builder, "MyBookmark2", true, true, "Bookmark2 is ").getFieldCode());
// Bookmark3 will be on a different page, so the field will display "on page 2".
Assert.assertEquals(" PAGEREF MyBookmark3 \\h \\p",
insertFieldPageRef(builder, "MyBookmark3", true, true, "Bookmark3 is ").getFieldCode());
insertAndNameBookmark(builder, "MyBookmark2");
builder.insertBreak(BreakType.PAGE_BREAK);
insertAndNameBookmark(builder, "MyBookmark3");
doc.updatePageLayout();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.PAGEREF.docx");
}
/// <summary>
/// Uses a document builder to insert a PAGEREF field and sets its properties.
/// </summary>
private FieldPageRef insertFieldPageRef(final DocumentBuilder builder, final String bookmarkName, final boolean insertHyperlink,
final boolean insertRelativePosition, final String textBefore) throws Exception {
builder.write(textBefore);
FieldPageRef field = (FieldPageRef) builder.insertField(FieldType.FIELD_PAGE_REF, true);
field.setBookmarkName(bookmarkName);
field.setInsertHyperlink(insertHyperlink);
field.setInsertRelativePosition(insertRelativePosition);
builder.writeln();
return field;
}
/// <summary>
/// Uses a document builder to insert a named bookmark.
/// </summary>
private void insertAndNameBookmark(final DocumentBuilder builder, final String bookmarkName) {
builder.startBookmark(bookmarkName);
builder.writeln(MessageFormat.format("Contents of bookmark \"{0}\".", bookmarkName));
builder.endBookmark(bookmarkName);
}
public void setBookmarkName(java.lang.String value)
throws java.lang.Exception
Examples:
Shows to insert PAGEREF fields to display the relative location of bookmarks.
public void fieldPageRef() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
insertAndNameBookmark(builder, "MyBookmark1");
// Insert a PAGEREF field that displays what page a bookmark is on.
// Set the InsertHyperlink flag to make the field also function as a clickable link to the bookmark.
Assert.assertEquals(" PAGEREF MyBookmark3 \\h",
insertFieldPageRef(builder, "MyBookmark3", true, false, "Hyperlink to Bookmark3, on page: ").getFieldCode());
// We can use the \p flag to get the PAGEREF field to display
// the bookmark's position relative to the position of the field.
// Bookmark1 is on the same page and above this field, so this field's displayed result will be "above".
Assert.assertEquals(" PAGEREF MyBookmark1 \\h \\p",
insertFieldPageRef(builder, "MyBookmark1", true, true, "Bookmark1 is ").getFieldCode());
// Bookmark2 will be on the same page and below this field, so this field's displayed result will be "below".
Assert.assertEquals(" PAGEREF MyBookmark2 \\h \\p",
insertFieldPageRef(builder, "MyBookmark2", true, true, "Bookmark2 is ").getFieldCode());
// Bookmark3 will be on a different page, so the field will display "on page 2".
Assert.assertEquals(" PAGEREF MyBookmark3 \\h \\p",
insertFieldPageRef(builder, "MyBookmark3", true, true, "Bookmark3 is ").getFieldCode());
insertAndNameBookmark(builder, "MyBookmark2");
builder.insertBreak(BreakType.PAGE_BREAK);
insertAndNameBookmark(builder, "MyBookmark3");
doc.updatePageLayout();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.PAGEREF.docx");
}
/// <summary>
/// Uses a document builder to insert a PAGEREF field and sets its properties.
/// </summary>
private FieldPageRef insertFieldPageRef(final DocumentBuilder builder, final String bookmarkName, final boolean insertHyperlink,
final boolean insertRelativePosition, final String textBefore) throws Exception {
builder.write(textBefore);
FieldPageRef field = (FieldPageRef) builder.insertField(FieldType.FIELD_PAGE_REF, true);
field.setBookmarkName(bookmarkName);
field.setInsertHyperlink(insertHyperlink);
field.setInsertRelativePosition(insertRelativePosition);
builder.writeln();
return field;
}
/// <summary>
/// Uses a document builder to insert a named bookmark.
/// </summary>
private void insertAndNameBookmark(final DocumentBuilder builder, final String bookmarkName) {
builder.startBookmark(bookmarkName);
builder.writeln(MessageFormat.format("Contents of bookmark \"{0}\".", bookmarkName));
builder.endBookmark(bookmarkName);
}
value - The name of the bookmark.java.lang.Exceptionpublic boolean getInsertHyperlink()
Examples:
Shows to insert PAGEREF fields to display the relative location of bookmarks.
public void fieldPageRef() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
insertAndNameBookmark(builder, "MyBookmark1");
// Insert a PAGEREF field that displays what page a bookmark is on.
// Set the InsertHyperlink flag to make the field also function as a clickable link to the bookmark.
Assert.assertEquals(" PAGEREF MyBookmark3 \\h",
insertFieldPageRef(builder, "MyBookmark3", true, false, "Hyperlink to Bookmark3, on page: ").getFieldCode());
// We can use the \p flag to get the PAGEREF field to display
// the bookmark's position relative to the position of the field.
// Bookmark1 is on the same page and above this field, so this field's displayed result will be "above".
Assert.assertEquals(" PAGEREF MyBookmark1 \\h \\p",
insertFieldPageRef(builder, "MyBookmark1", true, true, "Bookmark1 is ").getFieldCode());
// Bookmark2 will be on the same page and below this field, so this field's displayed result will be "below".
Assert.assertEquals(" PAGEREF MyBookmark2 \\h \\p",
insertFieldPageRef(builder, "MyBookmark2", true, true, "Bookmark2 is ").getFieldCode());
// Bookmark3 will be on a different page, so the field will display "on page 2".
Assert.assertEquals(" PAGEREF MyBookmark3 \\h \\p",
insertFieldPageRef(builder, "MyBookmark3", true, true, "Bookmark3 is ").getFieldCode());
insertAndNameBookmark(builder, "MyBookmark2");
builder.insertBreak(BreakType.PAGE_BREAK);
insertAndNameBookmark(builder, "MyBookmark3");
doc.updatePageLayout();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.PAGEREF.docx");
}
/// <summary>
/// Uses a document builder to insert a PAGEREF field and sets its properties.
/// </summary>
private FieldPageRef insertFieldPageRef(final DocumentBuilder builder, final String bookmarkName, final boolean insertHyperlink,
final boolean insertRelativePosition, final String textBefore) throws Exception {
builder.write(textBefore);
FieldPageRef field = (FieldPageRef) builder.insertField(FieldType.FIELD_PAGE_REF, true);
field.setBookmarkName(bookmarkName);
field.setInsertHyperlink(insertHyperlink);
field.setInsertRelativePosition(insertRelativePosition);
builder.writeln();
return field;
}
/// <summary>
/// Uses a document builder to insert a named bookmark.
/// </summary>
private void insertAndNameBookmark(final DocumentBuilder builder, final String bookmarkName) {
builder.startBookmark(bookmarkName);
builder.writeln(MessageFormat.format("Contents of bookmark \"{0}\".", bookmarkName));
builder.endBookmark(bookmarkName);
}
public void setInsertHyperlink(boolean value)
throws java.lang.Exception
Examples:
Shows to insert PAGEREF fields to display the relative location of bookmarks.
public void fieldPageRef() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
insertAndNameBookmark(builder, "MyBookmark1");
// Insert a PAGEREF field that displays what page a bookmark is on.
// Set the InsertHyperlink flag to make the field also function as a clickable link to the bookmark.
Assert.assertEquals(" PAGEREF MyBookmark3 \\h",
insertFieldPageRef(builder, "MyBookmark3", true, false, "Hyperlink to Bookmark3, on page: ").getFieldCode());
// We can use the \p flag to get the PAGEREF field to display
// the bookmark's position relative to the position of the field.
// Bookmark1 is on the same page and above this field, so this field's displayed result will be "above".
Assert.assertEquals(" PAGEREF MyBookmark1 \\h \\p",
insertFieldPageRef(builder, "MyBookmark1", true, true, "Bookmark1 is ").getFieldCode());
// Bookmark2 will be on the same page and below this field, so this field's displayed result will be "below".
Assert.assertEquals(" PAGEREF MyBookmark2 \\h \\p",
insertFieldPageRef(builder, "MyBookmark2", true, true, "Bookmark2 is ").getFieldCode());
// Bookmark3 will be on a different page, so the field will display "on page 2".
Assert.assertEquals(" PAGEREF MyBookmark3 \\h \\p",
insertFieldPageRef(builder, "MyBookmark3", true, true, "Bookmark3 is ").getFieldCode());
insertAndNameBookmark(builder, "MyBookmark2");
builder.insertBreak(BreakType.PAGE_BREAK);
insertAndNameBookmark(builder, "MyBookmark3");
doc.updatePageLayout();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.PAGEREF.docx");
}
/// <summary>
/// Uses a document builder to insert a PAGEREF field and sets its properties.
/// </summary>
private FieldPageRef insertFieldPageRef(final DocumentBuilder builder, final String bookmarkName, final boolean insertHyperlink,
final boolean insertRelativePosition, final String textBefore) throws Exception {
builder.write(textBefore);
FieldPageRef field = (FieldPageRef) builder.insertField(FieldType.FIELD_PAGE_REF, true);
field.setBookmarkName(bookmarkName);
field.setInsertHyperlink(insertHyperlink);
field.setInsertRelativePosition(insertRelativePosition);
builder.writeln();
return field;
}
/// <summary>
/// Uses a document builder to insert a named bookmark.
/// </summary>
private void insertAndNameBookmark(final DocumentBuilder builder, final String bookmarkName) {
builder.startBookmark(bookmarkName);
builder.writeln(MessageFormat.format("Contents of bookmark \"{0}\".", bookmarkName));
builder.endBookmark(bookmarkName);
}
value - Whether to insert a hyperlink to the bookmarked paragraph.java.lang.Exceptionpublic boolean getInsertRelativePosition()
Examples:
Shows to insert PAGEREF fields to display the relative location of bookmarks.
public void fieldPageRef() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
insertAndNameBookmark(builder, "MyBookmark1");
// Insert a PAGEREF field that displays what page a bookmark is on.
// Set the InsertHyperlink flag to make the field also function as a clickable link to the bookmark.
Assert.assertEquals(" PAGEREF MyBookmark3 \\h",
insertFieldPageRef(builder, "MyBookmark3", true, false, "Hyperlink to Bookmark3, on page: ").getFieldCode());
// We can use the \p flag to get the PAGEREF field to display
// the bookmark's position relative to the position of the field.
// Bookmark1 is on the same page and above this field, so this field's displayed result will be "above".
Assert.assertEquals(" PAGEREF MyBookmark1 \\h \\p",
insertFieldPageRef(builder, "MyBookmark1", true, true, "Bookmark1 is ").getFieldCode());
// Bookmark2 will be on the same page and below this field, so this field's displayed result will be "below".
Assert.assertEquals(" PAGEREF MyBookmark2 \\h \\p",
insertFieldPageRef(builder, "MyBookmark2", true, true, "Bookmark2 is ").getFieldCode());
// Bookmark3 will be on a different page, so the field will display "on page 2".
Assert.assertEquals(" PAGEREF MyBookmark3 \\h \\p",
insertFieldPageRef(builder, "MyBookmark3", true, true, "Bookmark3 is ").getFieldCode());
insertAndNameBookmark(builder, "MyBookmark2");
builder.insertBreak(BreakType.PAGE_BREAK);
insertAndNameBookmark(builder, "MyBookmark3");
doc.updatePageLayout();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.PAGEREF.docx");
}
/// <summary>
/// Uses a document builder to insert a PAGEREF field and sets its properties.
/// </summary>
private FieldPageRef insertFieldPageRef(final DocumentBuilder builder, final String bookmarkName, final boolean insertHyperlink,
final boolean insertRelativePosition, final String textBefore) throws Exception {
builder.write(textBefore);
FieldPageRef field = (FieldPageRef) builder.insertField(FieldType.FIELD_PAGE_REF, true);
field.setBookmarkName(bookmarkName);
field.setInsertHyperlink(insertHyperlink);
field.setInsertRelativePosition(insertRelativePosition);
builder.writeln();
return field;
}
/// <summary>
/// Uses a document builder to insert a named bookmark.
/// </summary>
private void insertAndNameBookmark(final DocumentBuilder builder, final String bookmarkName) {
builder.startBookmark(bookmarkName);
builder.writeln(MessageFormat.format("Contents of bookmark \"{0}\".", bookmarkName));
builder.endBookmark(bookmarkName);
}
public void setInsertRelativePosition(boolean value)
throws java.lang.Exception
Examples:
Shows to insert PAGEREF fields to display the relative location of bookmarks.
public void fieldPageRef() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
insertAndNameBookmark(builder, "MyBookmark1");
// Insert a PAGEREF field that displays what page a bookmark is on.
// Set the InsertHyperlink flag to make the field also function as a clickable link to the bookmark.
Assert.assertEquals(" PAGEREF MyBookmark3 \\h",
insertFieldPageRef(builder, "MyBookmark3", true, false, "Hyperlink to Bookmark3, on page: ").getFieldCode());
// We can use the \p flag to get the PAGEREF field to display
// the bookmark's position relative to the position of the field.
// Bookmark1 is on the same page and above this field, so this field's displayed result will be "above".
Assert.assertEquals(" PAGEREF MyBookmark1 \\h \\p",
insertFieldPageRef(builder, "MyBookmark1", true, true, "Bookmark1 is ").getFieldCode());
// Bookmark2 will be on the same page and below this field, so this field's displayed result will be "below".
Assert.assertEquals(" PAGEREF MyBookmark2 \\h \\p",
insertFieldPageRef(builder, "MyBookmark2", true, true, "Bookmark2 is ").getFieldCode());
// Bookmark3 will be on a different page, so the field will display "on page 2".
Assert.assertEquals(" PAGEREF MyBookmark3 \\h \\p",
insertFieldPageRef(builder, "MyBookmark3", true, true, "Bookmark3 is ").getFieldCode());
insertAndNameBookmark(builder, "MyBookmark2");
builder.insertBreak(BreakType.PAGE_BREAK);
insertAndNameBookmark(builder, "MyBookmark3");
doc.updatePageLayout();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.PAGEREF.docx");
}
/// <summary>
/// Uses a document builder to insert a PAGEREF field and sets its properties.
/// </summary>
private FieldPageRef insertFieldPageRef(final DocumentBuilder builder, final String bookmarkName, final boolean insertHyperlink,
final boolean insertRelativePosition, final String textBefore) throws Exception {
builder.write(textBefore);
FieldPageRef field = (FieldPageRef) builder.insertField(FieldType.FIELD_PAGE_REF, true);
field.setBookmarkName(bookmarkName);
field.setInsertHyperlink(insertHyperlink);
field.setInsertRelativePosition(insertRelativePosition);
builder.writeln();
return field;
}
/// <summary>
/// Uses a document builder to insert a named bookmark.
/// </summary>
private void insertAndNameBookmark(final DocumentBuilder builder, final String bookmarkName) {
builder.startBookmark(bookmarkName);
builder.writeln(MessageFormat.format("Contents of bookmark \"{0}\".", bookmarkName));
builder.endBookmark(bookmarkName);
}
value - Whether to insert a relative position of the bookmarked paragraph.java.lang.Exceptionpublic int getSwitchType(java.lang.String switchName)