Presentation with Tables
This guide shows how to create a presentation with a table using UniOffice
.
Before you begin
You should get your API key from your UniCloud account.
If this is your first time using UniOffice 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/unioffice-examples
To get the example navigate to the path presentation/tables
folder in the unioffice-examples
directory.
cd unioffice-examples/presentation/tables/
How it works
The import
section in lines 4-12
, imports the necessary packages.
The init
function in lines 14-21
, sets the license key using license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
.
The main
function of the example code is defined in lines 21-60
. In lines 22-24
, a new presentation object is created, and a new slide is instantiated using:
ppt := presentation.New()
defer ppt.Close()
slide := ppt.AddSlide()
The defer
statement here makes sure that the presentation object is closed, and the temporary resources, if any, are cleared at the end. In line 26
, a new table is created using slide.AddTable()
. Then the columns are set using the for
loop in lines 27-30
. We can tell the number of columns intended to be created from the for loop range.
In lines 31-45
, the outer loop creates a table row and the inner loop populates the columns of each row with a text. In lines 47-58
, the styles of the table are set. Finally, the presentation is written to file in line 59
using ppt.SaveToFile("out.pptx")
.
Run the code
Run this command to generate the presentation file with a table.
go run main.go