Edit in GitHubLog an issue

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.

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 {
12 // Initial setup, create credentials instance.
13 Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
14 .fromFile("pdfservices-api-credentials.json")
15 .build();
16
17 // Create an ExecutionContext using credentials and create a new operation instance.
18 ExecutionContext executionContext = ExecutionContext.create(credentials);
19 RotatePagesOperation rotatePagesOperation = RotatePagesOperation.createNew();
20
21 // Set operation input from a source file.
22 FileRef source = FileRef.createFromLocalFile("src/main/resources/rotatePagesInput.pdf");
23 rotatePagesOperation.setInput(source);
24
25 // Sets angle by 90 degrees (in clockwise direction) for rotating the specified pages of
26 // the input PDF file.
27 PageRanges firstPageRange = getFirstPageRangeForRotation();
28 rotatePagesOperation.setAngleToRotatePagesBy(Angle._90, firstPageRange);
29
30 // Sets angle by 180 degrees (in clockwise direction) for rotating the specified pages of
31 // the input PDF file.
32 PageRanges secondPageRange = getSecondPageRangeForRotation();
33 rotatePagesOperation.setAngleToRotatePagesBy(Angle._180, secondPageRange);
34
35 // Execute the operation.
36 FileRef result = rotatePagesOperation.execute(executionContext);
37
38 // Save the result to the specified location.
39 result.saveAs("output/rotatePagesOutput.pdf");
40
41 } catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {
42 LOGGER.error("Exception encountered while executing operation", e);
43 }
44 }
45
46 private static PageRanges getFirstPageRangeForRotation() {
47 // Specify pages for rotation.
48 PageRanges firstPageRange = new PageRanges();
49 // Add page 1.
50 firstPageRange.addSinglePage(1);
51
52 // Add pages 3 to 4.
53 firstPageRange.addRange(3, 4);
54 return firstPageRange;
55 }
56
57 private static PageRanges getSecondPageRangeForRotation() {
58 // Specify pages for rotation.
59 PageRanges secondPageRange = new PageRanges();
60 // Add page 2.
61 secondPageRange.addSinglePage(2);
62
63 return secondPageRange;
64 }
65 }
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell my personal information
  • AdChoices
Copyright © 2022 Adobe. All rights reserved.