How to Rotate PDF Pages using Rest API in Ruby

How to Rotate PDF Pages using Rest API in Ruby
How to Flip PDF Pages using Rest API in Ruby

Let us explore the scenarios related to rotation in PDF documents. You can rotate all pages or specific PDF pages programmatically using Ruby in your applications. We will be walking through the following PDF pages rotation scenarios with the help of simple examples of PDF rotation features. In this article, we will learn how to rotate PDF pages using REST API in Ruby.

The following topics shall be covered in this article:

PDF Pages Rotation Rest API and Ruby SDK

You can rotate pages by setting rotation angles like 90, 180 or 270 degrees using GroupDocs.Merger API. For rotating PDF files, i will be using the Ruby SDK of GroupDocs.Merger Cloud API. It allows you to split, combine, remove and rearrange a single page or a collection of pages from supported document formats of Word, Excel, PDF, PowerPoint, and HTML etc. You can install GroupDocs.Merger Cloud SDK to your Ruby application using the following command in the terminal:

gem install groupdocs_conversion_cloud

Please get your Client ID and Secret from the dashboard before following the below mentioned steps. Once you have your ID and Secret, add following in the code as shown below:

Once the API is configured successfully, you can use the Rotation enumeration to select a suitable value of rotation in the clockwise direction.

Rotate All Pages of a PDF Document using Ruby

You can rotate PDF pages in a PDF document programmatically on the cloud by following the steps given below. First, upload the PDF file to the cloud and the uploaded PDF file will be available in the files section of the dashboard on the cloud. There could be many use cases where you need to rotate PDF files. You can rotate all pages of a PDF file by following the steps given below:

  • Firstly, create an instance of the PagesApi
  • Next, create an instance of the RotateOptions
  • Then, create an instance of the FileInfo
  • Provide the input PDF document path and output file path
  • Next, set the desired page rotation like Rotate90
  • After that, create the RotateRequest with RotateOptions as an argument
  • Finally, call the rotate() method and save the output PDF document

The following code snippet shows how to rotate all pages of a PDF file using REST API in Ruby:

Finally, the above code sample will save the updated PDF file on the cloud.

Rotate Specific Pages of PDF file using Ruby

The rotation in a PDF document is applied on page level. Therefore, you can also rotate specific pages of PDF file as per your requirements. You only need to choose the page number you want to apply the rotation on. The steps below explain how to rotate certain pages of PDF file:

  • Firstly, create an instance of the PagesApi
  • Next, create an instance of the RotateOptions
  • Then, create an instance of the FileInfo
  • Provide the input PDF document path and output file path
  • Assign the exact page numbers using pages collection
  • Set the desired page rotation like Rotate90, Rotate180 or Rotate270
  • After that, create the RotateRequest with RotateOptions as an argument
  • Finally, call the rotate() method and save the output PDF document

The following code snippet elaborates how to rotate specific or certain pages in a PDF document using Ruby:

Finally, the above code sample will save the output PDF file on the cloud.

Rotate PDF Pages by Providing Page Number using Ruby

You can also rotate PDF pages by page number. You need to provide the start page number and end page number to apply the rotation. The steps below explain how to rotate PDF pages by page numbers of a PDF file:

  • Firstly, create an instance of the PagesApi
  • Next, create an instance of the RotateOptions
  • Then, create an instance of the FileInfo
  • Provide the input PDF document path and output file path
  • Set the start page number and end page number values;
  • Set the desired page rotation like Rotate270
  • After that, create the RotateRequest with RotateOptions as an argument
  • Finally, call the rotate() method and save the output PDF document

The code snippet below shows how to rotate pages in PDF document by providing page numbers using Ruby Rest API:

The above code example will save the output PDF document on the cloud.

Rotate PDF Pages by Setting Range Mode using Ruby

You can rotate image in a PDF document while adding or inserting the image in the PDF file. It can be helpful when you want to update or change the orientation of the image. You can follow these steps to rotate image on a PDF page:

  • Firstly, create an instance of the PagesApi
  • Next, create an instance of the RotateOptions
  • Then, create an instance of the FileInfo
  • Provide the input PDF document path and output file path
  • Set the desired page rotation like Rotate180
  • Next, set the start page number and end page number values;
  • Now, Set range mode to EvenPages or OddPages or AllPages
  • After that, create the RotateRequest with RotateOptions as an argument
  • Finally, call the rotate() method and save the output PDF document

The following code demonstrates how to rotate image or picture in a PDF document programmatically using Ruby:

Finally, the above code snippet will save the output PDF document on the cloud.

Online Rotate PDF Pages for Free

Please try the following free online tool to rotate PDF document pages, which is developed using the above API.

Summing up

In this article, you have learned:

  • how to rotate all pages in a PDF document using Ruby;
  • how to rotate certain PDF using Ruby;
  • how to rotate PDF Pages by page number using Ruby;
  • how to rotate PDF Pages by range mode using Ruby;

Additionally, you can learn more about GroupDocs.Merge Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. Moreover, groupdocs.cloud is writing new blog posts on other interesting topics. Therefore, please stay in touch for regular updates.

Ask a question

If you have any queries regarding pdf pages rotation, please feel free to ask us at Free Support Forum

See Also