Row Span

In this guide creating a table with multi row spanning cells will be explained.

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 create multi row spanning cells in a table.

Lines 9-17 imports the necessary unipdf packages and other go libraries. Lines 19-25 defines an init function that loads your metered License API key for authenticating your request.

The main function in lines 28-53 instantiates a new creator.Creator object, creates the row span table by calling rowSpan function and finally writes the document to unipdf-rowSpan-tables.pdf.

The function rowSpan defined in 55-125 creates the table with row spanning cells. In line 72 a table with 5 columns is created using c.NewTable(5). The anonymous function in lines 75-89 draws a cell using a font, number of rows to span, color and background color. Here a multi row spanning cell is drawn using table.MultiRowCell(rowspan). The rowspan variable here specifies the number of rows the cell should span. In lines 94-116 the table contents are drawn by calling drawCell function using the specified parameters. Then the table is added to the chapter using ch.Add(table) in line 118. Finally the chapter containing the text and the table is drawn using c.Draw(ch) in line 120.

Run the code

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

go run pdf_tables_rowspan.go

Sample output

PDF table row spanning

Got any Questions?

We're here to help you.