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 (InputStream inputStream = Files.newInputStream(new File("src/main/resources/reorderPagesInput.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 PageRanges pagesToReorder = getPageRangeForReorder();
24
25 // Create parameters for the job
26 ReorderPagesParams reorderPagesParams = ReorderPagesParams
27 .reorderPagesParamsBuilder(asset, pagesToReorder) // Add the asset as input to the params, along with its page order
28 .build();
29
30 // Creates a new job instance
31 ReorderPagesPDFJob reorderPagesPDFJob = new ReorderPagesPDFJob(reorderPagesParams);
32
33 // Submit the job and gets the job result
34 String location = pdfServices.submit(reorderPagesPDFJob);
35 PDFServicesResponse<ReorderPagesResult> pdfServicesResponse = pdfServices.getJobResult(location, ReorderPagesResult.class);
36
37 // Get content from the resulting asset(s)
38 Asset resultAsset = pdfServicesResponse.getResult().getAsset();
39 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
40
41 // Creates an output stream and copy stream asset's content to it
42 Files.createDirectories(Paths.get("output/"));
43 OutputStream outputStream = Files.newOutputStream(new File("output/reorderPagesOutput.pdf").toPath());
44 LOGGER.info("Saving asset at output/reorderPagesOutput.pdf");
45 IOUtils.copy(streamAsset.getInputStream(), outputStream);
46 outputStream.close();
47 } catch (IOException | ServiceApiException | SDKException | ServiceUsageException e) {
48 LOGGER.error("Exception encountered while executing operation", e);
49 }
50 }
51
52 private static PageRanges getPageRangeForReorder() {
53 // Specify order of the pages for an output document
54 PageRanges pageRanges = new PageRanges();
55 // Add pages 3 to 4
56 pageRanges.addRange(3, 4);
57
58 // Add page 1
59 pageRanges.addSinglePage(1);
60
61 return pageRanges;
62 }
63 }
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.