Edit in GitHubLog an issue

Reorder Pages

Reorder Pages in PDF#

The reorder pages operation moves pages from one location to another in a PDF file.

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.reorderpages.ReorderPDFPages
4
5 public class ReorderPDFPages {
6
7 // Initialize the logger.
8 private static final Logger LOGGER = LoggerFactory.getLogger(ReorderPDFPages.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 ReorderPagesOperation reorderPagesOperation = ReorderPagesOperation.createNew();
20
21 // Set operation input from a source file, along with specifying the order of the pages for
22 // rearranging the pages in a PDF file.
23 FileRef source = FileRef.createFromLocalFile("src/main/resources/reorderPagesInput.pdf");
24 PageRanges pageRanges = getPageRangeForReorder();
25 reorderPagesOperation.setInput(source);
26 reorderPagesOperation.setPagesOrder(pageRanges);
27
28 // Execute the operation.
29 FileRef result = reorderPagesOperation.execute(executionContext);
30
31 // Save the result to the specified location.
32 result.saveAs("output/reorderPagesOutput.pdf");
33
34 } catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {
35 LOGGER.error("Exception encountered while executing operation", e);
36 }
37 }
38
39 private static PageRanges getPageRangeForReorder() {
40 // Specify order of the pages for an output document.
41 PageRanges pageRanges = new PageRanges();
42 // Add pages 3 to 4.
43 pageRanges.addRange(3, 4);
44
45 // Add page 1.
46 pageRanges.addSinglePage(1);
47
48 return pageRanges;
49 }
50 }
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell my personal information
  • AdChoices
Copyright © 2022 Adobe. All rights reserved.