Car rental contract - Simple WinForms C# App

This C# app shows how to generate documents populated by data based on template (*.docx).


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.

This C# App shows how to create 5 populated by data Docx documents from a Word template (*.docx)

Two main steps:

  1. Create templates in MS Word or use the ready «car-rental-template.docx» and «insurance-template.docx».
  2. Create Winform C# app which allows to enter a data and generate populated documents by executing the mail merge process.

1. Create templates in MS Word with Merge Fields

In this step we have to create two templates.

  1. To start, open MS Word and create new documents named «car-rental-template.docx» and «insurance-template.docx».

  2. Add headings "Car Rental Contacts" and "Insurance Policy" correspondly to the document.

  3. Into "Car Rental Contacts" add Merge Fields and name they so:
    Day, Month, Year, RenterName, RenterAddress, RenterPhone, CarModel, CarVIN, CarMilleage and Period.

  4. 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»:


2. Create Winform C# app which allows to enter a data and generate populated documents

  1. Launch Visual Studio and Create new WinForms C# Application (.Net Framework) with name "CarRental".
    Create new WinForms C# Application (.Net Framework) with name CarRental
  2. Add a reference to SautinSoft.Document assembly.
    You may do this by two ways:
    1. Using Nuget (fast way):
    (Solution Explorer->right click by "References"->Manage Nuget Packages...->In the tab "Browse" type "SautinSoft" and find the "SautinSoft.Document")
    Add SautinSoft.Document.dll via Nuget.

    2. Second (old good way): Adding a reference to the "SautinSoft.Document.dll" file:

    First of all, download the Document .Net package. Unzip it at any place in your computer and find the assembly file "SautinSoft.Document.dll".
    (Solution Explorer->right click by "References"->Add Reference...->Browse "SautinSoft.Document.dll")
    Add reference to SautinSoft.Document.dll.
  3. Increase the form size and add TextBox control with name "tbRenterName" as shown on the picture below:
    Add TextBox control with name tbRenterName.
  4. Add other controls to make our Form the same as in the picture:
    Nine (9) TextBoxes: tbRenterAddress, tbRenterPhone, tbCarModel, tbCarVIN, tbCarMileage, tbPeriod, tbDay, tbMonth, tbYear.
    Two (2) CheckBoxes: chkCarContract and chkInsurance.
    One (1) ComboBox: comboFormat.
    One (1) Button: btnRun.
    Add TextBox control with name tbRenterName.

    Here you may find the ready CarRental App.

  5. 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()
                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.
                    // 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.
                    // Open the ready document for demonstration purposes.
  6. 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:

    Execution of the App.

    As result we get: «CarRentalContract-Homer Simpson.pdf» and «InsurancePolicy-Homer Simpson.pdf»:

    Car Rental contract in PDF.
    Insurance policy in PDF.
© SautinSoft 2018