Rearrange PDF Pages using REST API in Node.js

PDF is the most popular format for sharing and printing documents. In certain cases, we may need to reorder or swap the pages in PDF files. We can transform disorganized PDF files into well-structured documents by moving or swapping specific pages within PDF documents programmatically on the cloud. In this article, we will learn how to rearrange PDF pages using a REST API in Node.js.

The following topics shall be covered in this article:

REST API and Node.js SDK to Rearrange PDF Pages

For rearranging pages in a PDF document, we will be using the Node.js SDK of GroupDocs.Merger Cloud API. It allows splitting, combining, removing and rearranging a single page or a collection of pages within supported document formats. Please install it using the following command in the console:

npm install groupdocs-merger-cloud

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

Rearrange PDF Pages using REST API in Node.js

We can rearrange pages by moving any page to a new position within a PDF document programmatically on the cloud by following the steps given below:

  1. Upload the PDF file to the cloud
  2. Reorder pages of the uploaded PDF document
  3. Download the updated file

Upload the PDF File

Firstly, we will upload the PDF file to the cloud using the code example given below:

As a result, the uploaded PDF file will be available in the files section of the dashboard on the cloud.

Reorder PDF Pages using Node.js

Now, we will reorder the pages of the uploaded PDF file by following the steps given below:

  • Firstly, create an instance of the PagesApi.
  • Next, provide the uploaded PDF file path.
  • Then, set the output file path.
  • Next, set the current page number and new page number.
  • After that, create the MoveRequest with MoveOptions as an argument.
  • Finally, call the **move()** method and save the updated document.

The following code sample shows how to reorder PDF pages using a REST API in Node.js.

Rearrange PDF Pages using REST API in Node.js

Rearrange PDF Pages using REST API in Node.js

Download the Updated File

Finally, the above code sample will save the updated PDF file on the cloud. It can be downloaded using the following code sample:

Swap PDF Pages using Node.js

We can swap the position of two pages within a PDF document by following the steps given below:

  • Firstly, create an instance of the PagesApi.
  • Next, provide the uploaded PDF file path.
  • Then, set the output file path.
  • Next, set the first page number and the second page number.
  • After that, create the SwapRequest with SwapOptions as an argument.
  • Finally, call the **swap()** method and save the updated document.

The following code sample shows how to swap two pages within a PDF document using a REST API in Node.js.

Swap PDF Pages using a REST API in Node.js

Swap PDF Pages using a REST API in Node.js

Try Online

Please try the following free online tool to move or swap document pages, which is developed using the above API. https://products.groupdocs.app/merger/pdf

Conclusion

In this article, we have learned how to:

  • reorder and swap pages of a PDF document in Node.js;
  • upload a PDF file to the cloud;
  • download updated PDF from the cloud.

Besides, 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. In case of any ambiguity, please feel free to contact us on the [fo][17].

See Also

[17]: https://forum.groupdocs.cloud/c/editor/)[rum](https://forum.groupdocs.cloud/c/merger/