Rotate Pages
Rotate a page in an existing document.
REST API
See our public API Reference for 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.
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.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 (InputStream inputStream = Files.newInputStream(new File("src/main/resources/rotatePagesInput.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 // First set of page ranges for rotating the specified pages of the input PDF file.24 PageRanges firstPageRange = getFirstPageRangeForRotation();2526 // Second set of page ranges for rotating the specified pages of the input PDF file.27 PageRanges secondPageRange = getSecondPageRangeForRotation();2829 // Create parameters for the job30 RotatePagesParams rotatePagesParams = RotatePagesParams.rotatePagesParamsBuilder()31 .withAngleToRotatePagesBy(Angle._90, firstPageRange)32 .withAngleToRotatePagesBy(Angle._180, secondPageRange)33 .build();3435 // Creates a new job instance36 RotatePagesJob rotatePagesJob = new RotatePagesJob(asset, rotatePagesParams);3738 // Submit the job and gets the job result39 String location = pdfServices.submit(rotatePagesJob);40 PDFServicesResponse<RotatePagesResult> pdfServicesResponse = pdfServices.getJobResult(location, RotatePagesResult.class);4142 // Get content from the resulting asset(s)43 Asset resultAsset = pdfServicesResponse.getResult().getAsset();44 StreamAsset streamAsset = pdfServices.getContent(resultAsset);4546 // Creates an output stream and copy stream asset's content to it47 Files.createDirectories(Paths.get("output/"));48 OutputStream outputStream = Files.newOutputStream(new File("output/rotatePagesOutput.pdf").toPath());49 LOGGER.info("Saving asset at output/rotatePagesOutput.pdf");50 IOUtils.copy(streamAsset.getInputStream(), outputStream);51 outputStream.close();52 } catch (IOException | ServiceApiException | SDKException | ServiceUsageException e) {53 LOGGER.error("Exception encountered while executing operation", e);54 }55 }5657 private static PageRanges getFirstPageRangeForRotation() {58 // Specify pages for rotation59 PageRanges firstPageRange = new PageRanges();60 // Add page 161 firstPageRange.addSinglePage(1);6263 // Add pages 3 to 464 firstPageRange.addRange(3, 4);65 return firstPageRange;66 }6768 private static PageRanges getSecondPageRangeForRotation() {69 // Specify pages for rotation70 PageRanges secondPageRange = new PageRanges();71 // Add page 272 secondPageRange.addSinglePage(2);7374 return secondPageRange;75 }76 }
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.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 RotatePagesOperation rotatePagesOperation = RotatePagesOperation.CreateNew();2829 // Set operation input from a source file.30 FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"rotatePagesInput.pdf");31 rotatePagesOperation.SetInput(sourceFileRef);3233 // Sets angle by 90 degrees (in clockwise direction) for rotating the specified pages of34 // the input PDF file.35 PageRanges firstPageRange = GetFirstPageRangeForRotation();36 rotatePagesOperation.SetAngleToRotatePagesBy(Angle._90, firstPageRange);3738 // Sets angle by 180 degrees (in clockwise direction) for rotating the specified pages of39 // the input PDF file.40 PageRanges secondPageRange = GetSecondPageRangeForRotation();41 rotatePagesOperation.SetAngleToRotatePagesBy(Angle._180, secondPageRange);4243 // Execute the operation.44 FileRef result = rotatePagesOperation.Execute(executionContext);4546 // Save the result to the specified location.47 result.SaveAs(Directory.GetCurrentDirectory() + "/output/rotatePagesOutput.pdf");48 }49 catch (ServiceUsageException ex)50 {51 log.Error("Exception encountered while executing operation", ex);52 }53 // Catch more errors here . . .54 }5556 static void ConfigureLogging()57 {58 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());59 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));60 }6162 private static PageRanges GetFirstPageRangeForRotation()63 {64 // Specify pages for rotation.65 PageRanges firstPageRange = new PageRanges();66 // Add page 1.67 firstPageRange.AddSinglePage(1);6869 // Add pages 3 to 4.70 firstPageRange.AddRange(3, 4);71 return firstPageRange;72 }7374 private static PageRanges GetSecondPageRangeForRotation()75 {76 // Specify pages for rotation.77 PageRanges secondPageRange = new PageRanges();78 // Add page 2.79 secondPageRange.AddSinglePage(2);8081 return secondPageRange;82 }83 }84 }
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.js45const {6 ServicePrincipalCredentials,7 PDFServices,8 MimeType,9 PageRanges,10 RotatePagesParams,11 Angle,12 RotatePagesJob,13 RotatePagesResult,14 SDKError,15 ServiceUsageError,16 ServiceApiError17} = require("@adobe/pdfservices-node-sdk");18const fs = require("fs");1920(async () => {21 let readStream;22 try {23 // Initial setup, create credentials instance24 const credentials = new ServicePrincipalCredentials({25 clientId: process.env.PDF_SERVICES_CLIENT_ID,26 clientSecret: process.env.PDF_SERVICES_CLIENT_SECRET27 });2829 // Creates a PDF Services instance30 const pdfServices = new PDFServices({credentials});3132 // Creates an asset(s) from source file(s) and upload33 readStream = fs.createReadStream("./rotatePagesInput.pdf");34 const inputAsset = await pdfServices.upload({35 readStream,36 mimeType: MimeType.PDF37 });3839 // First set of page ranges for rotating the specified pages of the input PDF file40 const firstPageRange = getFirstPageRangeForRotation();4142 // Second set of page ranges for rotating the specified pages of the input PDF file43 const secondPageRange = getSecondPageRangeForRotation();4445 // Create parameters for the job46 const params = new RotatePagesParams()47 .setAngleToRotatePagesBy(Angle._90, firstPageRange)48 .setAngleToRotatePagesBy(Angle._180, secondPageRange);4950 // Creates a new job instance51 const job = new RotatePagesJob({inputAsset, params});5253 // Submit the job and get the job result54 const pollingURL = await pdfServices.submit({job});55 const pdfServicesResponse = await pdfServices.getJobResult({56 pollingURL,57 resultType: RotatePagesResult58 });5960 // Get content from the resulting asset(s)61 const resultAsset = pdfServicesResponse.result.asset;62 const streamAsset = await pdfServices.getContent({asset: resultAsset});6364 // Creates a write stream and copy stream asset's content to it65 const outputFilePath = "./rotatePagesOutput.pdf";66 console.log(`Saving asset at ${outputFilePath}`);6768 const writeStream = fs.createWriteStream(outputFilePath);69 streamAsset.readStream.pipe(writeStream);70 } catch (err) {71 if (err instanceof SDKError || err instanceof ServiceUsageError || err instanceof ServiceApiError) {72 console.log("Exception encountered while executing operation", err);73 } else {74 console.log("Exception encountered while executing operation", err);75 }76 } finally {77 readStream?.destroy();78 }79})();8081function getFirstPageRangeForRotation() {82 // Specify pages for rotation.83 const firstPageRange = new PageRanges();84 // Add page 1.85 firstPageRange.addSinglePage(1);86 // Add pages 3 to 4.87 firstPageRange.addRange(3, 4);88 return firstPageRange;89}9091function getSecondPageRangeForRotation() {92 // Specify pages for rotation.93 const secondPageRange = new PageRanges();94 // Add page 2.95 secondPageRange.addSinglePage(2);96 return secondPageRange;97}
Copied to your clipboard1// Please refer our REST API docs for more information2// https://developer.adobe.com/document-services/docs/apis/#tag/Page-Manipulation34curl --location --request POST 'https://pdf-services.adobe.io/operation/pagemanipulation' \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 "assetID": "urn:aaid:AS:UE1:23c30ee0-2e4d-46d6-87f2-087832fca718f",10 "pageActions": [11 {12 "rotate": {13 "angle": 90,14 "pageRanges": [15 {16 "start": 117 }18 ]19 }20 },21 {22 "rotate": {23 "angle": 180,24 "pageRanges": [25 {26 "start": 2,27 "end": 228 }29 ]30 }31 }32 ]33}'