Line Spacing

This guide will show you how to work with line spacing in text.

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/line-spacing folder in the unioffice-examples directory.

cd unioffice-examples/document/line-spacing/

How it works

// Copyright 2017 FoxyUtils ehf. All rights reserved.
package main
import (
"log"
"os"
"github.com/unidoc/unioffice/v2/common/license"
"github.com/unidoc/unioffice/v2/document"
"github.com/unidoc/unioffice/v2/measurement"
"github.com/unidoc/unioffice/v2/schema/soo/wml"
)
func init() {
// Make sure to load your metered License API key prior to using the library.
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
if err != nil {
panic(err)
}
}
func main() {
doc := document.New()
defer doc.Close()
lorem := `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin lobortis, lectus dictum feugiat tempus, sem neque finibus enim, sed eleifend sem nunc ac diam. Vestibulum tempus sagittis elementum.`
// single spaced
para := doc.AddParagraph()
run := para.AddRun()
run.AddText(lorem)
run.AddText(lorem)
run.AddBreak()
// double spaced is twice the text height (24 points in this case as the text height is 12 points)
para = doc.AddParagraph()
para.Properties().Spacing().SetLineSpacing(24*measurement.Point, wml.ST_LineSpacingRuleAuto)
run = para.AddRun()
run.AddText(lorem)
run.AddText(lorem)
run.AddBreak()
if err := doc.Validate(); err != nil {
log.Fatalf("error during validation: %s", err)
}
doc.SaveToFile("line-spacing.docx")
}

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

The init function in lines 14-21 authenticates your request with your UNIDOC_LICENSE_API_KEY.

In the main function in lines 23-47, a new document is generated, and a variable named lorem, which holds text, is defined. Next, a new paragraph is created, comprising a single run that inserts the content of the lorem variable twice. Subsequently, a blank line is inserted beneath this paragraph using the AddBreak() function.

Following this, the same operations are repeated, but this time specifying the line spacing using the SetLineSpacing() function.

Finally, the document is validated and saved.

Run the code

Run this command to create a new document with line spacing.

go run main.go

Sample output

Line Spacing example

Got any Questions?

We're here to help you.