Insert Image

This guide will demonstrate how to insert images into a document.

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

To get the example navigate to the path document/image folder in the unioffice-examples directory.

cd unioffice-examples/document/image/

How it works

Lines 4-15 import the UniOffice packages and other required dependencies.

The init function in lines 17-24 authenticates your request with your UNIDOC_LICENSE_API_KEY.

In the main function, spanning lines 28-85, a discernible pattern emerges. In lines 32-34, an image is directly accessed using ImageFromFile(), while lines 36-43 involve opening a file through ReadFile() and subsequently transforming it into an image with ImageFromBytes(). These images are subsequently integrated into the document, and references for future use are obtained in lines 45-52.

Moving forward to lines 54-64, an anchored image is introduced with meticulous adjustments to measurements and alignments. Subsequently, a run is added, repeating the text stored in the lorem variable 16 times, and both images are being inserted on index (i variable) 13 and 15, between lines 66-86.

Ultimately, the code concludes by saving the file.

Run the code

Run this command to create a new document with the 3 images added between the text.

go run main.go

Sample output

Image result 1

Image result 2

Got any Questions?

We're here to help you.