Edit Document

This guide will instruct you on editing a Word document by modifying the fields of a pre-designed template.

Before you begin

You should get your API key from your UniCloud account.

If this is your first time using UniOffice SDK, follow this guide to set up a local development environment.

Clone the project repository

In your terminal, clone the examples repository. It contains the Go code we will be using for this guide.

git clone https://github.com/unidoc/unioffice-examples

Navigate to the path document/edit-document/ folder in the unioffice-examples directory.

cd unioffice-examples/document/edit-document/

It can be observed that there is a document that appears as follows:

Pre-designed template

How it works

Lines 5-13 import the UniOffice packages and other required dependencies.

The init function in lines 15-22 authenticates your request with your UNIDOC_LICENSE_API_KEY.

Entering the main function, the file is opened in lines 25-29, and all the paragraphs of the document are collected in lines 31-34.

Furthermore, in this example, there are structured document tags that are commonly found in templates, like the one we are using. These tags are extracted and added to the list of paragraphs in lines 39-43. This is done to ensure that all parts of the document are considered for manipulation, although normally using just the paragraphs works correctly.

Finally, the core of the code in lines 45-110 iterates through the paragraphs. For each paragraph, its different Runs (text defined with the same style) are looped through. The content of each run’s text is read, and depending on its content, it is replaced with another text field. This process results in the edited document, which is then saved when it’s finished.

Run the code

Run this command to generate the edited file based on the template:

go run main.go

Sample output

Edited document

Got any Questions?

We're here to help you.