FieldMergingArgsBaseDocumentFieldName Property

Gets the name of the merge field as specified in the document.

Namespace:  Aspose.Words.MailMerging
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.7.0


public string DocumentFieldName { get; }

Property Value

Type: String


If you have a mapping from a document field name to a different data source field name, then this is the original field name as specified in the document.

If you specified a field name prefix, for example "Image:MyFieldName" in the document, then DocumentFieldName returns field name without the prefix, that is "MyFieldName".


Shows how to mail merge HTML data into a document.
public void InsertHtml()
    Document doc = new Document(MyDir + "Field sample - MERGEFIELD.docx");

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

    const string html = @"<html>
            <h1>Hello world!</h1>

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

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

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.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