Simple Grid
This guide will show how to use the grid creator to produce pdf pages with simple grids layout. The Grid
component is similar to the Table
component but follows a different structure. It uses HTML-style hierarchy of table->rows->cells
, unlike the table->cells
approach used in Table
.
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 necessary libraries are imported in lines 9-15
. The metered API key is set using license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
in the init
function defined in lines 17-24
.
The main
function, where the simple grid is created, starts in line 26
. First, a creator object is created in line 27
, and the global margins of the pages are set in line 28
using SetPageMargins(50, 50, 50, 50)
. A creator.Grid
object with 2 columns is instantiated in line 31
. Then a new row is added in line 35-35
using:
row := grid.NewRow()
AddCell("Company", c, row, true, true)
AddCell("UniDoc", c, row, true, false)
First, the row object is created by calling the NewRow
method of the creator.Grid
object. Then, two cells are added using the AddCell
helper function. This function is defined in lines 64-83
. We will see its contents and what it does later. In general, the function takes the cell content, the creator object, a boolean to specify whether to set borders, and another boolean to set the background.
A similar block is used in lines 49-51
to create another row. In line 53
, the grid is drawn using creator.Draw
method.
Finally, the document is written to file in line 59
.
The AddCell
helper function is defined in lines 64-83
. It takes input text, the creator object, the GridRow
, isBorder
and isBackground
parameters. In line 65
, a new cell is created using row.NewCell()
. Lines 70-73
, create a paragraph with its content and properties. In lines 75-76
a border is added if isBorder
is true. If isBackground
is true, a background color is set to the cell in 78-79
. In line 81
, the content of the cell is set.
Run the code
Use this command to run the code.
go run pdf_grid_simple.go