DOCX Template API
Generate dynamic MS Word documents using a DOCX template and JSON data, allowing seamless data injection and automated reporting.
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.
With the latest update, you can choose between two methods to bind and replace content in your DOCX templates. This flexibility allows you to either replace data in property fields or directly in placeholders within the document.
{{my_name}} or {{address}}. It supports inserting text, tables, or images.Selecting the appropriate BindingMethod enables you to customize the template to your document's needs, depending on whether you are working with predefined properties or dynamic placeholders.
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".

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 programmatically.

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.

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.
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.
Use the following JSON structure when BindingMethod is set to "Property Fields":
[
{
"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 typedatetime - represents the Date value typeinteger - represents the Number value typeboolean - represents the Yes or no value typeTo set up placeholders in your DOCX template, simply insert placeholder text enclosed in double curly braces, like {{my_name}}. These placeholders will act as markers for dynamic content. When generating the final document, the content within each placeholder (e.g., my_name) will be replaced with the corresponding value from your JSON data.
For example, if you add {{my_name}} to the document, it can later be dynamically replaced with the actual name specified in your JSON input.

For custom placeholders, use this JSON structure to enable various content types, including string (text), table, html, image and conditional:
[
{
"Name": "name",
"Value": "John",
"Type": "text",
"Link": "https://www.convertapi.com/"
},
{
"Name": "myCustomTable",
"Type": "table",
"TableData": [
["Row1Cell1", "Row1Cell2", "Row1Cell3"],
["Row2Cell1", "Row2Cell2", ""]
]
},
{
"Name": "myHtml",
"Value": "<h2 style=\"color:green\">Hello <i>World!</i></h2><p>Another paragraph <b>here</b>.</p>",
"Type": "html",
"Link": "https://www.example.com"
},
{
"Name": "MyCondition",
"Value": "false",
"Type": "conditional"
},
{
"Name": "myImage",
"ImageWidth": 200,
"ImageHeight": 100,
"Value": "/9j/4AAQS...<Rest_of_image_base64_string>",
"Type": "image",
"Link": "https://www.convertapi.com/"
}
]Text (Placeholder #1 in the image above)
Use this for straightforward text replacements. For example, {{name}} can be replaced with "John".
Table (Placeholder #4 in the image above)
Populate placeholders with tabular data using TableData. Each row is defined as an array of cell values, and columns should align consistently across rows.
Html (Placeholder #3 in the image above)
Embed HTML content directly into your .docx file. This supports advanced styling and customization.
Image (Placeholder #2 in the image above)
Replace placeholders with images by providing a base64-encoded string in the Value. You can control image dimensions using ImageWidth and ImageHeight. Optionally, include a Link property.
Conditional (Placeholder #5.1 and #5.2 in the image above)
This placeholder type does not include any direct data. Instead, it controls the visibility of a section in your document. Wrap the section with {{#IF MyCondition}} and {{#ENDIF MyCondition}}. If MyCondition is true, the enclosed content remains; if false, it is removed.
Hyperlinks
Each placeholder type can include aLink. For instance, if you addlink: "https://www.convertapi.com/"to a text property, the entire text becomes hyperlinked. Similarly, you can useLinkon other placeholders (e.g., images) to create clickable elements (like buttons).
Note: The
Linkproperty works for any placeholder type, not just text.
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:
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 a JSON object from your backend code. This process not only saves time, but also eliminates the possibility of 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 the interactive demo tool online for free!