Include Proper Page Breaks in Mail Merged Word Document using C# | DOCX CSV Section Start New Page

When Aspose.Words merges a template that has its Layout Section Start set to “Continuous”, page breaks are not being inserted between each merged document. When Word is used to do the merge, page breaks are inserted.

I have attached a test case you can use to recreate the problem (I removed the Aspose.Words.20.3.0 package to reduce its size).TestCase14.zip (5.3 MB)

@dspector1,

Aspose.Words allows you to control page breaks in output document. It depends on your template document settings (Layout > Page Setup > Layout > Section Start). Before performing mail merge with Aspose.Words, you can change this setting using PageSetup.SectionStart property in Aspose.Words. Simple code is as follows:

Document doc = new Document("E:\\TestCase14\\MergeTest.docx");
doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;

var source = new SaCsvMailMergeSource("E:\\TestCase14\\test.csv");

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

doc.MailMerge.Execute(source);

OoxmlSaveOptions options = new OoxmlSaveOptions();
options.SaveFormat = SaveFormat.Docx;

doc.Save("E:\\TestCase14\\20.3.pdf");
doc.Save("E:\\TestCase14\\20.3.docx", options);

Hope, this helps.

I understand how to work around the issue, either using code like you proposed or by changing the Word template directly. That being said, my understanding is that Aspose merges should generate the same output as those done in Word, without needing to manipulate the Word template. Unless I am missing something, the current behavior of Aspose, while logical, is not correct.

@dspector1,

We have logged this problem in our issue tracking system. Your ticket number is WORDSNET-20126. We will further look into the details of this problem and will keep you updated on the status of the linked issue. Sorry for any inconvenience.

@dspector1,

Regarding WORDSNET-20126, we have completed the work on your issue and concluded that we would not be able to implement the fix of this issue. Your issue (WORDSNET-20126) has now been closed with ‘Won’t Fix’ resolution. Please see the following analysis details.

Even if MS Word generates mail merge output document with next page, odd page or even page breaks, we believe, Aspose.Words should not follow this behavior. Current Aspose.Words Mail Merge behavior is more flexible and allows customers to control output section breaks either with code or via template document and it allows customers to control output section breaks.

In particular, you may use the following code snippet:

switch (doc.FirstSection.PageSetup.SectionStart)
{
    case SectionStart.Continuous:
    case SectionStart.NewColumn:
        doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
        break;
}

Also, changing the current behavior will break backward compatibility for many of our other users.

I understand your reasoning, but feel that Word’s behavior is the “gold standard”, however flawed it might be. Perhaps a property should be introduced to enable “strict Word behavioral compliance”. It can default to “false” to avoid backward compatibility issues.

@dspector1,

Thanks for the additional information. We have logged these details in our issue tracking system. We will keep you posted here on any more updates.

@dspector1,

To address this problem, we have logged another ticket in our issue tracking system with ID WORDSNET-20480. You will be notified via this thread as soon as WORDSNET-20480 wiil be resolved in future. Sorry for any inconvenience.

@dspector1,

It is to update you that we have integrated the fix of WORDSNET-20480 in Aspose.Words for .NET 20.7 release. We have also added a new public property MailMerge.RetainFirstSectionStart in Aspose.Words 20.7 that you can use to get or set a value indicating whether the SectionStart of the first document section and its copies for subsequent data source rows are retained during mail merge or updated according to MS Word’s behavior. The following C# code demonstrates its usage:

Document document = new Document(path);
document.MailMerge.RetainFirstSectionStart = false;
document.MailMerge.Execute(dataSource);

The issues you have found earlier (filed as WORDSNET-20480) have been fixed in this Aspose.Words for .NET 20.7 update and this Aspose.Words for Java 20.7 update.