Page Wrapping Grids

This guide will show how to use the grid creator to produce pdf pages with simple grids layout.

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

First, the necessary libraries are imported in 9-16. Then, the metered API key is set in the init function defined in lines 20-27.

The main function, where the grid is created and drawn, is defined in lines 29-87. First, a creator object is created in line 30 using creator.New(). Then the page margins for all the pages to be crated are set using this creator is set using SetPageMargins(50, 50, 50, 50) in line 31.

The creator.Grid object with 4 columns is instantiated from the creator object using c.NewGrid(4). Using nested for loops in 36-52, 25 rows each with 4 cells are created. In the outer loop, at each iteration a new row is created using row := grid.NewRow(). This is done in line 37. Then in the inner loop, the cell are added to these rows as follows:

  cell, err := row.NewCell()
  if err != nil {
    log.Fatal(err)
  }

  p := c.NewStyledParagraph()

  p.SetText(fmt.Sprintf("Row: %d Cell: %d", cell.row, cell.col))
  p.SetMargins(5, 5, 5, 5)
  p.SetFontSize(12)
  cell.SetBorder(CellBorderSideAll, CellBorderStyleSingle, 1)
  cell.SetContent(p)

First a new cell is created from the row using row.NewCell(). Then a new styled paragraph is created, its text set and its properties specified in lines 44-48. The border of the cell is set in line 49. Then, content of the cell is set to the paragraph object in line 50.

In line 54, another row intended to wrap across pages is created. Two cells spanning columns,as specified in the argument are then created similarly in lines 67-76. In line 78 the whole grid is applied using the Draw method of the creator object.

Finally, the pdf document is written to a file using c.WriteToFile("unipdf-grid-wrapping.pdf") in line 84.

Run the code

Using this command run the code to create the page wrapping grids.

go run pdf_grid_wrapping.go

Sample output

output

Got any Questions?

We're here to help you.