We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Mail merge with regions creates blank page at the end of document

I have word template with nested mail merge regions. I use this link to create template.

Basically the template has two regions corresponds to 2 tables. The Table1 (outer table/outer region) and Table2 (Nested Table/Nested Region). Please see the attached template
When i execute with mail merge region, the created document has continuous flow without any page break after Table1 row.

I would like to have each row from Table1 start on new page. So as per the suggestion Controlling New Pages when Using Mail Merge with Regions i added a page break at the end of the outer mail merge region. ( just before «TableEnd:Table1»). That moves the «TableEnd:Table1» on new page in the template.

So now each row from Table1 starts on new page as expected However the document always creates extra blank page at the end. How do i solve this issue
Template.zip (22.0 KB)

@Laksh,

Thanks for your inquiry. You can also achieve your requirement by setting “page break before” of first paragraph in the region. Please check the attached image for detail. page break before.png (5.1 KB)

Please use following steps to specify a “page break before” a paragraph.

  • Click the paragraph that you want to follow the page break.
  • On the Page Layout tab, click the Paragraph Dialog Box Launcher, and then click the Line and Page Breaks tab.
  • Select the Page break before check box.

If you want to insert page break at the end of the mail merge region, you can remove last empty page of document using following code example.

// mail merge code...
while (string.IsNullOrEmpty(document.LastSection.Body.LastParagraph.ToString(SaveFormat.Text).Trim()))
{

    int childnodes = document.LastSection.Body.LastParagraph.ChildNodes.Cast<Node>().Where(child => child.NodeType != NodeType.Run).ToList<Node>().Count;
    if (childnodes > 0)
        break;
    else
        document.LastSection.Body.LastParagraph.Remove();

}

document.Save(MyDir + "17.10.docx");

Thanks
It worked when Table1 has more than 1 row. However if Table1 has 1 row then it still creates Blank Page.

@Laksh,

Thanks for your inquiry. We have tested the scenario using latest version of Aspose.Words for .NET 17.10 with following code example. We have not found the shared issue.

Document doc = new Document(MyDir + "Template.docx");

DataTable table = new DataTable("table1");
table.Columns.Add("Name", typeof(string));

var newRow = table.NewRow();
newRow["Name"] = "Jhon";
table.Rows.Add(newRow);
                
DataTable table2 = new DataTable("table2");
table2.Columns.Add("AccountNumber", typeof(string));

var newRow2 = table2.NewRow();
newRow2["AccountNumber"] = "33324234";
table2.Rows.Add(newRow2);

DataSet ds = new DataSet();
ds.Tables.Add(table);
ds.Tables.Add(table2);

doc.MailMerge.CleanupOptions =
    MailMergeCleanupOptions.RemoveContainingFields |
    MailMergeCleanupOptions.RemoveEmptyParagraphs |
    MailMergeCleanupOptions.RemoveUnusedFields |
    MailMergeCleanupOptions.RemoveUnusedRegions |
    MailMergeCleanupOptions.RemoveStaticFields;

doc.MailMerge.ExecuteWithRegions(ds);


while (string.IsNullOrEmpty(doc.LastSection.Body.LastParagraph.ToString(SaveFormat.Text).Trim()))
{

    int childnodes = doc.LastSection.Body.LastParagraph.ChildNodes.Cast<Node>().Where(child => child.NodeType != NodeType.Run).ToList<Node>().Count;
    if (childnodes > 0)
        break;
    else
        doc.LastSection.Body.LastParagraph.Remove();
}

doc.Save(MyDir + "17.10.docx");

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document. If you are using the same “Template.docx”, no need to share it again.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.