Generate DOCX and PDF from code using a Word template

Kostas, Developer

Paperwork is a real burden for many businesses, including ours. Repetitive tasks like filling in proposals, agreements, invoices, and NDAs consume a lot of time and resources. However, the good news is that most of these tasks can be automated! Using our DOCX Template API, you can generate custom documents with the information prefilled by your system programatically. You can think of these custom properties as placeholders for your actual data. Simply inject the relevant data (text, dates, numbers, or boolean values) into these placeholders and the final document containing your data will be ready in no time!

For instance, if you want to generate a quote for a potential customer who is visiting your website, you can simply let them fill in their company details on your website, select the desired services or products, and with a single click, they can download an auto-generated pricing quote in a DOCX or a PDF file format. You can also do the same for legal signable documents, where you would prefill both parties' details from the code, and the only thing left to do manually is to sign it.

How to prepare a DOCX template for dynamic values injection?

Custom properties can be leveraged in document templates to automate the population of specific information inside the document's content. This is particularly useful for standardized documents where certain details remain constant across multiple instances. Creating a document template may seem like a daunting task at first, but once you get the hang of it, it can save you countless hours of repetitive work.

To access and manage custom document properties in Microsoft Word, you can go to the "File" menu, select "Info" -> "Properties" -> "Advanced properties".

custom-props-bubbles.png

Once the "Properties" dialog pops up, please choose the "Custom" tab. From there, you can view, edit, or add custom properties as needed. Each property consists of Name, Type, and Value - you can think of them as placeholders. Set up as many placeholders as needed for your document. Later on we will insert these properties into the document and prefill the "Value" of each property from the code programatically.

custom-properties.png

Once you have created the properties, you can go ahead and insert them into your document content where needed. To do so, go back to your document view, click on the "Insert" tab at the top, and locate the "Quick parts" button. Once you click on it, the dropdown will appear. You need to select the "Field" -> "DocProperty" and select your newly created property from the "Field properties" list. Repeat this action for all your custom-created properties and place them where applicable.

custom-properties.png

As soon as you insert all the custom properties to your document content, your template is complete! You can download the sample quote DOCX template that we created for this demonstration purpose here: quote-template.docx.

Insert values to DOCX template from code using JSON

Let's try a real-life example. We prepared a DOCX template for automatic quote generation. In this example document, we have added 7 custom properties where we will inject values using a simple JSON object. For this purpose, we can use the DOCX Template API, or the Dynamic PDF API endpoint. Both of these converters require two things - the DOCX template and a JSON object containing values to be inserted into the document. The only difference is that the first one produces an editable DOCX document with custom values injected and the second one generates a PDF file.

No matter which output format, PDF or DOCX you choose, our JSON object would look like this:

[
  {
    "Name": "QuoteNumber",
    "Value": 7458,
    "Type": "integer"
  },
  {
    "Name": "IssueDate",
    "Value": "2024-07-30",
    "Type": "datetime"
  },
  {
    "Name": "DueDate",
    "Value": "2024-08-30",
    "Type": "datetime"
  },
  {
    "Name": "ArchitecturalWork",
    "Value": "7 999.99",
    "Type": "string"
  },
  {
    "Name": "InteriorWork",
    "Value": "6 000.00",
    "Type": "string"
  },
  {
    "Name": "GrandTotal",
    "Value": "13 999.99",
    "Type": "string"
  },
  {
    "Name": "Customer",
    "Value": "ConvertAPI",
    "Type": "string"
  }
]

As you may have noticed, we specified the custom document properties in a Word document by setting the property value type. Usually, the Text value type is the most flexible option as it accepts all sorts of information, however, the values injected into the document can be set up of different types. These types must be specified in the JSON object in the following manner:

  • string - represents Text value type
  • datetime - represents the Date value type
  • integer - represents the Number value type
  • boolean - represents the Yes or no value type

Generated DOCX and PDF documents with custom data

Using our DOCX template and the JSON object provided, we can easily generate professionally formatted DOCX and PDF documents with your custom data using either of our conversion endpoints. You can check out the conversion results here:

Conclusion

Our DOCX Template API eliminates the need for manual data entry and repetitive work, allowing you to focus on more important tasks at hand. With just a few simple steps, you can save your precious time and automate your paperwork. All you need to do is prepare a DOCX document template and prefill it with data using JSON object from your backend code. This process not only saves time, but also eliminates the possibility of a human error - the output documents are accurate and consistent every time! You can try to inject custom variables into your documents and generate DOCX and PDF files using interactive demo tool online for free!