Grids With Column Spans

This guide will show how to use the grid creator to produce PDF pages that have grids layout with columns spans.

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.

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 grid folder in the unipdf-examples directory.

cd unipdf-examples/grid

How it works

The code begins by importing the necessary dependencies. Then the init function sets up the license key.

The main function is defined in lines 28-74. It starts by instantiating the creator object and specifying the page margins in lines 29-30. Then the creator.Grid object is instantiated by specifying the number of columns we want to use using the creator.NewGrid() method. Then a new row is created using the grid object in line grid.NewRow() in line 36. A a cell that spans 3 columns and one row is created using the row.NewMultiCell(3, 1) in line 39. This method returns a pointer to creator.GridCell which can then be populated with content. This is done in lines 45-57.

An image is read from file, then an creator.Image is created in line 49. Lines 54-55, set the alignment properties of the cell. Then in line 57, the content of the cell is set with the image using cell.SetContent(img). In lines 60-63, a second row is added and populated with text using the helper function AddLoremIpsumCell defined in lines 76-89.

In line 65, the grid is drawn using the Draw method of the creator object. and finally the document is written to file in line 71.

Run the code

To run this code, use this command:

go run pdf_grid_colspan.go

Sample output

output

Got any Questions?

We're here to help you.