FieldMerging

IFieldMergingCallback.FieldMerging method

Called when the Aspose.Words mail merge engine is about to insert data into a merge field in the document.

public void FieldMerging(FieldMergingArgs args)

Examples

Shows how to execute a mail merge with a custom callback that handles merge data in the form of HTML documents.

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

builder.InsertField(@"MERGEFIELD  html_Title  \b Content");
builder.InsertField(@"MERGEFIELD  html_Body  \b Content");

object[] mergeData =
{
    "<html>" +
        "<h1>" +
            "<span style=\"color: #0000ff; font-family: Arial;\">Hello World!</span>" +
        "</h1>" +
    "</html>", 

    "<html>" +
        "<blockquote>" +
            "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>" +
        "</blockquote>" +
    "</html>"
};

doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertHtml();
doc.MailMerge.Execute(new[] { "html_Title", "html_Body" }, mergeData);

doc.Save(ArtifactsDir + "MailMergeEvent.MergeHtml.docx");

Shows how to execute a mail merge with a custom callback that handles merge data in the form of HTML documents (HandleMergeFieldInsertHtml).

/// <summary>
/// If the mail merge encounters a MERGEFIELD whose name starts with the "html_" prefix,
/// this callback parses its merge data as HTML content and adds the result to the document location of the MERGEFIELD.
/// </summary>
private class HandleMergeFieldInsertHtml : IFieldMergingCallback
{
    /// <summary>
    /// Called when a mail merge merges data into a MERGEFIELD.
    /// </summary>
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
    {
        if (args.DocumentFieldName.StartsWith("html_") && args.Field.GetFieldCode().Contains("\\b"))
        {
            // Add parsed HTML data to the document's body.
            DocumentBuilder builder = new DocumentBuilder(args.Document);
            builder.MoveToMergeField(args.DocumentFieldName);
            builder.InsertHtml((string)args.FieldValue);

            // Since we have already inserted the merged content manually,
            // we will not need to respond to this event by returning content via the "Text" property. 
            args.Text = string.Empty;
        }
    }

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

Shows how to insert images stored in a database BLOB field into a report.

Document doc = new Document(MyDir + "Mail merge destination - Northwind employees.docx");

doc.MailMerge.FieldMergingCallback = new HandleMergeImageFieldFromBlob();

string connString = $"Data Source={DatabaseDir + "Northwind.db"}";
string query = "SELECT FirstName, LastName, Title, Address, City, Region, Country, PhotoBLOB FROM Employees";

using (SqliteConnection conn = new SqliteConnection(connString))
{
    conn.Open();

    // Open the data reader, which needs to be in a mode that reads all records at once.
    SqliteCommand cmd = new SqliteCommand(query, conn);
    IDataReader dataReader = cmd.ExecuteReader();

    doc.MailMerge.ExecuteWithRegions(dataReader, "Employees");
}

doc.Save(ArtifactsDir + "MailMergeEvent.ImageFromBlob.docx");

Shows how to insert images stored in a database BLOB field into a report (HandleMergeImageFieldFromBlob).

private class HandleMergeImageFieldFromBlob : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
    {
        // Do nothing.
    }

    /// <summary>
    /// This is called when a mail merge encounters a MERGEFIELD in the document with an "Image:" tag in its name.
    /// </summary>
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        MemoryStream imageStream = new MemoryStream((byte[])e.FieldValue);
        e.ImageStream = imageStream;
    }
}

See Also