Text Formatting Using HTML Tags

This guide will show how to use HTML tags to format text in docx document using unioffice. This feature is found in unioffice v2.
This guide will use an example code provided in the examples repository.

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/text-formatting-tag folder in the unioffice-examples directory.

cd unioffice-examples/document/text-formatting-tag

How it works

In lines 8-14, we find the import section where the necessary dependencies are imported. Then the init function defined in lines 16-23 sets the API key by calling license.SetMeteredKey function. The API key that is provided as the parameter is obtained from the system environment.

The main function is then defined next in lines 25-93. In line 26 of this function, a new document is instantiated using document.New(). Then the createParaRunHTML function is used to add paragraphs with runs that are formatted using HTML tags. Since this function is used every where in the main function, let’s first see its definition and how it works. This function is defined in lines 95-100 as follows:

func createParaRunHTML(doc *document.Document, s string) document.Run {
	para := doc.AddParagraph()
	run := para.AddRun()
	run.AddHTML(s)
	return run
}

The function accepts the *document.Document and html string as parameters. First a paragraph is created in line 96. Then document.Run is instantiated from the paragraph using para.AddRun(). And the html tagged string is added to the run using run.AddHTML(s). And finally this run is returned from the function.

Now let’s return to the main function and see how different kinds of HTML tags are used to format document contents. In line 30 and 33, a bold text is added using the <b> and <strong> tags respectively. In lines 36-57, italic, underlined, stroked, subscripted, superscripted, and highlighted texts are added in similar ways. Also, combined tags are used in line 60. Lines 62 and 63 add two empty paragraphs.

In lines 66-67, different nested tags are added as follows.

htmlPara := doc.AddParagraph()
htmlPara.AddHTML("<b><i>HTML text</i></b> and this one is <u><strike>underlined and stroked</strike></u>")

Here, the HTML is directly added to the paragraph using the AddHTML method. Similarly different kinds of nested tags are added in lines 69-82. In Lines 84-87, both raw text and HTML tagged content are added.

The supported HTML tags are:

  • <b> and <strong> for bold text
  • <i> and <em> for italicized text
  • <u> for underlining
  • <strike> for strike through text
  • <mark> for highlighting text
  • <sub> subscript formatting
  • <sup> for superscript formatting

Finally, the document is saved to file in line 89 using doc.SaveToFile("text_formatting_using_tag.docx").

Run the code

To run the code use the following command inside the text-formatting-tag folder.

go run main.go

Sample output

document with different formats

Got any Questions?

We're here to help you.