Font Subsetting

This guide will show you how to optimize a PDF document by using only a subset of a font file to reduce the PDF document size.

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.

cd unipdf-examples/compress

How it works

Lines 13-22 import the UniPDF packages and other required dependencies.

The init function in lines 24-31 authenticates your request with your UNIDOC_LICENSE_API_KEY.

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 70-72 where we declaring an options to enable font subsetting for all the font files used in the document.

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_annotate_add_line.go input.pdf output.pdf

Sample output

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: 124868 bytes
Compression ratio: 53.21%
Processing time: 366.40 ms

Got any Questions?

We're here to help you.