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
// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
// Run the sample:
// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.reorderpages.ReorderPDFPages
public class ReorderPDFPages {
// Initialize the logger.
private static final Logger LOGGER = LoggerFactory.getLogger(ReorderPDFPages.class);
public static void main(String[] args) {
try (InputStream inputStream = Files.newInputStream(new File("src/main/resources/reorderPagesInput.pdf").toPath())) {
// Initial setup, create credentials instance
Credentials credentials = new ServicePrincipalCredentials(
System.getenv("PDF_SERVICES_CLIENT_ID"),
System.getenv("PDF_SERVICES_CLIENT_SECRET"));
// Creates a PDF Services instance
PDFServices pdfServices = new PDFServices(credentials);
// Creates an asset(s) from source file(s) and upload
Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.PDF.getMediaType());
PageRanges pagesToReorder = getPageRangeForReorder();
// Create parameters for the job
ReorderPagesParams reorderPagesParams = ReorderPagesParams
.reorderPagesParamsBuilder(asset, pagesToReorder) // Add the asset as input to the params, along with its page order
.build();
// Creates a new job instance
ReorderPagesPDFJob reorderPagesPDFJob = new ReorderPagesPDFJob(reorderPagesParams);
// Submit the job and gets the job result
String location = pdfServices.submit(reorderPagesPDFJob);
PDFServicesResponse<ReorderPagesResult> pdfServicesResponse = pdfServices.getJobResult(location, ReorderPagesResult.class);
// Get content from the resulting asset(s)
Asset resultAsset = pdfServicesResponse.getResult().getAsset();
StreamAsset streamAsset = pdfServices.getContent(resultAsset);
// Creates an output stream and copy stream asset's content to it
Files.createDirectories(Paths.get("output/"));
OutputStream outputStream = Files.newOutputStream(new File("output/reorderPagesOutput.pdf").toPath());
LOGGER.info("Saving asset at output/reorderPagesOutput.pdf");
IOUtils.copy(streamAsset.getInputStream(), outputStream);
outputStream.close();
} catch (IOException | ServiceApiException | SDKException | ServiceUsageException e) {
LOGGER.error("Exception encountered while executing operation", e);
}
}
private static PageRanges getPageRangeForReorder() {
// Specify order of the pages for an output document
PageRanges pageRanges = new PageRanges();
// Add pages 3 to 4
pageRanges.addRange(3, 4);
// Add page 1
pageRanges.addSinglePage(1);
return pageRanges;
}
}
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.