How to Change a PDF Document’s Paper Size in Java
If you’re familiar with the ins and outs of PDF formatting, you’re probably aware that there are a variety of paper sizes to choose from. Which size you choose has an impact on the type of project/contents that a PDF document can comfortably hold; on one end of the spectrum, the smallest option is A7, which is effectively the size of a large post-it note, and on the other end of the spectrum, the largest option is A0, perfect for displaying posters and drawings. With our PDF paper size API, you can easily edit the paper size of your PDFs at scale by simply specifying a string (A7 — A0) along with your input file. You can easily take advantage of this API using ready-to-run Java code examples provided below.
Your first step is to install the SDK; you can do that by adding a reference to the pom.xml repository:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
And then adding one to the pom.xml dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
After that, you can call the API. Include the imports at the top of your file, and authenticate your API key in the following snippet (where indicated). If you don’t have an API key, just visit our website and register a free account.
// 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: 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.
String paperSize = "paperSize_example"; // String | The desired paper size for the resized PDF document. Size ranges from A7 (smallest) to A0 (largest).
try {
byte[] result = apiInstance.editPdfResize(inputFile, paperSize);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditPdfApi#editPdfResize");
e.printStackTrace();
}
And that’s all you need. Nice and simple!