We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to have one source item per page with MailMerge ?

Hello,

I have a Word template and I am using it with MailMerge and several sources. I would like one page per source.
I have manage to do that by adding a page break at the end of the template but I always have an extra blank page at the end of the document.

Could you tell me how I can remove this page or if you have another solution to have a source item per page ?

Thanks

3 Replies

PE Priyanga Elangovan Syncfusion Team November 1, 2016 01:10 PM UTC

Hi Delphine,

Thank you for contacting Syncfusion support.

Based on the given details, we have assumed that your requirement is to perform mail merge with various data source(records) and maintain each record in each page.

We suspect that you have added the pagebreak at the end of the template document. Due to this reason the extra page is added at the end of resultant document. So please remove the pagebreak from the template document.

You can utilize the mail merge event functionality of DocIO to insert the page break at the end of merge fields during mail merge to maintain each record in each page. Please skip to insert the page break at the time of last record binding to remove the extra page at end of the resultant document.

Kindly refer the following UG documentation link to know more details about mail merge event
https://help.syncfusion.com/file-formats/docio/working-with-mailmerge#event-support-for-mail-merge

Please find the template document and code snippet from below.
Template Document:
http://www.syncfusion.com/downloads/support/forum/127171/doc/Template-107945538

Code snippet:
 
//Loads the template document 
WordDocument document = new WordDocument(@"Template.docx"); 
//Gets the employee details as “IEnumerable” collection  
List<Employee> employeeList = GetEmployee(); 
//Creates an instance of “MailMergeDataTable” by specifying mail merge group name and “IEnumerable” collection. 
MailMergeDataTable dataTable = new MailMergeDataTable("Employee", employeeList); 
//Performs the mail merge event to insert the page break 
document.MailMerge.MergeField += new MergeFieldEventHandler(MergeField_InsertPageBreak); 
//Performs Mail merge 
document.MailMerge.ExecuteGroup(dataTable); 
//Saves and closes the Word document instance 
document.Save("Result.docx"); 
document.Close(); 
 
//Sets the employee details 
public static List<Employee> GetEmployee() 
{ 
List<Employee> employees = new List<Employee>()
employees.Add(
new Employee("Nancy", "Sales Representative", "Tacoma")); 
employees.Add(new Employee("Andrew", "Sales Representative", "Tacoma")); 
employees.Add(new Employee("Janet", "Sales Representative", "Tacoma")); 
employees.Add(new Employee("Margaret", "Sales Representative", "Tacoma")); 
employees.Add(new Employee("Steven", "Sales Representative", "Tacoma")); 
return employees; 
} 
} 
//Creates the employee class. 
public class Employee 
{ 
public string FirstName { get; set; } 
public string Address { get; set; } 
public string Title { get; set; } 
public Employee(string firstName, string title, string address) 
{ 
FirstName = firstName; 
Title = title; 
Address = address; 
} 
} 
//Declare this variable in main class 
static int i = 1; 
static List<Employee> employeelist = GetEmployee(); 
 
//Creates the mail merge event to insert the page break 
private static void MergeField_InsertPageBreak(object sender, MergeFieldEventArgs args){ 
{ 
if (args.FieldName == "Address" && i != employeelist.Count) 
{ 
//Gets the owner paragraph 
WParagraph paragraph = args.CurrentMergeField.OwnerParagraph; 
//Appends the page break 
paragraph.AppendBreak(BreakType.PageBreak); 
i++; 
} 
} 

If we misunderstood any of your requirement, then Kindly revert to us with the code snippet/sample along with the input template document which you have used to reproduce the mentioned issues and your working scenario. Thereby we will analyze further on the mentioned case and will provided you appropriate solution.

Regards,
Priyanga.E
 
 



DL Delphine Lacour November 2, 2016 10:28 AM UTC

Hi,

your perfectly understood what I wanted to do. 
Thanks for the solution.


VR Vijay Ramachandran Syncfusion Team November 3, 2016 05:16 AM UTC

Hi Delphine,

Thank you for your update.

We are happy to hear that the provide solution solved your problem. Please let us know if you have any other questions. We will be happy to assist you as always.

Regards,
Vijay R
 


Loader.
Live Chat Icon For mobile
Up arrow icon