How to Rotate All Pages in a PDF Document using Java
Using our suite of PDF APIs, you can empower your application to handle basic PDF administration tasks quickly and easily without having to write all the code yourself. In this article, I’m going to highlight one such API you can use to rotate pages within a PDF document by a multiple of 90 degrees. It’s a simple, easy service to include in your app using ready-to-run code snippets to structure our API call in Java. I’ve included instructions below to help you get set up without a hassle.
First things first — let’s install Maven. Add a reference to the pom.xml repository:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Next up, let’s add a reference to the pom.xml dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Now it’s time to add the import classes:
// 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;
And after that, you can make your API call using the below code block. Make sure to setttle the following parameters:
- Your input PDF file
- The angle you want to rotate your page (must be a multiple of 90 degrees)
- Your Cloudmersive API key (obtainable by registering a free account on our website)
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth 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/inputfile"); // 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();
}