1. Products
  2.   Word Processing
  3.   GO
  4.   unioffice
 
  

Go LIbrary to Generate & Edit MS Word Documents

Open Source Go Library for Managing & Automating common Word Processing tasks like inserting Header & Footer, Tables & Images to Word DOCX files & more.

unioffice is a powerful open source pure Go library that allows software developers to manage word documents and automate common word processing tasks with ease inside their own applications without requiring Microsoft Word. The library is quite well optimized and allows you to easily edit the code to meet your requirements.

The unioffice library is a strong Go-based library that can be used for generating, editing, formatting, and processing of Office Open XML documents. The library supports several important word processing features, such as reading, writing & modifying Word documents, Text Formatting support, Auto-generated table of contents, Placing on a document page, headers and footers insertion, adding tables, Opening a document as a template, form fields support and much more.

Previous Next

Getting Started with unioffice

The recommended way to install unioffice into your project is by using GitHub. Please use the following command for a smooth installation.

Install unioffice via GitHub

go get github.com/unidoc/unioffice/
go build -i github.com/unidoc/unioffice/...   

Create Word DOCX Document via Go API

The open source library unioffice has provided the facility for creating a new Word DOCX Document with ease. You can also easily open and modify an existing Microsoft Word Document inside your own applications. The library also included features for adding new text paragraphs, inserting images to a page, text alignment, adding headers and footers, adding tables, and many more.

Access Word Document Properties via Go API

func main() {
	doc, err := document.Open("document.docx")
	if err != nil {
		log.Fatalf("error opening document: %s", err)
	}
	defer doc.Close()

	cp := doc.GetOrCreateCustomProperties()

	// You can read properties from the document
	fmt.Println("AppVersion", *cp.GetPropertyByName("AppVersion").X().Lpwstr)
	fmt.Println("Company", *cp.GetPropertyByName("Company").X().Lpwstr)
	fmt.Println("DocSecurity", *cp.GetPropertyByName("DocSecurity").X().I4)
	fmt.Println("LinksUpToDate", *cp.GetPropertyByName("LinksUpToDate").X().Bool)
	fmt.Println("Non-existent", cp.GetPropertyByName("nonexistentproperty"))

	// And change them as well
	cp.SetPropertyAsLpwstr("Company", "Another company") // text, existing property
	fmt.Println("Company", *cp.GetPropertyByName("Company").X().Lpwstr)

	// Adding new properties
	cp.SetPropertyAsLpwstr("Another text property", "My text value") // text
	cp.SetPropertyAsI4("Another integer number property", 42)        // int32
	cp.SetPropertyAsR8("Another float number property", 3.14)        // float64
	cp.SetPropertyAsDate("Another date property", time.Now())        // date

	doc.SaveToFile("document_customized.docx")

	// For new documents all is the same
	docNew := document.New()
	defer docNew.Close()

	cpNew := docNew.GetOrCreateCustomProperties()
	cpNew.SetPropertyAsLpwstr("Another text property", "My text value") // text
	cpNew.SetPropertyAsI4("Another integer number property", 42)        // int23
	cpNew.SetPropertyAsR8("Another float number property", 3.14)        // float64
	cpNew.SetPropertyAsDate("Another date property", time.Now())        // date

	docNew.SaveToFile("document_new.docx")
}
  

 Insert Images in Word DOCX Files

The open source library unioffice provides software developers the ability to use images inside Microsoft Word documents. It supports functionalities like inserting images to the place of your choice, modifying an existing image, text wrapping around the image, deleting the image, and many more. For adding an image it is necessary to provide the name and location of the image.

Manage Images in Word Documents via Go API

