FieldGreetingLine

FieldGreetingLine class

Implements the GREETINGLINE field.

public class FieldGreetingLine : Field

Constructors

Name Description
FieldGreetingLine() The default constructor.

Properties

Name Description
AlternateText { get; set; } Gets or sets the text to include in the field if the name is blank.
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).
LanguageId { get; set; } Gets or sets the language id used to format the name.
LocaleId { get; set; } Gets or sets the LCID of the field.
NameFormat { get; set; } Gets or sets the format of the name included in 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).
GetFieldNames() Returns a collection of mail merge field names used by the field.
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 a mail merge greeting line.

Examples

Shows how to insert a GREETINGLINE field.

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

// Create a generic greeting using a GREETINGLINE field, and some text after it.
FieldGreetingLine field = (FieldGreetingLine)builder.InsertField(FieldType.FieldGreetingLine, true);
builder.Writeln("\n\n\tThis is your custom greeting, created programmatically using Aspose Words!");

// A GREETINGLINE field accepts values from a data source during a mail merge, like a MERGEFIELD.
// It can also format how the source's data is written in its place once the mail merge is complete.
// The field names collection corresponds to the columns from the data source
// that the field will take values from.
Assert.AreEqual(0, field.GetFieldNames().Length);

// To populate that array, we need to specify a format for our greeting line.
field.NameFormat = "<< _BEFORE_ Dear >><< _TITLE0_ >><< _LAST0_ >><< _AFTER_ ,>> ";

// Now, our field will accept values from these two columns in the data source.
Assert.AreEqual("Courtesy Title", field.GetFieldNames()[0]);
Assert.AreEqual("Last Name", field.GetFieldNames()[1]);
Assert.AreEqual(2, field.GetFieldNames().Length);

// This string will cover any cases where the data table data is invalid
// by substituting the malformed name with a string.
field.AlternateText = "Sir or Madam";

// Set a locale to format the result.
field.LanguageId = new CultureInfo("en-US").LCID.ToString();

Assert.AreEqual(" GREETINGLINE  \\f \"<< _BEFORE_ Dear >><< _TITLE0_ >><< _LAST0_ >><< _AFTER_ ,>> \" \\e \"Sir or Madam\" \\l 1033", 
    field.GetFieldCode());

// Create a data table with columns whose names match elements
// from the field's field names collection, and then carry out the mail merge.
DataTable table = new DataTable("Employees");
table.Columns.Add("Courtesy Title");
table.Columns.Add("First Name");
table.Columns.Add("Last Name");
table.Rows.Add("Mr.", "John", "Doe");
table.Rows.Add("Mrs.", "Jane", "Cardholder");

// This row has an invalid value in the Courtesy Title column, so our greeting will default to the alternate text.
table.Rows.Add("", "No", "Name");

doc.MailMerge.Execute(table);

Assert.That(doc.Range.Fields, Is.Empty);
Assert.AreEqual("Dear Mr. Doe,\r\r\tThis is your custom greeting, created programmatically using Aspose Words!\r" +
                "\fDear Mrs. Cardholder,\r\r\tThis is your custom greeting, created programmatically using Aspose Words!\r" +
                "\fDear Sir or Madam,\r\r\tThis is your custom greeting, created programmatically using Aspose Words!",
    doc.GetText().Trim());

See Also