FieldMergingArgsText Property
Gets or sets the text that will be inserted into the document for the current merge field.

Namespace: Aspose.Words.MailMerging
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 20.1
Syntax
public string Text { get; set; }

Property Value

Type: String
Remarks

When your event handler is called, this property is set to null.

If you leave Text as null, the mail merge engine will insert FieldValue in place of the merge field.

If you set Text to any string (including empty), the string will be inserted into the document in place of the merge field.

Examples
Shows how to mail merge HTML data into a document.
public void MailMergeInsertHtml()
{
    // File 'MailMerge.InsertHtml.doc' has merge field named 'htmlField1' in it
    // File 'MailMerge.HtmlData.html' contains some valid HTML data
    // The same approach can be used when merging HTML data from database
    Document doc = new Document(MyDir + "MailMerge.InsertHtml.doc");

    // Add a handler for the MergeField event
    doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertHtml();

    // Load some HTML from file
    StreamReader sr = File.OpenText(MyDir + "MailMerge.HtmlData.html");
    string htmltext = sr.ReadToEnd();
    sr.Close();

    // Execute mail merge
    doc.MailMerge.Execute(new string[] { "htmlField1" }, new object[] { htmltext });

    // Save resulting document with a new name
    doc.Save(ArtifactsDir + "MailMerge.InsertHtml.doc");
}

private class HandleMergeFieldInsertHtml : IFieldMergingCallback
{
    /// <summary>
    /// This is called when merge field is actually merged with data in the document.
    /// </summary>
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
    {
        // All merge fields that expect HTML data should be marked with some prefix, e.g. 'html'
        if (args.DocumentFieldName.StartsWith("html") && args.Field.GetFieldCode().Contains("\\b"))
        {
            FieldMergeField field = args.Field;

            // Insert the text for this merge field as HTML data, using DocumentBuilder
            DocumentBuilder builder = new DocumentBuilder(args.Document);
            builder.MoveToMergeField(args.DocumentFieldName);
            builder.Write(field.TextBefore);
            builder.InsertHtml((string) args.FieldValue);

            // The HTML text itself should not be inserted
            // We have already inserted it as an HTML
            args.Text = "";
        }
    }

    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
    {
        // Do nothing
    }
}
See Also