Edit in GitHubLog an issue

Rotate Pages

Rotate a page in an existing document.

REST API

See our public API Reference for Rotate Pages.

Rotate Pages in PDF

The rotate pages operation selectively rotates pages in PDF file. For example, you can change portrait view to landscape view.

Please refer the API usage guide to understand how to use our APIs.

Copied to your clipboard
1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
2// Run the sample:
3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.rotatepages.RotatePDFPages
4
5 public class RotatePDFPages {
6
7 // Initialize the logger.
8 private static final Logger LOGGER = LoggerFactory.getLogger(RotatePDFPages.class);
9
10 public static void main(String[] args) {
11 try (InputStream inputStream = Files.newInputStream(new File("src/main/resources/rotatePagesInput.pdf").toPath())) {
12 // Initial setup, create credentials instance
13 Credentials credentials = new ServicePrincipalCredentials(
14 System.getenv("PDF_SERVICES_CLIENT_ID"),
15 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
16
17 // Creates a PDF Services instance
18 PDFServices pdfServices = new PDFServices(credentials);
19
20 // Creates an asset(s) from source file(s) and upload
21 Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.PDF.getMediaType());
22
23 // First set of page ranges for rotating the specified pages of the input PDF file.
24 PageRanges firstPageRange = getFirstPageRangeForRotation();
25
26 // Second set of page ranges for rotating the specified pages of the input PDF file.
27 PageRanges secondPageRange = getSecondPageRangeForRotation();
28
29 // Create parameters for the job
30 RotatePagesParams rotatePagesParams = RotatePagesParams.rotatePagesParamsBuilder()
31 .withAngleToRotatePagesBy(Angle._90, firstPageRange)
32 .withAngleToRotatePagesBy(Angle._180, secondPageRange)
33 .build();
34
35 // Creates a new job instance
36 RotatePagesJob rotatePagesJob = new RotatePagesJob(asset, rotatePagesParams);
37
38 // Submit the job and gets the job result
39 String location = pdfServices.submit(rotatePagesJob);
40 PDFServicesResponse<RotatePagesResult> pdfServicesResponse = pdfServices.getJobResult(location, RotatePagesResult.class);
41
42 // Get content from the resulting asset(s)
43 Asset resultAsset = pdfServicesResponse.getResult().getAsset();
44 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
45
46 // Creates an output stream and copy stream asset's content to it
47 Files.createDirectories(Paths.get("output/"));
48 OutputStream outputStream = Files.newOutputStream(new File("output/rotatePagesOutput.pdf").toPath());
49 LOGGER.info("Saving asset at output/rotatePagesOutput.pdf");
50 IOUtils.copy(streamAsset.getInputStream(), outputStream);
51 outputStream.close();
52 } catch (IOException | ServiceApiException | SDKException | ServiceUsageException e) {
53 LOGGER.error("Exception encountered while executing operation", e);
54 }
55 }
56
57 private static PageRanges getFirstPageRangeForRotation() {
58 // Specify pages for rotation
59 PageRanges firstPageRange = new PageRanges();
60 // Add page 1
61 firstPageRange.addSinglePage(1);
62
63 // Add pages 3 to 4
64 firstPageRange.addRange(3, 4);
65 return firstPageRange;
66 }
67
68 private static PageRanges getSecondPageRangeForRotation() {
69 // Specify pages for rotation
70 PageRanges secondPageRange = new PageRanges();
71 // Add page 2
72 secondPageRange.addSinglePage(2);
73
74 return secondPageRange;
75 }
76 }
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.