Search code examples
c#fileiodocxmailmerge

MailMerge TaleStart-TableEnd: Add enter on end of page with multiline rows


We have a MailMerge docx which has the following table:

_____________________________________________________________________________
Date         Id        Description                                Amount
_____________________________________________________________________________
{{TableStart {{Id}}    {{Description}}                            € {{Amount 
:Lines}}{{Da                                                      \# 0,00}}{{
te \@"dd-MM-                                                      TableEnd:Li
yyyy"}}                                                           nes}}
_____________________________________________________________________________
                                                        Total    € {{Total \#
                                                                 0,00}}
_____________________________________________________________________________

Here is an example result row:

____________________________________________________________________________
Date         Id        Description                                Amount
____________________________________________________________________________
03-09-2015   0001      Company Name                               € 25,00
                       Buyer Name 1, Buyer Name 2
                       Product description
                       Extra description line

As you can see, the description has multiple lines. When the end of a page is reached, it just continues on the next page. So with the example above, the line could be like this at the end of page 1:

03-09-2015   0001      Company Name                               € 25,00
                       Buyer Name 1, Buyer Name 2

And like this at the start of page 2:

                       Product description
                       Extra description line

What I'd like instead is the following: When an item doesn't fit on the page anymore, the entire item must go to the start of the next page. Basically I want to prevent items from splitting between pages. Is there any way to accomplish this with MailMerge?

Also, we use C# in our project. Here is the code we use for the MailMerge. I think it's a bit to ambitious to ask if there is a setting to allow the behavior I desire in the MailMerge libraries. Anyway, here is the code we use to convert the data & docx to a pdf:

var pdf = _documentService.CreateTableFile(new TableFileData(date, companyId, 
    dataList.Select(x => new TableRowData
    {
        Description = x.Description,
        Amount = x.Amount,
        Date = x.Date,
        Id = x.Id
    }).ToList()));

var path = Path.Combine(FileService.GetTemporaryPath(), Path.GetRandomFileName());
var file = Path.ChangeExtension(path, "pdf");

using (var fs = File.OpenWrite(file))
{
    fs.Write(pdf, 0, pdf.Length);
}

Process.Start(file);

With CreateTableFile-method:

public byte[] CreateTableFile(TableFileData data)
{
    if (data == null) throw new ArgumentNullException("data");

    const string fileName = "TableFile.docx";
    var path = Path.Combine(_templatePath, fileName);

    using (var fs = File.OpenRead(path))
    {
        var dataSource = new DocumentDataSource(data);
        return GenerateDocument(fs, dataSource);
    }
}

With GenerateDocument-method:

private static byte[] GenerateDocument(Stream template, DocumentDataSource dataSource, IFieldMergingCallback fieldMergingCallback = null)
{
    var doc = new Document(template);

    doc.MailMerge.FieldMergingCallback = fieldMergingCallback;
    doc.MailMerge.UseNonMergeFields = true;
    doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveContainingFields |
        MailMergeCleanupOptions.RemoveUnusedFields |
        MailMergeCleanupOptions.RemoveUnusedRegions |
        MailMergeCleanupOptions.RemoveEmptyParagraphs;

    doc.MailMerge.Execute(dataSource);
    doc.MailMerge.ExecuteWithRegions((IMailMergeDataSourceRoot)dataSource);

    doc.UpdateFields();

    using (var ms = new MemoryStream())
    {
        var options = new PdfSaveOptions { WarningCallback = new AsposeWarningCallback() };
        doc.Save(ms, options);
        return ms.ToArray();
    }
}

Solution

  • After @bibadia's suggestion in the first comment of the question, I've unchecked the suggested checkbox of the table settings in the docx: enter image description here

    This did the trick, so thanks a lot bibadia!