Mail Merge is the feature which allows to easy generate documents populated by data using a template.
To illustrate how works Mail Merge function, let's create a WinForms C# application which allows to enter data and generates "car rental contract" and "insurance policy" populated by this data.
Thus, our application will have a form to enter a data: RenterName, OwnerName, Date etc.
Furthermore,
we'll prepare two templates "car-rental-template.docx" and "insurance-template.docx". The app will
populate templates by data and generate ready documents in PDF format.
The advantage of this approach is that you enter the data only a one time and get multiplicity various populated documents.
In this step we have to create two templates.
Into "Car Rental Contacts" add Merge Fields and name they so:
Day, Month, Year, RenterName, RenterAddress, RenterPhone, CarModel, CarVIN, CarMilleage
and Period.
Into "Insurance Policy" add Merge Fields and name they so:
Day, Month, Year, RenterName, RenterPhone, CarModel
and Period.
If you are already familiar with adding of Merge Fields, you may skip this step and use completely ready templates «car-rental-template.docx» and «insurance-template.docx»
If you are novice in this theme and need help, see How to Insert Merge Fields
As result we get: «car-rental-template.docx» and «insurance-template.docx»:
You may add the reference to the SautinSoft.Document assembly by two ways:
1. Nuget (fast way):
2. Old good way by adding the reference:
Note:
SautinSoft.Document.dll compiled for .NET Core is located inside (document_net.zip->Document .Net X.X\Bin\.NET Core X.X) folder.
SautinSoft.Document.dll compiled for .NET Framework is located inside (document_net.zip->Document .Net X.X\Bin\.NET Framework X.X) folder.
Here you may find the ready CarRental App
Here is the full code of our CarRental App:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Globalization;
using SautinSoft.Document;
namespace CarRental
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
comboFormat.SelectedIndex = 0;
// Some Bonus, autocomplite date
// Year
tbYear.Text = DateTime.Now.Year.ToString();
// Month
CultureInfo ci = new CultureInfo("en-US");
tbMonth.Text = ci.DateTimeFormat.GetMonthName(DateTime.Now.Month);
// Day
tbDay.Text = DateTime.Now.Day.ToString();
}
private void btnRun_Click(object sender, EventArgs e)
{
// Templates <Template Name, Path to a template file>.
Dictionary<string, string> templateCollection = new Dictionary<string, string>();
if (chkCarContract.Checked)
templateCollection.Add("CarRentalContract", @"d:\car-rental-template.docx");
if (chkInsurance.Checked)
templateCollection.Add("InsurancePolicy", @"d:\insurance-template.docx");
// Create dataSource
var dataSource = new
{
RenterName = tbRenterName.Text,
RenterAddress = tbRenterAddress.Text,
RenterPhone = tbRenterPhone.Text,
CarModel = tbCarModel.Text,
CarVIN = tbCarVIN.Text,
CarMileage = tbCarMileage.Text,
Period = tbPeriod.Text,
Day = tbDay.Text,
Month = tbMonth.Text,
Year = tbYear.Text
};
foreach (KeyValuePair<string, string> template in templateCollection)
{
// template.Value - contains a path to template file.
// For example, "d:\car-rental-template.docx"
DocumentCore dc = DocumentCore.Load(template.Value);
// Do Mail Merge
// Import data to the template.
dc.MailMerge.Execute(dataSource);
// Save the ready document
// Specify extension for the ready document
string ext = comboFormat.Text;
// template.Key - contains a name of our ready document
// For example, if RenterName is "John", as result we get: "CarRentalContract-John.pdf".
string readyDocPath = String.Format("{0}-{1}.{2}", template.Key, dataSource.RenterName, ext);
// The file format is detected automatically from the file extension.
dc.Save(readyDocPath);
// Open the ready document for demonstration purposes.
System.Diagnostics.Process.Start(readyDocPath);
}
}
}
}
Let us say, Homer Simpson has broken his car again and need to rent a car. After execution of the App with such data, you will get similar results:
As result we get: «CarRentalContract-Homer Simpson.pdf» and «InsurancePolicy-Homer Simpson.pdf»: