Styling

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 import section in lines 9-17 imports the necessary packages and libraries. The init function in lines 19-26 loads you metered license key and authenticates your request.

The main function in lines 28-53 which is the entry point to the example code calls stylingContent function to create the document and writes it to file using c.WriteToFile("unipdf-style-tables.pdf") in line 50.

In lines 55-75 the stylingContent function is defined where the styled table is drawn. Here a new page is created using c.NewPage() and content with borders and content with background are created by calling contentBorders and contentBackground functions respectively. Finally the content which is inside a chapter object drawn using c.Draw(ch).

The function contentBorders in lines 77-141 creates a table with different content borders. Cell borders are set using cell.SetBorder method. The color and line style of the border are set using SetBorderColor and SetBorderLineStyle methods. The SetBorder method of a tableCell takes two arguments, which determine the side where the border should be added and the style of the border added. The border side could be either of the following values. CellBorderSideLeft, CellBorderSideRight,CellBorderSideTop, CellBorderSideBottom or CellBorderSideAll.

And the border style could be CellBorderStyleNone,CellBorderStyleSingle or CellBorderStyleDouble.

A content with background is created using the function contentBackground. The background color of cell is set using cell.SetBackgroundColor(bgColor) which is done in line 171.

Run the code

go run pdf_tables_styling.go

Sample output

PDF table styling

Got any Questions?

We're here to help you.