Word to PDF Conversion with Go and OLE

This guide will show you how to create a new Word Document, add content, and convert it to PDF using OLE technology. It’s important to note that this guide can only be run on Windows, as it utilizes OLE.

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/pdf-export-ole folder in the unioffice-examples directory.

cd unioffice-examples/document/pdf-export-ole/

How it works

Lines 4-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.

In the main function, spanning from line 26 to 51, a new Word document is created, and three paragraphs with different styles, each containing different text content, are added to it. Then, at line 44, the created document is saved.

Subsequently, at line 47, the ConvertToPDF function, defined from lines 54 to 77, is called. This function uses the OLE library to interact with the Word application. It initiates Word, opens the simple.docx document, saves it in PDF format, and finally, closes the Word application, all accomplished through OLE technology.

Run the code

Run this command to create a new Word Document and convert it to a PDF.

go run main.go

Sample output

Line Spacing example

Got any Questions?

We're here to help you.