Reorder Pages
Reorder Pages in PDF
The reorder pages operation moves pages from one location to another in a PDF file.
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 {12 // Initial setup, create credentials instance.13 Credentials credentials = Credentials.serviceAccountCredentialsBuilder()14 .fromFile("pdfservices-api-credentials.json")15 .build();1617 // Create an ExecutionContext using credentials and create a new operation instance.18 ExecutionContext executionContext = ExecutionContext.create(credentials);19 ReorderPagesOperation reorderPagesOperation = ReorderPagesOperation.createNew();2021 // Set operation input from a source file, along with specifying the order of the pages for22 // 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);2728 // Execute the operation.29 FileRef result = reorderPagesOperation.execute(executionContext);3031 // Save the result to the specified location.32 result.saveAs("output/reorderPagesOutput.pdf");3334 } catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {35 LOGGER.error("Exception encountered while executing operation", e);36 }37 }3839 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);4445 // Add page 1.46 pageRanges.addSinglePage(1);4748 return pageRanges;49 }50 }
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.ServiceAccountCredentialsBuilder()19 .FromFile(Directory.GetCurrentDirectory() + "/pdfservices-api-credentials.json")20 .Build();2122 // Create an ExecutionContext using credentials.23 ExecutionContext executionContext = ExecutionContext.Create(credentials);2425 // Create a new operation instance26 ReorderPagesOperation reorderPagesOperation = ReorderPagesOperation.CreateNew();2728 // Set operation input from a source file, along with specifying the order of the pages for29 // rearranging the pages in a PDF file.30 FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"reorderPagesInput.pdf");31 reorderPagesOperation.SetInput(sourceFileRef);32 PageRanges pageRanges = GetPageRangeForReorder();33 reorderPagesOperation.SetPagesOrder(pageRanges);3435 // Execute the operation.36 FileRef result = reorderPagesOperation.Execute(executionContext);3738 // Save the result to the specified location.39 result.SaveAs(Directory.GetCurrentDirectory() + "/output/reorderPagesOutput.pdf");40 }41 catch (ServiceUsageException ex)42 {43 log.Error("Exception encountered while executing operation", ex);44 }45 // Catch more errors here . . .46 }4748 private static PageRanges GetPageRangeForReorder()49 {50 // Specify order of the pages for an output document.51 PageRanges pageRanges = new PageRanges();52 // Add pages 3 to 4.53 pageRanges.AddRange(3, 4);5455 // Add page 1.56 pageRanges.AddSinglePage(1);5758 return pageRanges;59 }6061 static void ConfigureLogging()62 {63 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());64 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));65 }66 }67 }
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.js45 const PDFServicesSdk = require('@adobe/pdfservices-node-sdk');67 const getPageRangeForReorder = () => {8 // Specify order of the pages for an output document.9 const pageRanges = new PDFServicesSdk.PageRanges();1011 // Add pages 3 to 4.12 pageRanges.addPageRange(3, 4);1314 // Add page 1.15 pageRanges.addSinglePage(1);1617 return pageRanges;18 };19 try {20 // Initial setup, create credentials instance.21 const credentials = PDFServicesSdk.Credentials22 .serviceAccountCredentialsBuilder()23 .fromFile("pdfservices-api-credentials.json")24 .build();2526 // Create an ExecutionContext using credentials and create a new operation instance.27 const executionContext = PDFServicesSdk.ExecutionContext.create(credentials),28 reorderPagesOperation = PDFServicesSdk.ReorderPages.Operation.createNew();2930 // Set operation input from a source file, along with specifying the order of the pages for31 // rearranging the pages in a PDF file.32 const input = PDFServicesSdk.FileRef.createFromLocalFile('resources/reorderPagesInput.pdf');33 const pageRanges = getPageRangeForReorder();34 reorderPagesOperation.setInput(input);35 reorderPagesOperation.setPagesOrder(pageRanges);3637 // Execute the operation and Save the result to the specified location.38 reorderPagesOperation.execute(executionContext)39 .then(result => result.saveAsFile('output/reorderPagesOutput.pdf'))40 .catch(err => {41 if(err instanceof PDFServicesSdk.Error.ServiceApiError42 || err instanceof PDFServicesSdk.Error.ServiceUsageError) {43 console.log('Exception encountered while executing operation', err);44 } else {45 console.log('Exception encountered while executing operation', err);46 }47 });48 } catch (err) {49 console.log('Exception encountered while executing operation', err);50 }
Copied to your clipboard1// Please refer our REST API docs for more information2// https://documentcloud.adobe.com/document-services/index.html#post-combinePDF34curl --location --request POST 'https://cpf-ue1.adobe.io/ops/:create?respondWith=%7B%22reltype%22%3A%20%22http%3A%2F%2Fns.adobe.com%2Frel%2Fprimary%22%7D' \5--header 'Authorization: Bearer {{Placeholder for token}}' \6--header 'Accept: application/json, text/plain, */*' \7--header 'x-api-key: {{Placeholder for client_id}}' \8--header 'Prefer: respond-async,wait=0' \9--form 'contentAnalyzerRequests="{10 \"cpf:inputs\": {11 \"documentsIn\": [{12 \"pageRanges\": {13 \"cpf:inline\": [{14 \"start\": 1,15 \"end\": 416 }]17 },18 \"documentIn\": {19 \"cpf:location\": \"InputFile0\",20 \"dc:format\": \"application/pdf\"21 }22 }, {23 \"pageRanges\": {24 \"cpf:inline\": [{25 \"start\": 1,26 \"end\": 2527 }]28 },29 \"documentIn\": {30 \"cpf:location\": \"InputFile1\",31 \"dc:format\": \"application/pdf\"32 }33 }, {34 \"pageRanges\": {35 \"cpf:inline\": [{36 \"start\": 137 }, {38 \"end\": 2539 }, {40 \"start\": 1,41 \"end\": 2542 }]43 },44 \"documentIn\": {45 \"cpf:location\": \"InputFile2\",46 \"dc:format\": \"application/pdf\"47 }48 }]49 },50 \"cpf:engine\": {51 \"repo:assetId\": \"urn:aaid:cpf:Service-916ee91c156b42349a7847a7d564fb13\"52 },53 \"cpf:outputs\": {54 \"documentOut\": {55 \"cpf:location\": \"OutputFile\",56 \"dc:format\": \"application/pdf\"57 }58 }59}"' \60--form 'InputFile0=@"{{Placeholder for input file (absolute path)}}"' \61--form 'InputFile1=@"{{Placeholder for input file (absolute path)}}"' \62--form 'InputFile2=@"{{Placeholder for input file (absolute path)}}"'