MailMerge with Regions - add cells horizontally in a sub-table

Dear Aspose Team

I have defined a Template (see Template.PNG) which creates a table for each project in the list. This table contains basic information about a project. This works as expected. But now I would like to create a sub-table containing the milestone of this project. Each row should contain four cells containing the milestone name. If there are more than four milestones, the next four should be in a new row (see ExpectedResult.PNG).

I have defined the following models

public class myProjectList
{
    public string Title { get; set; }
    public string PL { get; set; }
    public string TotalBudget { get; set; }
    public List Milestones { get; set; }
}
public class myMilestone
{
    public string MilestoneName { get; set; }
}

And I am using the following code:

public Document getAgenda(string agendaTemplateFileName)
{
    Document doc = new Document(agendaTemplateFileName);
    List projects = new List();
    for (int i = 0; i < 2; i++)
    {
        List milestones = new List();
        for (int i = 0; i < 8; i++)
        {
            milestones.Add(new myMilestone()
            {
                MilestoneName = "M" + i + "00"
            });
        }
        projects.Add(new myProjectList()
        {
            Title = "CustomerProject " + i,
            Milestones = milestones
        });
    }
    doc.MailMerge.ExecuteWithRegions(new MailMergeDataSource(projects, "ProjectList"));
    return doc;
}

I think I need to do something like the following, but I don’t know exactly where to place it and how to achieve that

DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToMergeField("MilestoneName");
builder.StartTable();
int i = 0;
foreach (myMilestone ms in milestones)
{
    builder.InsertCell();
    builder.Write(ms.MilestoneName);
    if (i % 4 == 0)
    {
        builder.EndRow;
    }
    i++;
}
builder.EndTable();

It would be great If someone could help me to get this work. If you need any additional information, let me know

Thanks in advance

Hi Lukas,

Thanks for your inquiry. In you case, I suggest you please do not use mail merge with regions for Milestones. Please use only simple mail merge field <> in your template and implement IFieldMergingCallback interface as shown in following code snippet. You need to move cursor to MilestoneName field and write code to create table inside FieldMerging method. Hope this helps you.

public class HandleMergeField : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
    {
        if (args.FieldName == "MilestoneName")
        {
            DocumentBuilder builder = new DocumentBuilder(args.Document);
            builder.MoveToMergeField("MilestoneName");
            // We call this method to start building the table.
            builder.StartTable();
            // Your code....
            builder.EndTable();
        }
    }
    /// 
    /// This is called when mail merge engine encounters Image:XXX merge field in the document.
    /// You have a chance to return an Image object, file name or a stream that contains the image.
    /// 
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
    }
}
Document doc = new Document(MyDir + "in.docx");
doc.MailMerge.FieldMergingCallback = new HandleMergeField();
doc.MailMerge.ExecuteWithRegions(your data source….);
doc.Save(MyDir + "Out.docx");

Please read following documentation links for your kind reference.
https://reference.aspose.com/words/net/aspose.words.mailmerging/ifieldmergingcallback/