func main() {
	doc := document.New()
	defer doc.Close()

	img1, err := common.ImageFromFile("gophercolor.png")
	if err != nil {
		log.Fatalf("unable to create image: %s", err)
	}
	img2data, err := ioutil.ReadFile("gophercolor.png")
	if err != nil {
		log.Fatalf("unable to read file: %s", err)
	}
	img2, err := common.ImageFromBytes(img2data)
	if err != nil {
		log.Fatalf("unable to create image: %s", err)
	}

	img1ref, err := doc.AddImage(img1)
	if err != nil {
		log.Fatalf("unable to add image to document: %s", err)
	}
	img2ref, err := doc.AddImage(img2)
	if err != nil {
		log.Fatalf("unable to add image to document: %s", err)
	}

	para := doc.AddParagraph()
	anchored, err := para.AddRun().AddDrawingAnchored(img1ref)
	if err != nil {
		log.Fatalf("unable to add anchored image: %s", err)
	}
	anchored.SetName("Gopher")
	anchored.SetSize(2*measurement.Inch, 2*measurement.Inch)
	anchored.SetOrigin(wml.WdST_RelFromHPage, wml.WdST_RelFromVTopMargin)
	anchored.SetHAlignment(wml.WdST_AlignHCenter)
	anchored.SetYOffset(3 * measurement.Inch)
	anchored.SetTextWrapSquare(wml.WdST_WrapTextBothSides)

	run := para.AddRun()
	for i := 0; i < 16; i++ {
		run.AddText(lorem)

		// drop an inline image in
		if i == 13 {
			inl, err := run.AddDrawingInline(img1ref)
			if err != nil {
				log.Fatalf("unable to add inline image: %s", err)
			}
			inl.SetSize(1*measurement.Inch, 1*measurement.Inch)
		}
		if i == 15 {
			inl, err := run.AddDrawingInline(img2ref)
			if err != nil {
				log.Fatalf("unable to add inline image: %s", err)
			}
			inl.SetSize(1*measurement.Inch, 1*measurement.Inch)
		}
	}
	doc.SaveToFile("image.docx")
}
  

Add Header & Footer to Word Document

Headers and footers can be used to include the information that users want to appear on every page of a document such as an author name, document title, or page numbers. The unioffice library allows software developers to add headers & footers to word documents with ease. It also allows having different headers and footers depending on the document section. It also included support for even, odd, and first functionalities.

Add Header & Footer to Word Document via Go API

func main() {
	doc := document.New()
	defer doc.Close()

	img, err := common.ImageFromFile("gophercolor.png")
	if err != nil {
		log.Fatalf("unable to create image: %s", err)
	}

	hdr := doc.AddHeader()
	// We need to add a reference of the image to the header instead of to the
	// document
	iref, err := hdr.AddImage(img)
	if err != nil {
		log.Fatalf("unable to to add image to document: %s", err)
	}

	para := hdr.AddParagraph()
	para.Properties().AddTabStop(2.5*measurement.Inch, wml.ST_TabJcCenter, wml.ST_TabTlcNone)
	run := para.AddRun()
	run.AddTab()
	run.AddText("My Document Title")

	imgInl, _ := para.AddRun().AddDrawingInline(iref)
	imgInl.SetSize(1*measurement.Inch, 1*measurement.Inch)

	// Headers and footers are not immediately associated with a document as a
	// document can have multiple headers and footers for different sections.
	doc.BodySection().SetHeader(hdr, wml.ST_HdrFtrDefault)

	ftr := doc.AddFooter()
	para = ftr.AddParagraph()
	para.Properties().AddTabStop(6*measurement.Inch, wml.ST_TabJcRight, wml.ST_TabTlcNone)
	run = para.AddRun()
	run.AddText("Some subtitle goes here")
	run.AddTab()
	run.AddText("Pg ")
	run.AddField(document.FieldCurrentPage)
	run.AddText(" of ")
	run.AddField(document.FieldNumberOfPages)
	doc.BodySection().SetFooter(ftr, wml.ST_HdrFtrDefault)

	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`

	for i := 0; i < 5; i++ {
		para = doc.AddParagraph()
		run = para.AddRun()
		run.AddText(lorem)
	}

	doc.SaveToFile("header-footer.docx")
}
  

Working with Tables in Word DOCX

The open source library unioffice enables computer programmers to add and modify tables inside word documents. Tables are very useful and can be used to organize and present data in a better way.  It supports adding a table with and without borders as well as allows constructing a table style with ease. You can easily place content in a table and adjust the size according to your needs.

 English