Skip to content
Grids with Column Spans

Grids with Column Spans

A cell covering more than one column comes from NewMultiCell rather than NewCell:

grid := c.NewGrid(3)

row := grid.NewRow()

// Span all three columns: colspan 3, rowspan 1.
cell, err := row.NewMultiCell(3, 1)
if err != nil {
    return err
}
if err := cell.SetContent(p); err != nil {
    return err
}

Note the argument order: colspan first, then rowspan. This is the reverse of Table.MultiCell(rowspan, colspan), so a span copied across from table code comes out transposed. NewCell() is NewMultiCell(1, 1).

Rows that are already full

NewMultiCell returns an error when the cell would run past the last column, with the message can't add any more cells to this row. A three column grid holding one cell of colspan 3 is full, and the next NewCell on that row fails rather than wrapping onto the following row.

This is the useful half of the difference from Table. A table quietly pushes the overflow onto the next row, which shows up much later as a layout that is one row taller than intended. A grid tells you at the call site.

Run the example

The example makes a three column grid whose first row is a single spanning cell used as a title, followed by ordinary rows. AddLoremIpsumCell fills the body cells.

git clone https://github.com/unidoc/unipdf-examples.git
cd unipdf-examples/grid
go run pdf_grid_colspan.go

If this is your first time using UniPDF, follow the getting started guide to create an API key and set up your development environment.

View the full source

Sample output

Grid with column spans

Last updated on