Articles in this section
Category / Section

Is it possible to merge outer group fields placed within inner nested group using nested mail merge?

4 mins read

Yes, it is possible to merge outer group fields placed within the inner nested group using the nested mail merge in C# by utilizing the mail merge functionality of Syncfusion Essential DocIO.

The Syncfusion Essential DocIO is a .NET Core Word library used to generate reports like invoice, payroll, letter, and more, by performing a mail merge faster in a batch process without Microsoft Word or interop dependencies.

How to design input Word document template

Merge any field in the nested group by mapping the domain or column of its ancestor group or table in the data source. To achieve this, add a corresponding group name or table name as a prefix to the merge field name and the “:” separator.

For example, the attached template’s fields are designed as follows.

  • The merge field name should be like “GroupName:Id” («GroupName:MergeFieldName»)
  • The merge field name should be like “Image:GroupName:Photo” («Image:GroupName:MergeFieldName»)
  • The merge field name should be like “TableName:Id” («TableName:MergeFieldName»)
  • The merge field name should be like “Image:TableName:Photo” («Image:TableName:MergeFieldName») 

Input Word document in ASP.NET Core DocIO

In the above template, the outer group merge fields are highlighted within the inner nested group of a Word document.

Steps to merge the outer group fields placed within the inner nested group using the nested mail merge

  1. Create a new C# .NET Core console application project. Create .NET Core console application in Visual Studio in ASP.NET Core Word
  2. Install the Syncfusion.DocIO.Net.Core NuGet package to reference your .NET Core applications from NuGet.org. Add DocIO.Net.Core NuGet packages of ASP.NET Core Word
  3. Include the following namespace in the Program.cs file.

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
  1. Use the following code example to merge outer group fields placed within the inner nested group using the nested mail merge.

C#

using (FileStream fileStream = new FileStream(Path.GetFullPath(@"../../../Template.docx"), FileMode.Open, FileAccess.ReadWrite))
{
    //Open the template Word document.
    using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic))
    {
        //Create the commands which contain the queries to get the data from the dataset.
        ArrayList commands = new ArrayList();
        commands.Add(new DictionaryEntry("Bills", ""));
        commands.Add(new DictionaryEntry("ProductDetails", "BillId = %Bills.BillId%"));
        commands.Add(new DictionaryEntry("Price", "DetailID = %ProductDetails.DetailID%"));
        //Create the data set that contains data to perform the mail merge.
        DataSet dataSet = GetDataSet();
        //Remove groups that contain empty merge fields. 
        document.MailMerge.RemoveEmptyGroup = true;
        //Remove paragraphs that contain empty merge fields. 
        document.MailMerge.RemoveEmptyParagraphs = true;
        //Execute the nested mail merge.
        document.MailMerge.ExecuteNestedGroup(dataSet, commands); 
        //Create a file stream.
        using (FileStream outputStream = new FileStream(Path.GetFullPath(@"../../../Result.docx"), FileMode.Create, FileAccess.ReadWrite))
        {
            //Save a Word document to the file stream.
            document.Save(outputStream, FormatType.Docx);
        }
    }
}
  1. Use the following helper method to get data set details to perform a mail merge in a Word document.

C#

/// <summary>
/// Create a data set to perform the mail merge.
/// </summary>
private static DataSet GetDataSet()
{
    DataSet ds = new DataSet();
    DataTable table = new DataTable("Bills");
    table.Columns.Add("BillID");
    table.Columns.Add("ControlNumber");
    table.Columns.Add("RecipientId");
    table.Columns.Add("Picture", typeof(byte[]));
    DataRow row = table.NewRow();
    table.Rows.Add(row);
    row["BillID"] = "BL7936";
    row["ControlNumber"] = "CN100";
    row["RecipientId"] = "900893674";
    FileStream fs = new FileStream((@"../../../Data/Mountain-300.png"), FileMode.Open, FileAccess.Read);
    byte[] buff = new byte[fs.Length];
    fs.Read(buff, 0, buff.Length);
    fs.Dispose();
    row["Picture"] = buff;
    ds.Tables.Add(table);
 
    table = new DataTable("ProductDetails");
    table.Columns.Add("BillID");
    table.Columns.Add("DetailID");
    table.Columns.Add("ControlNumber");
    table.Columns.Add("ProductAmount");
    table.Columns.Add("Picture", typeof(byte[]));
    row = table.NewRow();
    table.Rows.Add(row);
    row["BillID"] = "BL7936";
    row["DetailID"] = "6758671";
    row["ControlNumber"] = "CN110";
    row["ProductAmount"] = "1500";
    fs = new FileStream((@"../../../Data/Road-550-W.png"), FileMode.Open, FileAccess.ReadWrite);
    buff = new byte[fs.Length];
    fs.Read(buff, 0, buff.Length);
    fs.Dispose();
    row["Picture"] = buff;
    ds.Tables.Add(table);
 
    table = new DataTable("Price");
    table.Columns.Add("DetailID");
    table.Columns.Add("ControlNumber");
    table.Columns.Add("DiscountAmount");
    table.Columns.Add("Picture", typeof(byte[]));
    row = table.NewRow();
    table.Rows.Add(row);
    row["DetailID"] = "6758671";
    row["ControlNumber"] = "CN111";
    row["DiscountAmount"] = "500";
    fs = new FileStream((@"../../../Data/Mountain-200.png"), FileMode.Open, FileAccess.ReadWrite);
    buff = new byte[fs.Length];
    fs.Read(buff, 0, buff.Length);
    fs.Dispose();
    row["Picture"] = buff;
    ds.Tables.Add(table);
    return ds;
}

A complete working sample to merge the outer group fields placed within the inner nested group using the nested mail merge in a Word document using C# can be downloaded from GitHub.

By executing the program, you will get the output document as follows.

Output Word document in ASP.NET Core DocIO

Take a moment to peruse the documentation where you can find basic Word document processing options along with the features like mail mergemerge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, a PDF and Image conversions with code examples.

Explore more about the rich set of Syncfusion Word Framework features.

See Also:

How to replace merge field with HTML string using Mail merge

How to perform the mail merge in a Word document using an image from a URL

Conclusion

I hope you enjoyed learning about if it is possible to merge outer group fields placed within inner nested group using nested mail merge.

You can refer to our ASP.NET Core DocIO’s feature tour page to know about its other groundbreaking feature representations. You can also explore our ASP.NET Core DocIO documentation to understand how to present and manipulate data.

For current customers, you can check out our ASP.NET Core components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our ASP.NET Core DocIO and other ASP.NET Core components.

If you have any queries or require clarifications, please let us know in the comment section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied