Column Span

This guide shows how to create tables with multi column spanning cells. Column spanning cells are table cells that span multiple columns.

Before you begin

You should get your API key from your UniCloud account.

If this is your first time using UniPDF SDK, follow this guide to set up a local development environment.

Project setup

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/unipdf-examples.git

Navigate to the tables folder in the unipdf-examples directory.

cd unipdf-examples/tables

Configure environment variables

Replace the UNIDOC_LICENSE_API_KEY with API credentials from your UniCloud account.

Linux/Mac

export UNIDOC_LICENSE_API_KEY=PUT_YOUR_API_KEY_HERE

Windows

set UNIDOC_LICENSE_API_KEY=PUT_YOUR_API_KEY_HERE

How it works

The following example code shows how to draw a table with multi columns spanning cells.

The section in lines 9-16 imports unipdf packages and other necessary go libraries. The init() function in lines 18-25 authenticates your request by loading your metered License API key before running main function.

The main function in lines 27-52 instantiates a new Creator object with the given margins. The necessary fonts for the pdf document are created and the columnSpan function is called to create the example page. Finally the document is written to file in line 49 using Creator.WriteToFile(file_path).

In lines 54-146 the pdf page with the example table is created using c.NewPage(). The table created using NewTable(5) function. In this example a table with 5 columns is created. Then a multi column cell is added using MultiColCell(colspan) where the colspan specifies how many columns the cell spans.

The table is added to creator.Chapter in Line 139 and finally the chapter is drawn on creator in lines 141-143.

Run the code

To create the example document run the code using the following command.

go run pdf_tables_colspan.go

Sample output

After running code you should see a pdf that look like as follows.

PDF table colspan

Got any Questions?

We're here to help you.