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.
Java
.NET
Node JS
REST API
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples2// Run the sample:3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.reorderpages.ReorderPDFPages45 public class ReorderPDFPages {67 // Initialize the logger.8 private static final Logger LOGGER = LoggerFactory.getLogger(ReorderPDFPages.class);910 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 instance13 Credentials credentials = new ServicePrincipalCredentials(14 System.getenv("PDF_SERVICES_CLIENT_ID"),15 System.getenv("PDF_SERVICES_CLIENT_SECRET"));1617 // Creates a PDF Services instance18 PDFServices pdfServices = new PDFServices(credentials);1920 // Creates an asset(s) from source file(s) and upload21 Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.PDF.getMediaType());2223 PageRanges pagesToReorder = getPageRangeForReorder();2425 // Create parameters for the job26 ReorderPagesParams reorderPagesParams = ReorderPagesParams27 .reorderPagesParamsBuilder(asset, pagesToReorder) // Add the asset as input to the params, along with its page order28 .build();2930 // Creates a new job instance31 ReorderPagesPDFJob reorderPagesPDFJob = new ReorderPagesPDFJob(reorderPagesParams);3233 // Submit the job and gets the job result34 String location = pdfServices.submit(reorderPagesPDFJob);35 PDFServicesResponse<ReorderPagesResult> pdfServicesResponse = pdfServices.getJobResult(location, ReorderPagesResult.class);3637 // Get content from the resulting asset(s)38 Asset resultAsset = pdfServicesResponse.getResult().getAsset();39 StreamAsset streamAsset = pdfServices.getContent(resultAsset);4041 // Creates an output stream and copy stream asset's content to it42 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 }5152 private static PageRanges getPageRangeForReorder() {53 // Specify order of the pages for an output document54 PageRanges pageRanges = new PageRanges();55 // Add pages 3 to 456 pageRanges.addRange(3, 4);5758 // Add page 159 pageRanges.addSinglePage(1);6061 return pageRanges;62 }63 }
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_net_samples2// Run the sample:3// cd ReorderPages/4// dotnet run ReorderPDFPages.csproj56 namespace ReorderPDFPages7 {8 class Program9 {10 private static readonly ILog log = LogManager.GetLogger(typeof(Program));11 static void Main()12 {13 // Configure the logging14 ConfigureLogging();15 try16 {17 // Initial setup, create credentials instance.18 Credentials credentials = Credentials.ServicePrincipalCredentialsBuilder()19 .WithClientId("PDF_SERVICES_CLIENT_ID")20 .WithClientSecret("PDF_SERVICES_CLIENT_SECRET")21 .Build();2223 // Create an ExecutionContext using credentials.24 ExecutionContext executionContext = ExecutionContext.Create(credentials);2526 // Create a new operation instance27 ReorderPagesOperation reorderPagesOperation = ReorderPagesOperation.CreateNew();2829 // Set operation input from a source file, along with specifying the order of the pages for30 // rearranging the pages in a PDF file.31 FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"reorderPagesInput.pdf");32 reorderPagesOperation.SetInput(sourceFileRef);33 PageRanges pageRanges = GetPageRangeForReorder();34 reorderPagesOperation.SetPagesOrder(pageRanges);3536 // Execute the operation.37 FileRef result = reorderPagesOperation.Execute(executionContext);3839 // Save the result to the specified location.40 result.SaveAs(Directory.GetCurrentDirectory() + "/output/reorderPagesOutput.pdf");41 }42 catch (ServiceUsageException ex)43 {44 log.Error("Exception encountered while executing operation", ex);45 }46 // Catch more errors here . . .47 }4849 private static PageRanges GetPageRangeForReorder()50 {51 // Specify order of the pages for an output document.52 PageRanges pageRanges = new PageRanges();53 // Add pages 3 to 4.54 pageRanges.AddRange(3, 4);5556 // Add page 1.57 pageRanges.AddSinglePage(1);5859 return pageRanges;60 }6162 static void ConfigureLogging()63 {64 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());65 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));66 }67 }68 }
Copied to your clipboard1// Get the samples from http://www.adobe.com/go/pdftoolsapi_node_sample2// Run the sample:3// node src/reorderpages/reorder-pdf-pages.js45const {6 ServicePrincipalCredentials,7 PDFServices,8 MimeType,9 PageRanges,10 ReorderPagesParams,11 ReorderPagesJob,12 ReorderPagesResult,13 SDKError,14 ServiceUsageError,15 ServiceApiError16} = require("@adobe/pdfservices-node-sdk");17const fs = require("fs");1819(async () => {20 let readStream;21 try {22 // Initial setup, create credentials instance23 const credentials = new ServicePrincipalCredentials({24 clientId: process.env.PDF_SERVICES_CLIENT_ID,25 clientSecret: process.env.PDF_SERVICES_CLIENT_SECRET26 });2728 // Creates a PDF Services instance29 const pdfServices = new PDFServices({credentials});3031 // Creates an asset(s) from source file(s) and upload32 readStream = fs.createReadStream("./reorderPagesInput.pdf");33 const inputAsset = await pdfServices.upload({34 readStream,35 mimeType: MimeType.PDF36 });3738 // Create parameters for the job39 // Add the asset as input to the params, along with its page order40 const params = new ReorderPagesParams({41 asset: inputAsset,42 pageRanges: getPageRangeForReorder()43 });4445 // Creates a new job instance46 const job = new ReorderPagesJob({params});4748 // Submit the job and get the job result49 const pollingURL = await pdfServices.submit({job});50 const pdfServicesResponse = await pdfServices.getJobResult({51 pollingURL,52 resultType: ReorderPagesResult53 });5455 // Get content from the resulting asset(s)56 const resultAsset = pdfServicesResponse.result.asset;57 const streamAsset = await pdfServices.getContent({asset: resultAsset});5859 // Creates an output stream and copy result asset's content to it60 const outputFilePath = "./reorderPagesOutput.pdf";61 console.log(`Saving asset at ${outputFilePath}`);6263 const outputStream = fs.createWriteStream(outputFilePath);64 streamAsset.readStream.pipe(outputStream);65 } catch (err) {66 if (err instanceof SDKError || err instanceof ServiceUsageError || err instanceof ServiceApiError) {67 console.log("Exception encountered while executing operation", err);68 } else {69 console.log("Exception encountered while executing operation", err);70 }71 } finally {72 readStream?.destroy();73 }74})();7576function getPageRangeForReorder() {77 // Specify order of the pages for an output document78 const pageRanges = new PageRanges();79 // Add pages 3 to 480 pageRanges.addRange(3, 4);81 // Add page 182 pageRanges.addSinglePage(1);83 return pageRanges;84}
Copied to your clipboard1// Please refer our REST API docs for more information2// https://developer.adobe.com/document-services/docs/apis/#tag/Combine-PDF34curl --location --request POST 'https://pdf-services.adobe.io/operation/combinepdf' \5--header 'x-api-key: {{Placeholder for client_id}}' \6--header 'Content-Type: application/json' \7--header 'Authorization: Bearer {{Placeholder for token}}' \8--data-raw '{9 "assets": [10 {11 "assetID": "urn:aaid:AS:UE1:23c30ee0-2e4d-46d6-87f2-087832fca718",12 "pageRanges": [13 {14 "start": 3,15 "end": 316 },17 {18 "start": 1,19 "end": 120 },21 {22 "start": 4,23 "end": 424 }25 ]26 }27 ]28}'