How to rotate all pages in a PDF document in Java

Cloudmersive
2 min readFeb 14, 2020

--

Having trouble with PDF page rotation? Don’t worry, this API demonstration will have you squared away in just a few minutes.

To get things rolling, we need to install our library. This requires a pair of references in our POM file.

First our repositories reference.

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

And now our dependencies reference.

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.34</version>
</dependency>
</dependencies>

Now we can call editPdfRotateAllPages and input our desired angle for rotation in increments of 90 degrees.

// Import classes://import com.cloudmersive.client.invoker.ApiClient;//import com.cloudmersive.client.invoker.ApiException;//import com.cloudmersive.client.invoker.Configuration;//import com.cloudmersive.client.invoker.auth.*;//import com.cloudmersive.client.EditPdfApi;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: ApikeyApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");Apikey.setApiKey("YOUR API KEY");// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.setApiKeyPrefix("Token");EditPdfApi apiInstance = new EditPdfApi();File inputFile = new File("/path/to/file"); // File | Input file to perform the operation on.Integer rotationAngle = 56; // Integer | The angle to rotate the page in degrees, must be a multiple of 90 degrees, e.g. 90, 180, 270, or -90, -180, -270, etc.try {byte[] result = apiInstance.editPdfRotateAllPages(inputFile, rotationAngle);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling EditPdfApi#editPdfRotateAllPages");e.printStackTrace();}

Easy. Now you can enjoy all that time you just saved. For more easy solutions like this, you might like to read some of the other entries in this series, for all your Java quick solutions.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet