Skip to main content Skip to footer

How to Create an Apartment Lease Word Document with GcWord Report Templates and C#

This blog is part one of a multi-part series highlighting various use cases for the new GcWord V4 API reporting template feature to automate the creation of new documents in specific industries. This edition will show users how to easily use GcWord Reporting Templates (similar to MS Word templates) to automate a rental industry process (in this case, student housing).

It's the end of June into early July, and the rental market is heating up. College is about to come back into session for the first time in nearly two years (thanks, COVID!). Traditionally, a client uses Word templates to create and distribute their leasing agreements, but the client quickly realizes this year is different. Usually, their inventory has a predictable turn-over, but this year and going forward, it appears as though the turn-over will be greater, and more importantly, condensed within a few-week period near the end of rental terms. As such, they have never considered having to automate their leasing agreements until now.

They have approached your company to help with a project driving data into Word based leasing agreements. Because of the volume, their existing Word templates have become cumbersome and difficult for them to connect with their new SQL-driven application (web and desktop-based for administration). They want an easy way to enter information for renters in a single location, then utilize a standard leasing agreement to produce Word documents their agents can then modify as needed before having tenants sign.

With the new GcWord V4 API tools utilizing C#, the client's desired outcome will be a snap to implement! Ultimately, you can earn your company revenue and save the client time, energy, and money and reduce errors due to multiple data entry points. This blog demonstrates how to use the GcWord API (using C#) to quickly and efficiently create and save documents using GcWord Reporting Templates (and the 'mustache' syntax) from an existing data source.

For a much more detailed look at all various options in a single blog, check out the original introductory blog on Word Templates.

See the step-by-step instructions below to get started and download the complete sample project here.

Steps to Create GcWord Report Template

The GcWord V4 API utilizes Report Templates and a "mustache syntax" to indicate the fields populated through a data source. An example of the Housing rental agreement is shown below with the proper syntax.

Notice the consistent use of double curly brackets on both sides of the templated field name. There are more advanced features with this syntax, which will get covered in a separate blog that uses these templates with datasets. For now, we will focus on this template and some of the field properties and features. Note, there are only a few formatters used, but these are consistent and easy to understand. The chart below will help you to understand the layout of the House Rental Agreement sample document:

Sample Document

Below is the example of the full House Rental Agreement utilizing the above syntax:

Sample template file for rental agreement to be used by GcWord V4 API in Csharp by GrapeCity

Sample template file for rental agreement to be used by GcWord V4 API in Csharp by GrapeCity

Steps to Create Files from GcWord Report Template

To use the GcWord V4 API to create documents from this template, follow the example located here, but a synopsis of the steps is below:

  1. Be sure to include all appropriate namespaces in your project

    using System;
    using System.IO;
    using System.Drawing;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Linq;
    using GrapeCity.Documents.Word;
  2. Next, instantiate a new GcWordDocument object and load your template file

    var doc = new GcWordDocument();  
    //now load your file  
    doc.Load(Path.Combine("Resources", WordDocs", "House_Rental_Template.docx");
  3. Set up a dataset
    var ds = new DataSet();
  4. Read the data into the object (In this case, an XML file)
    ds.ReadXml(Path.Combine("Resources", "data", "GcWordTplDataSet.xml")); 
  5. Lastly, process and save the document (typically called from a separate method which returns an object of type GcWordDocument)

As stated, the full example and our documentation site have more information, but the above steps are the main portions of what is required to utilize the API to get a template and create a new file from a data source. The final product will look like this:

Sample template file for rental agreement to be used by GcWord V4 API in Csharp by GrapeCity

Sample template file for rental agreement to be used by GcWord V4 API in Csharp by GrapeCity

Sample template file for rental agreement to be used by GcWord V4 API in Csharp by GrapeCity

Download the sample and give it a try! Next month, the second part in this series will discuss how to set up another word template document for a different industry.

We hope this helps get the imagination flowing on all the ways to utilize the GcWord V4 API. Happy coding!

Sean Deegan

Sean Deegan

Product Manager
comments powered by Disqus