Use a Template
document.OpenTemplate opens a .docx and discards its body, leaving the
styles, numbering definitions and header parts behind. You get an empty document
that already knows what “Heading1” and “GridTable4-Accent1” look like, so the
content you generate picks up an existing house style without you defining a
single font or color in Go.
| Call | Body content | Styles and numbering | Page setup |
|---|---|---|---|
document.New() | Empty | Library defaults | Library defaults |
document.Open(path) | Kept | Kept | Kept |
document.OpenTemplate(path) | Discarded | Kept | Lost, see below |
Use Open when you are editing a document, OpenTemplate when you are
generating one and only want the formatting.
Applying a style
doc, err := document.OpenTemplate("template.docx")
if err != nil {
log.Fatalf("error opening template: %s", err)
}
defer doc.Close()
para := doc.AddParagraph()
para.SetStyle("Heading1")
para.AddRun().AddText("Major Section")SetStyle takes the style ID, not the name Word shows in its gallery. The two
often differ: the style displayed as “Heading 1” usually has the ID Heading1,
and a custom style can have an ID bearing no resemblance to its name at all.
Iterating doc.Styles.Styles() and printing StyleID() next to Name(), as the
example does, is the quickest way to find out what a given template actually
contains. If you only know the display name, doc.Styles.SearchStyleByName hands
you back the Style so you can read StyleID() off it.
Table styles work the same way. table.Properties().SetStyle takes the style ID,
and the built-in table styles bring their own banding and emphasis defaults, so
the example turns several off through table.Properties().TableLook() after
applying the style.
Preparing the template
Word deletes unused styles when it saves. A document that merely has the
Heading 1 style available in its gallery does not carry a definition for it in
styles.xml unless something in the document is set in that style. To build a
template, write one paragraph in each style you plan to use and save it; the
template.docx shipped with the example is a page of exactly that.
Style definitions that come in through the template do not need registering.
Once OpenTemplate has read the file, any ID in styles.xml is usable.
Limitations
SetStyle writes the string straight into the paragraph’s properties. There is
no lookup and no error: an ID that does not exist in the document leaves the
paragraph rendering in the default style, which usually reads as “the style did
not apply” rather than as a bug in the ID.
Page size, margins, columns and header references live in the body’s section
properties, and OpenTemplate replaces the whole body. A landscape template
therefore produces a portrait document, and a template’s header stops appearing
even though the header part is still in the file. Reattaching the header is
covered in Template with a Header; page setup has
to be applied again through doc.BodySection().
Content wiped by OpenTemplate includes anything in the body: bookmarks,
tables, images placed in paragraphs. Only the parts stored outside the body
survive.
Run the example
The example prints every style in template.docx with its ID and type, then
builds a short document using the title, subtitle and heading styles and a
five-by-five table in a built-in table style.
git clone https://github.com/unidoc/unioffice-examples.git
cd unioffice-examples/document/use-template
go run main.goIf this is your first time using UniOffice, follow the getting started guide to create an API key and set up your development environment.