Articles in this section
Category / Section

Generate ID Card in Multi-Column Page by Mail Merge in ASP.NET Word.

5 mins read

Syncfusion® Essential DocIO is a ASP.NET Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can generate ID cards in a multi-column page using mail merge in a Word document using C#.

To achieve this, design a template document with a two-column layout. Then, perform mail merge using DocIO to populate the content while maintaining the column structure.

Steps to generate ID cards in a multi-column page using mail merge in a Word document:

  1. Create a new .NET Core console application project. Create console application in Visual Studio
  2. Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your project from NuGet.org.
    Add DocIO NuGet package reference to the project

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without trail message.

  1. Include the following namespaces in Program.cs file
    C#
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO; 
  1. Use the following code example to generate ID cards in a multi-column page using mail merge in a Word document.
    C#
using (FileStream fileStream = new FileStream(Path.GetFullPath("Template.docx"), FileMode.Open, FileAccess.ReadWrite))
{
   //Loads an existing Word document into DocIO instance.
   using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic))
   {
       //Gets the employee details as IEnumerable collection.
       List<Employee> employeeList = GetEmployees();
       //Creates an instance of MailMergeDataTable by specifying MailMerge group name and IEnumerable collection.
       MailMergeDataTable dataSource = new MailMergeDataTable("Employees", employeeList);
       //Uses the mail merge events handler for image fields.
       document.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MergeField_EmployeeImage);
       //Performs Mail merge.
       document.MailMerge.ExecuteGroup(dataSource);
       //Creates file stream.
       using (FileStream outputStream = new FileStream(Path.GetFullPath("Result.docx"), FileMode.Create, FileAccess.ReadWrite))
       {
           //Saves the Word document to file stream.
           document.Save(outputStream, FormatType.Docx);
       }
   }
}
  1. Use the following code example to represents the method that handles MergeImageField event.
    C#
private static void MergeField_EmployeeImage(object sender, MergeImageFieldEventArgs args)
{
   //Binds image from file system during mail merge.
   if (args.FieldName == "Photo")
   {
       string photoFileName = args.FieldValue.ToString();
       //Gets the image from file system.
       FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/" + photoFileName), FileMode.Open, FileAccess.Read);
       args.ImageStream = imageStream;
       //Gets the picture, to be merged for image merge field
       WPicture picture = args.Picture;
       //Resizes the picture
       picture.Height = 90;
       picture.Width = 90;
   }
}

  1. Use the following code example to get the employee details to perform mail merge.
    C#
public static List<Employee> GetEmployees()
{
   List<Employee> employees = new List<Employee>();
   employees.Add(new Employee("Nancy", "Smith", "Sales Representative", "505 - 20th Ave. E. Apt. 2A,", "Seattle", "WA", "USA", "Nancy.png"));
   employees.Add(new Employee("Andrew", "Fuller", "Vice President, Sales", "908 W. Capital Way", "Tacoma", "WA", "USA", "Andrew.png"));
   employees.Add(new Employee("Roland", "Mendel", "Sales Representative", "722 Moss Bay Blvd.", "Kirkland", "WA", "USA", "Janet.png"));
   employees.Add(new Employee("Margaret", "Peacock", "Sales Representative", "4110 Old Redmond Rd.", "Redmond", "WA", "USA", "Margaret.png"));
   employees.Add(new Employee("Steven", "Buchanan", "Sales Manager", "14 Garrett Hill", "London", string.Empty, "UK", "Steven.png"));
   return employees;
} 
  1. Use the following code example to represents a class to maintain employee details.
    C#
/// <summary>
/// Represents a class to maintain employee details.
/// </summary>
public class Employee
{
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public string Address { get; set; }
   public string City { get; set; }
   public string Region { get; set; }
   public string Country { get; set; }
   public string Title { get; set; }
   public string Photo { get; set; }
   public Employee(string firstName, string lastName, string title, string address, string city, string region, string country, string photoFilePath)
   {
       FirstName = firstName;
       LastName = lastName;
       Title = title;
       Address = address;
       City = city;
       Region = region;
       Country = country;
       Photo = photoFilePath;
   }
} 

You can download a complete working sample to generate ID cards in a multi-column page using mail merge from the GitHub.

Output Word document

Take a moment to peruse the documentation where you can find basic Word document processing options along with the features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.

Conclusion

I hope you enjoyed learning about how to generate ID card in multi-column page by mail merge in ASP.NET Word.

You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core DocIO Example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied