FieldPageRef

FieldPageRef class

Implements the PAGEREF field.

public class FieldPageRef : Field

Constructors

Name Description
FieldPageRef() The default constructor.

Properties

Name Description
BookmarkName { get; set; } Gets or sets the name of the bookmark.
DisplayResult { get; } Gets the text that represents the displayed field result.
End { get; } Gets the node that represents the field end.
Format { get; } Gets a FieldFormat object that provides typed access to field’s formatting.
InsertHyperlink { get; set; } Gets or sets whether to insert a hyperlink to the bookmarked paragraph.
InsertRelativePosition { get; set; } Gets or sets whether to insert a relative position of the bookmarked paragraph.
IsDirty { get; set; } Gets or sets whether the current result of the field is no longer correct (stale) due to other modifications made to the document.
IsLocked { get; set; } Gets or sets whether the field is locked (should not recalculate its result).
LocaleId { get; set; } Gets or sets the LCID of the field.
Result { get; set; } Gets or sets text that is between the field separator and field end.
Separator { get; } Gets the node that represents the field separator. Can be null.
Start { get; } Gets the node that represents the start of the field.
virtual Type { get; } Gets the Microsoft Word field type.

Methods

Name Description
GetFieldCode() Returns text between field start and field separator (or field end if there is no separator). Both field code and field result of child fields are included.
GetFieldCode(bool) Returns text between field start and field separator (or field end if there is no separator).
Remove() Removes the field from the document. Returns a node right after the field. If the field’s end is the last child of its parent node, returns its parent paragraph. If the field is already removed, returns null.
Unlink() Performs the field unlink.
Update() Performs the field update. Throws if the field is being updated already.
Update(bool) Performs a field update. Throws if the field is being updated already.

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()
{
    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.AreEqual(" 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.AreEqual(" 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.AreEqual(" 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.AreEqual(" PAGEREF  MyBookmark3 \\h \\p", 
        InsertFieldPageRef(builder, "MyBookmark3", true, true, "Bookmark3 is ").GetFieldCode());

    InsertAndNameBookmark(builder, "MyBookmark2");
    builder.InsertBreak(BreakType.PageBreak);
    InsertAndNameBookmark(builder, "MyBookmark3");

    doc.UpdateFields();
    doc.Save(ArtifactsDir + "Field.PAGEREF.docx");

/// <summary>
/// Uses a document builder to insert a PAGEREF field and sets its properties.
/// </summary>
private static FieldPageRef InsertFieldPageRef(DocumentBuilder builder, string bookmarkName, bool insertHyperlink, bool insertRelativePosition, string textBefore)
{
    builder.Write(textBefore);

    FieldPageRef field = (FieldPageRef)builder.InsertField(FieldType.FieldPageRef, true);
    field.BookmarkName = bookmarkName;
    field.InsertHyperlink = insertHyperlink;
    field.InsertRelativePosition = insertRelativePosition;
    builder.Writeln();

    return field;
}

/// <summary>
/// Uses a document builder to insert a named bookmark.
/// </summary>
private static void InsertAndNameBookmark(DocumentBuilder builder, string bookmarkName)
{
    builder.StartBookmark(bookmarkName);
    builder.Writeln($"Contents of bookmark \"{bookmarkName}\".");
    builder.EndBookmark(bookmarkName);
}

See Also