Rotate Pages
Rotate Pages in PDF
The rotate pages operation selectively rotates pages in PDF file. For example, you can change portrait view to landscape view.
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.rotatepages.RotatePDFPages45 public class RotatePDFPages {67 // Initialize the logger.8 private static final Logger LOGGER = LoggerFactory.getLogger(RotatePDFPages.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 RotatePagesOperation rotatePagesOperation = RotatePagesOperation.createNew();2021 // Set operation input from a source file.22 FileRef source = FileRef.createFromLocalFile("src/main/resources/rotatePagesInput.pdf");23 rotatePagesOperation.setInput(source);2425 // Sets angle by 90 degrees (in clockwise direction) for rotating the specified pages of26 // the input PDF file.27 PageRanges firstPageRange = getFirstPageRangeForRotation();28 rotatePagesOperation.setAngleToRotatePagesBy(Angle._90, firstPageRange);2930 // Sets angle by 180 degrees (in clockwise direction) for rotating the specified pages of31 // the input PDF file.32 PageRanges secondPageRange = getSecondPageRangeForRotation();33 rotatePagesOperation.setAngleToRotatePagesBy(Angle._180, secondPageRange);3435 // Execute the operation.36 FileRef result = rotatePagesOperation.execute(executionContext);3738 // Save the result to the specified location.39 result.saveAs("output/rotatePagesOutput.pdf");4041 } catch (IOException | ServiceApiException | SdkException | ServiceUsageException e) {42 LOGGER.error("Exception encountered while executing operation", e);43 }44 }4546 private static PageRanges getFirstPageRangeForRotation() {47 // Specify pages for rotation.48 PageRanges firstPageRange = new PageRanges();49 // Add page 1.50 firstPageRange.addSinglePage(1);5152 // Add pages 3 to 4.53 firstPageRange.addRange(3, 4);54 return firstPageRange;55 }5657 private static PageRanges getSecondPageRangeForRotation() {58 // Specify pages for rotation.59 PageRanges secondPageRange = new PageRanges();60 // Add page 2.61 secondPageRange.addSinglePage(2);6263 return secondPageRange;64 }65 }
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_net_samples2// Run the sample:3// cd RotatePDFPages/4// dotnet run RotatePDFPages.csproj56 namespace RotatePDFPages7 {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 RotatePagesOperation rotatePagesOperation = RotatePagesOperation.CreateNew();2728 // Set operation input from a source file.29 FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"rotatePagesInput.pdf");30 rotatePagesOperation.SetInput(sourceFileRef);3132 // Sets angle by 90 degrees (in clockwise direction) for rotating the specified pages of33 // the input PDF file.34 PageRanges firstPageRange = GetFirstPageRangeForRotation();35 rotatePagesOperation.SetAngleToRotatePagesBy(Angle._90, firstPageRange);3637 // Sets angle by 180 degrees (in clockwise direction) for rotating the specified pages of38 // the input PDF file.39 PageRanges secondPageRange = GetSecondPageRangeForRotation();40 rotatePagesOperation.SetAngleToRotatePagesBy(Angle._180, secondPageRange);4142 // Execute the operation.43 FileRef result = rotatePagesOperation.Execute(executionContext);4445 // Save the result to the specified location.46 result.SaveAs(Directory.GetCurrentDirectory() + "/output/rotatePagesOutput.pdf");47 }48 catch (ServiceUsageException ex)49 {50 log.Error("Exception encountered while executing operation", ex);51 }52 // Catch more errors here . . .53 }5455 static void ConfigureLogging()56 {57 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());58 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));59 }6061 private static PageRanges GetFirstPageRangeForRotation()62 {63 // Specify pages for rotation.64 PageRanges firstPageRange = new PageRanges();65 // Add page 1.66 firstPageRange.AddSinglePage(1);6768 // Add pages 3 to 4.69 firstPageRange.AddRange(3, 4);70 return firstPageRange;71 }7273 private static PageRanges GetSecondPageRangeForRotation()74 {75 // Specify pages for rotation.76 PageRanges secondPageRange = new PageRanges();77 // Add page 2.78 secondPageRange.AddSinglePage(2);7980 return secondPageRange;81 }82 }83 }
Copied to your clipboard1// Get the samples from http://www.adobe.com/go/pdftoolsapi_node_sample2// Run the sample:3// node src/rotatepages/rotate-pdf-pages.js45 const PDFServicesSdk = require('@adobe/pdfservices-node-sdk');67 const getFirstPageRangeForRotation = () => {8 // Specify pages for rotation.9 const firstPageRange = new PDFServicesSdk.PageRanges();10 // Add page 1.11 firstPageRange.addSinglePage(1);1213 // Add pages 3 to 4.14 firstPageRange.addPageRange(3, 4);1516 return firstPageRange;17 };1819 const getSecondPageRangeForRotation = () => {20 // Specify pages for rotation.21 const secondPageRange = new PDFServicesSdk.PageRanges();22 // Add page 2.23 secondPageRange.addSinglePage(2);2425 return secondPageRange;26 };2728 try {29 // Initial setup, create credentials instance.30 const credentials = PDFServicesSdk.Credentials31 .serviceAccountCredentialsBuilder()32 .fromFile("pdfservices-api-credentials.json")33 .build();3435 // Create an ExecutionContext using credentials and create a new operation instance.36 const executionContext = PDFServicesSdk.ExecutionContext.create(credentials),37 rotatePagesOperation = PDFServicesSdk.RotatePages.Operation.createNew();3839 // Set operation input from a source file.40 const input = PDFServicesSdk.FileRef.createFromLocalFile('resources/rotatePagesInput.pdf');41 rotatePagesOperation.setInput(input);4243 // Sets angle by 90 degrees (in clockwise direction) for rotating the specified pages of44 // the input PDF file.45 const firstPageRange = getFirstPageRangeForRotation();46 rotatePagesOperation.setAngleToRotatePagesBy(PDFServicesSdk.RotatePages.Angle._90, firstPageRange);4748 // Sets angle by 180 degrees (in clockwise direction) for rotating the specified pages of49 // the input PDF file.50 const secondPageRange = getSecondPageRangeForRotation();51 rotatePagesOperation.setAngleToRotatePagesBy(PDFServicesSdk.RotatePages.Angle._180,secondPageRange);5253 // Execute the operation and Save the result to the specified location.54 rotatePagesOperation.execute(executionContext)55 .then(result => result.saveAsFile('output/rotatePagesOutput.pdf'))56 .catch(err => {57 if (err instanceof PDFServicesSdk.Error.ServiceApiError58 || err instanceof PDFServicesSdk.Error.ServiceUsageError) {59 console.log('Exception encountered while executing operation', err);60 } else {61 console.log('Exception encountered while executing operation', err);62 }63 });64 } catch (err) {65 console.log('Exception encountered while executing operation', err);66 }
Copied to your clipboard1// Please refer our REST API docs for more information2// https://documentcloud.adobe.com/document-services/index.html#post-pageManipulation34curl --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 \"params\": {12 \"cpf:inline\": {13 \"pageActions\": [{14 \"pageAction\": {15 \"rotate\": {16 \"pageRanges\": [{17 \"start\": 1,18 \"end\": 119 }],20 \"angle\": 9021 }22 }23 }]24 }25 },26 \"documentIn\": {27 \"cpf:location\": \"InputFile0\",28 \"dc:format\": \"application/pdf\"29 }30 },31 \"cpf:engine\": {32 \"repo:assetId\": \"urn:aaid:cpf:Service-4735fcf3cf924b25879e6fcf7aa84ad4\"33 },34 \"cpf:outputs\": {35 \"documentOut\": {36 \"cpf:location\": \"cid:multipartLabelOut\",37 \"dc:format\": \"application/pdf\"38 }39 }40}"' \41--form 'InputFile0=@"{{Placeholder for input file (absolute path)}}"'