This guide will show you how to optimize a PDF document by using various options provided by UniPDF.
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.
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
compress folder in the unipdf-examples directory.
How it works
9-18 import the UniPDF packages and other required dependencies.
init function in lines
20-27 authenticates your request with your
The main function in lines
32-89 is just like any other function when we want to create a regular PDF file, one difference would be on line
66-74 where we use various options available to help reduce PDF document size.
Run the code
Run this command to add a line annotation to the specified page in a PDF. This will also get all the required dependencies to run the program.
go run pdf_optimize.go input.pdf output.pdf
Running the code example will show you some info on how much compression is made, similar like the following.
Original file: input.pdf
Original size: 266845 bytes
Optimized file: output.pdf
Optimized size: 207347 bytes
Compression ratio: 22.30%
Processing time: 53.17 ms