Draw Shapes

This guide will show you how to draw different shapes on pdf files.

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 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 drawing folder in the unipdf-examples directory.

cd unipdf-examples/drawing 

Configure environment variables

Replace the UNIDOC_LICENSE_API_KEY with your API credentials from your UniCloud account.





How it works

Shapes in pdf are drawn using the provided methods of the creator object.

The following example code shows how different shapes are drawn using the creator package.

Lines 9-15 import necessary unipdf packages and other required libraries.

The init function in lines 18-25 authenticates your request with UNIDOC_LICENSE_API_KEY.

Lines 27-87 contains the main function that calls each function for drawing different shapes and handles respective errors returned by the functions.

Inside the main function the front page of the document is generated using CreateFrontPage function.

Lines 89-97 defines a drawHeader function which is used for creating the header text displayed above each shape.

Lines 100-110 defines drawLine function for drawing a single line.

Lines 113-128 defines drawPolyLine function for drawing line based polygon.

Lines 131-146 defines drawRectangle function for drawing a single rectangle shape.

Lines 148-158 defines drawEllipse function for drawing ellipse.

In Lines 161-248 the functions for drawing curve, Bezier curve,curve polygon and polygon shapes are defined.

Run the code

go run pdf_draw_shapes.go

Sample output

Shapes PDF

Got any Questions?

We're here to help you.