Aspose.Words.dll - Execute region - no. of rows in a page

Hi Team,
We are using “aspose.words”>>“Execute region” functionality in one of our project. We need to fix number of records in a page. Is there any function supported for this issue?
Best Regards,
Manish Misra

This message was posted using Email2Forum by ShL77.

Hi

Thanks for your inquiry. Unfortunately, there is no direct way to achieve this. However, it is possible to achieve using FieldMerging callback.
Attached is simple template and here is the code:

[Test]
public void Test001()
{
    // Get data. 
    // Just to demostrate the technique I generate data manually.
    DataTable data = new DataTable("data");
    data.Columns.Add("id");
    data.Columns.Add("Col1");
    data.Columns.Add("Col2");
    for (int i = 0; i < 100; i++)
        data.Rows.Add(new object[] { i, string.Format("dummy data {0}", i), string.Format("second column {0}", i) });
    // Open template.
    Document doc = new Document(@"Test001\in.doc");
    // Add MergeField callback.
    doc.MailMerge.FieldMergingCallback = new HandleMergeField(10);
    // Execute mail merge with regions.
    doc.MailMerge.ExecuteWithRegions(data);
    // Save output.
    doc.Save(@"Test001\out.doc");
}
private class HandleMergeField : IFieldMergingCallback
{
    public HandleMergeField(int recordsPerPage)
    {
        mRecordsPerPage = recordsPerPage;
    }
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
    {
        // Use once of fields in row as indicator of row start.
        // For example lets using "id" field.
        if (args.FieldName == "id")
        {
            mRecordIndex++;
            if (mRecordIndex > mRecordsPerPage)
            {
                // Reset index or row.
                mRecordIndex = 0;
                // Move to the paragraph where the mergefied is located and set PageBreakBefore property.
                DocumentBuilder builder = new DocumentBuilder(args.Document);
                builder.MoveToField(args.Field, false);
                builder.CurrentParagraph.ParagraphFormat.PageBreakBefore = true;
            }
        }
    }
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        // Do nothing.
    }
    private int mRecordIndex;
    private readonly int mRecordsPerPage;
}

Hope this helps.
Best regards.

Thanks a lot for your help.
It works for us.

Hi Team,
I am using Execute With Region functionality of Aspose.Word. Suppose my DataTable contains 3 rows 3 Columns.
I want to render all DataTable values as MergeFields not as Text which can be used for further process.
How can I acheive this functionality? Please guide me.

Thanks,
Mandar Kavishwar

Hi Mandar,
Thanks for your request. If you would like to fill the document with data and then fill the same document with other data, it is impossible to achieve using mail merge. This is how mail merge works: during mail merge, merge fields are replaced with values. So in final document, merge fields are no longer available. In MS Word, this works exactly the same.
If you need to fill document with data several times, you can consider using bookmarks or form fields. Or you can create the template document and use this template each time when you need to perform mailmerge.
Best regards,