Rotate PDF Pages

This guide shows you how to rotate all the pages in a PDF. The setRotation function will rotate all the pages in the PDF by a set angle in multiples of 90 degrees.

If you want to rotate a specific page in the PDF, see pdf_page_rotate.

To remove the rotation effect on a PDF and set its rotation to 0 degrees, see pdf_rotate_flatten.

Sample input

PDF to be rotated

Before you begin

You should get your API key from yourUniCloud 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

Change into the repository directory and navigate to the pages folder

cd unipdf-examples/pages

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

Lines 12-19 import the UniPDF packages and other required dependencies.

Lines 21-28 authenticate your request with your UNIDOC_LICENSE_API_KEY with the init function.

Lines 30-56 create the main function that accepts three arguments: the input path to the file to be rotated, the angle to rotate the PDF you want the cropped PDF and the output path to the rotated PDF. The main function passes the arguments as inputs to the rotatePDF function.

The rotatePDF function in lines 59-80 read the PDF from the input path and rotate all its pages with the setRotation function and save the rotated PDF to the output path.

Run the code

Run this command to rotate the PDF. This will also get all the required dependencies to run the program.

# angle is set to 270
go run pdf_rotate.go input.pdf <angle> output.pdf

Sample output

The PDF is rotated at your specified angle degrees away from its initial location.

Rotated PDF

Got any Questions?

We're here to help you.