Skip to main content

Replace text in a word document

The following workflow outlines the process required to replace text in a word document.

Use case

We have a word document that needs to be prepared for our client. The document itself is set up to be a template of sorts - variable fields in the document are named so we can 'fill' the template out with a simple search and replace.

The template we're referencing throughout this use case is MyTemplate.docx.

The bucket we're referencing throughout this use case is Production.

Template example

Workflow

Uploading the template

For this example we're going to assume that the template is used often and as such, lives permanently in a storage container in doclabs.

You can take a look at an upload example here.

Replacing text

Replacing text is quite simple. We simply POST a request to the doclabs Word.API with key/value pairs in the body.

curl --location 'https://word-api.doclabs.cloud/api/v1/MyTemplate.docx/text/replace?storageName=Production' \
--header 'Content-Type: application/json' \
--header 'x-api-key: ••••••' \
--data '{
"data": {
"USERNAME": "John Doe",
"USERSTREET": "1 John St",
"USERCITY": "Johnville",
"USERSTATE": "JN",
"USERPOSTCODE": "12345",
"TODAYS_DATE": "01/01/2024"
}
}'

The response we receive will tell us the location of our newly generated document.

{
"bucketName": "Production",
"contentPathName": "MyTemplate.c32f2db1-a382-443e-a18f-2b86a6d36165.docx"
}

Understanding the result

Downloading the document can be done either through the console or Storage.API. An example using the API can be seen here.

If we were to view the new document now, it would look something like this:

Modified template example

Converting to PDF

Before we email this letter off to our client, we need to convert it to a PDF.

curl --location --request POST 'https://word-api.doclabs.cloud/api/v1/MyTemplate.c32f2db1-a382-443e-a18f-2b86a6d36165.docx/convert?type=pdf&storageName=Production' \
--header 'x-api-key: ••••••' \
--data ''

The response we receive tells us the location of our newly generated document.

{
"bucketName": "Production",
"contentPathName": "MyTemplate.a4c16035-5e3f-4ecf-a5ac-1ce13365f44d.pdf"
}

Securing the PDF

This particular document should not be modified once sent out to our client. So let's quickly secure it before downloading and sending it off.

curl --location '/api/v1/MyTemplate.a4c16035-5e3f-4ecf-a5ac-1ce13365f44d.pdf/protect?storageName=Production' \
--header 'Content-Type: application/json' \
--header 'x-api-key: ••••••' \
--data '{
"ownerPassword": "capvAanW8hX23Lz36Cxk8TbQk"
}'

And the result:

{
"bucketName": "Production",
"contentPathName": "MyTemplate.ea4492b1-df90-48df-9635-20c380adf2c8.pdf"
}

Downloading the result

From here we can either download it from the console or the Storage.API.

Recap

In this example we went over the process of replacing text in our template, converting it to a PDF, securing the PDF with an owner password and then finally downloading.

This process would have added 4 calls to our API quota.