FieldRef Class

Implements the REF field.
Inheritance Hierarchy

Namespace:  Aspose.Words.Fields
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public class FieldRef : Field

The FieldRef type exposes the following members.

Constructors
  NameDescription
Public methodFieldRef
Initializes a new instance of the FieldRef class
Properties
  NameDescription
Public propertyCode exampleBookmarkName
Gets or sets the referenced bookmark's name.
Public propertyCode exampleDisplayResult
Gets the text that represents the displayed field result.
(Inherited from Field.)
Public propertyCode exampleEnd
Gets the node that represents the field end.
(Inherited from Field.)
Public propertyCode exampleFormat
Gets a FieldFormat object that provides typed access to field's formatting.
(Inherited from Field.)
Public propertyCode exampleIncludeNoteOrComment
Gets or sets whether to increment footnote, endnote, and annotation numbers that are marked by the bookmark, and insert the corresponding footnote, endnote, and comment text.
Public propertyCode exampleInsertHyperlink
Gets or sets whether to create a hyperlink to the bookmarked paragraph.
Public propertyCode exampleInsertParagraphNumber
Gets or sets whether to insert the paragraph number of the referenced paragraph exactly as it appears in the document.
Public propertyCode exampleInsertParagraphNumberInFullContext
Gets or sets whether to insert the paragraph number of the referenced paragraph in full context.
Public propertyCode exampleInsertParagraphNumberInRelativeContext
Gets or sets whether to insert the paragraph number of the referenced paragraph in relative context.
Public propertyCode exampleInsertRelativePosition
Gets or sets whether to insert the relative position of the referenced paragraph.
Public propertyCode exampleIsDirty
Gets or sets whether the current result of the field is no longer correct (stale) due to other modifications made to the document.
(Inherited from Field.)
Public propertyCode exampleIsLocked
Gets or sets whether the field is locked (should not recalculate its result).
(Inherited from Field.)
Public propertyCode exampleLocaleId
Gets or sets the LCID of the field.
(Inherited from Field.)
Public propertyCode exampleNumberSeparator
Gets or sets the character sequence that is used to separate sequence numbers and page numbers.
Public propertyCode exampleResult
Gets or sets text that is between the field separator and field end.
(Inherited from Field.)
Public propertyCode exampleSeparator
Gets the node that represents the field separator. Can be null.
(Inherited from Field.)
Public propertyCode exampleStart
Gets the node that represents the start of the field.
(Inherited from Field.)
Public propertyCode exampleSuppressNonDelimiters
Gets or sets whether to suppress non-delimiter characters.
Public propertyCode exampleType
Gets the Microsoft Word field type.
(Inherited from Field.)
Methods
  NameDescription
Public methodEquals (Inherited from Object.)
Public methodCode exampleGetFieldCode
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.
(Inherited from Field.)
Public methodCode exampleGetFieldCode(Boolean)
Returns text between field start and field separator (or field end if there is no separator).
(Inherited from Field.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Public methodCode exampleRemove
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.
(Inherited from Field.)
Public methodToString (Inherited from Object.)
Public methodCode exampleUnlink
Performs the field unlink.
(Inherited from Field.)
Public methodCode exampleUpdate
Performs the field update. Throws if the field is being updated already.
(Inherited from Field.)
Public methodCode exampleUpdate(Boolean)
Performs a field update. Throws if the field is being updated already.
(Inherited from Field.)
Remarks
Inserts the text or graphics represented by the specified bookmark.
Examples
Shows how to insert REF fields to reference bookmarks and present them in various ways.
[Ignore("WORDSNET-18067")]
public void FieldRef()
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    // Insert the bookmark that all our REF fields will reference and leave it at the end of the document
    builder.StartBookmark("MyBookmark");
    builder.InsertFootnote(FootnoteType.Footnote, "MyBookmark footnote #1");
    builder.Write("Text that will appear in REF field");
    builder.InsertFootnote(FootnoteType.Footnote, "MyBookmark footnote #2");
    builder.EndBookmark("MyBookmark");
    builder.MoveToDocumentStart();

    // We will apply a custom list format, where the amount of angle brackets indicates the list level we are currently at
    // Note that the angle brackets count as non-delimiter characters
    builder.ListFormat.ApplyNumberDefault();
    builder.ListFormat.ListLevel.NumberFormat = "> \x0000";

    // Insert a REF field that will contain the text within our bookmark, act as a hyperlink, and clone the bookmark's footnotes
    FieldRef field = InsertFieldRef(builder, "MyBookmark", "", "\n");
    field.IncludeNoteOrComment = true;
    field.InsertHyperlink = true;

    Assert.AreEqual(" REF  MyBookmark \\f \\h", field.GetFieldCode());

    // Insert a REF field and display whether the referenced bookmark is above or below it
    field = InsertFieldRef(builder, "MyBookmark", "The referenced paragraph is ", " this field.\n");
    field.InsertRelativePosition = true;

    Assert.AreEqual(" REF  MyBookmark \\p", field.GetFieldCode());

    // Display the list number of the bookmark, as it appears in the document
    field = InsertFieldRef(builder, "MyBookmark", "The bookmark's paragraph number is ", "\n");
    field.InsertParagraphNumber = true;

    Assert.AreEqual(" REF  MyBookmark \\n", field.GetFieldCode());

    // Display the list number of the bookmark, but with non-delimiter characters omitted
    // In this case they are the angle brackets
    field = InsertFieldRef(builder, "MyBookmark", "The bookmark's paragraph number, non-delimiters suppressed, is ", "\n");
    field.InsertParagraphNumber = true;
    field.SuppressNonDelimiters = true;

    Assert.AreEqual(" REF  MyBookmark \\n \\t", field.GetFieldCode());

    // Move down one list level
    builder.ListFormat.ListLevelNumber++;
    builder.ListFormat.ListLevel.NumberFormat = ">> \x0001";

    // Display the list number of the bookmark as well as the numbers of all the list levels above it
    field = InsertFieldRef(builder, "MyBookmark", "The bookmark's full context paragraph number is ", "\n");
    field.InsertParagraphNumberInFullContext = true;

    Assert.AreEqual(" REF  MyBookmark \\w", field.GetFieldCode());

    builder.InsertBreak(BreakType.PageBreak);

    // Display the list level numbers between this REF field and the bookmark that it is referencing
    field = InsertFieldRef(builder, "MyBookmark", "The bookmark's relative paragraph number is ", "\n");
    field.InsertParagraphNumberInRelativeContext = true;

    Assert.AreEqual(" REF  MyBookmark \\r", field.GetFieldCode());

    // The bookmark, which is at the end of the document, will show up as a list item here
    builder.Writeln("List level above bookmark");
    builder.ListFormat.ListLevelNumber++;
    builder.ListFormat.ListLevel.NumberFormat = ">>> \x0002";

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

/// <summary>
/// Get the document builder to insert a REF field, reference a bookmark with it, and add text before and after.
/// </summary>
private static FieldRef InsertFieldRef(DocumentBuilder builder, string bookmarkName, string textBefore, string textAfter)
{
    builder.Write(textBefore);
    FieldRef field = (FieldRef)builder.InsertField(FieldType.FieldRef, true);
    field.BookmarkName = bookmarkName;
    builder.Write(textAfter);
    return field;
}
See Also