Remove Image Watermark

This guide will show you how to remove image watermarks from a PDF document using unipdf.

Before you begin

Before you begin this guide 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 images folder in the unipdf-examples directory.

cd unipdf-examples/images

Configure environment variables

Replace the UNIDOC_LICENSE_API_KEY with your 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-15 imports necessary libraries for license validation, file handling, and PDF manipulation.

The init function defined in lines 17-24 authenticates the library request using a metered API key.

The main function defined in lines 26-81 performs the main operations of removing a watermark image from a PDF file. In this function, In Lines 27-30 the command-line arguments are validated to ensure the both the input and output file paths are provided. Then lines 32-33 sets the inputPath and outputPath variables. Line 35 creates a PDF reader object using model.NewPdfReaderFromFile(inputPath, nil).

Line 42 gets the number of pages using the PdffReader object by calling pdfReader.GetNumPages() method. Lines 48-65 iterate through each page perform the removing of the watermark image as follows.

for i := 0; i < numPages; i++ {
	pageNum := i + 1

	// Read the page.
	page, err := pdfReader.GetPage(pageNum)
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		os.Exit(1)
	}

	// For watermarks applied by Adobe, just pass nil as argument.
	// For custom watermarks use pdf_list_images.go to figure out the name of watermark object.
	err = page.RemoveWatermarkImage("Img1")
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		os.Exit(1)
	}
}

First it gets the corresponding Page object for the specific page number in line 52. Then in line 60 it performs the watermark removal by calling the RemoveWatermarkImage method and providing an image name used for water mark. This image name can be obtained by running pdf_list_images. This process Sis described in this guide. For watermarks created by Adobe, a nil can be passed as an argument.

After iterating through all pages, the PDFReader object is converted to PDFWriter in line 68. Then in line 74, the modified document is written to file using WriteToFile method of the writer object. A confirmation message is then printed in line 80 to show that everything has gone well.

Run the code

To run the code use the following command.

go run pdf_remove_watermark_image.go input.pdf output.pdf

Got any Questions?

We're here to help you.