Add Watermark

This guide explains how to add watermark to pdf documents.

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

Navigate to the image folder in the unipdf-examples directory.

cd unipdf-examples/image

Configure environment variables

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





How it works

In the above example code line 7 the package name is defined.

In the import section in lines 9-17 imports unipdf packages and other necessary library. The init section loads your metered License API key and authenticates your request to the unipdf library.

The main function in lines 28-45 reads the values from the environment variables and calls addWatermarkImage function with the necessary parameters to add the watermark.

The addWatermarkImage function defined in lines 48-102 is the responsible function for adding the watermark to the pdf document. In this function in line 54 a new creator.Image object is created from the given watermark image path. The loop in lines 76-92 iterates through the number of pages, gets the corresponding page from the given pdf file, edits the page and adds it to the creator object. The code c.Draw(watermarkImg) in line 91 is where the watermark image is added to the page by calling the Draw method on creator object. Finally the document is written to pdf file using c.WriteToFile(outputPath) in line 100.

Run the code

Run the code using the following command. The necessary files should be given as environment variables as shown in the sample command.

go run pdf_watermark_image.go input.pdf watermark.jpg output.pdf

Got any Questions?

We're here to help you.