FieldAuthor

FieldAuthor class

Implements the AUTHOR field.

To learn more, visit the Working with Fields documentation article.

public class FieldAuthor : Field

Constructors

NameDescription
FieldAuthor()The default constructor.

Properties

NameDescription
AuthorName { get; set; }Gets or sets the document author’s name.
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.
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

NameDescription
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

Retrieves, and optionally sets, the document author’s name, as recorded in the Author property of the built-in document properties.

Examples

Shows how to use an AUTHOR field to display a document creator’s name.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// AUTHOR fields source their results from the built-in document property called "Author".
// If we create and save a document in Microsoft Word,
// it will have our username in that property.
// However, if we create a document programmatically using Aspose.Words,
// the "Author" property, by default, will be an empty string. 
Assert.AreEqual(string.Empty, doc.BuiltInDocumentProperties.Author);

// Set a backup author name for AUTHOR fields to use
// if the "Author" property contains an empty string.
doc.FieldOptions.DefaultDocumentAuthor = "Joe Bloggs";

builder.Write("This document was created by ");
FieldAuthor field = (FieldAuthor)builder.InsertField(FieldType.FieldAuthor, true);
field.Update();

Assert.AreEqual(" AUTHOR ", field.GetFieldCode());
Assert.AreEqual("Joe Bloggs", field.Result);

// Updating an AUTHOR field that contains a value
// will apply that value to the "Author" built-in property.
Assert.AreEqual("Joe Bloggs", doc.BuiltInDocumentProperties.Author);

// Changing this property, then updating the AUTHOR field will apply this value to the field.
doc.BuiltInDocumentProperties.Author = "John Doe";
field.Update();

Assert.AreEqual(" AUTHOR ", field.GetFieldCode());
Assert.AreEqual("John Doe", field.Result);

// If we update an AUTHOR field after changing its "Name" property,
// then the field will display the new name and apply the new name to the built-in property.
field.AuthorName = "Jane Doe";
field.Update();

Assert.AreEqual(" AUTHOR  \"Jane Doe\"", field.GetFieldCode());
Assert.AreEqual("Jane Doe", field.Result);

// AUTHOR fields do not affect the DefaultDocumentAuthor property.
Assert.AreEqual("Jane Doe", doc.BuiltInDocumentProperties.Author);
Assert.AreEqual("Joe Bloggs", doc.FieldOptions.DefaultDocumentAuthor);

doc.Save(ArtifactsDir + "Field.AUTHOR.docx");

See Also