Edit in GitHubLog an issue

Reorder Pages

Reorder the pages of a PDF file to reorganize.

Rest API

See our public API Reference for Reorder Pages

Reorder Pages in PDF

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

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.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.servicePrincipalCredentialsBuilder()
14 .withClientId("PDF_SERVICES_CLIENT_ID")
15 .withClientSecret("PDF_SERVICES_CLIENT_SECRET")
16 .build();
17
18 // Create an ExecutionContext using credentials and create a new operation instance.
19 ExecutionContext executionContext = ExecutionContext.create(credentials);
20 ReorderPagesOperation reorderPagesOperation = ReorderPagesOperation.createNew();
21
22 // Set operation input from a source file, along with specifying the order of the pages for
23 // rearranging the pages in a PDF file.
24 FileRef source = FileRef.createFromLocalFile("src/main/resources/reorderPagesInput.pdf");
25 PageRanges pageRanges = getPageRangeForReorder();
26 reorderPagesOperation.setInput(source);
27 reorderPagesOperation.setPagesOrder(pageRanges);
28
29 // Execute the operation.
30 FileRef result = reorderPagesOperation.execute(executionContext);
31
32 // Save the result to the specified location.
33 result.saveAs("output/reorderPagesOutput.pdf");
34
35 } catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {
36 LOGGER.error("Exception encountered while executing operation", e);
37 }
38 }
39
40 private static PageRanges getPageRangeForReorder() {
41 // Specify order of the pages for an output document.
42 PageRanges pageRanges = new PageRanges();
43 // Add pages 3 to 4.
44 pageRanges.addRange(3, 4);
45
46 // Add page 1.
47 pageRanges.addSinglePage(1);
48
49 return pageRanges;
50 }
51 }
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2023 Adobe. All rights reserved.