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:

// This code example demonstrates how to add your client id and secret in the code.
global.clientId = '659fe7da-715b-4744-a0f7-cf469a392b73';
global.clientSecret = 'b377c36cfa28fa69960ebac6b6e36421';
global.myStorage = "";
const configuration = new groupdocs_merger_cloud.Configuration(clientId, clientSecret);
configuration.apiBaseUrl = "https://api.groupdocs.cloud";

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:

// This code example demonstrates how to upload a PDF file to the cloud.
// Construct FileApi
let fileApi = groupdocs_merger_cloud.FileApi.fromConfig(configuration);
let file = 'C:\\Files\\merger\\sample.pdf';
// Read files one by one
fs.readFile(file, (err, fileStream) => {
// Create upload file request
let request = new groupdocs_merger_cloud.UploadFileRequest("sample.pdf", fileStream, myStorage);
// Upload file
fileApi.uploadFile(request);
});

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.

// This code example demonstrates how to move pages within PDF document.
// Api initialization
let pagesApi = groupdocs_merger_cloud.PagesApi.fromKeys(clientId, clientSecret);
// Initialize MoveOptions
let options = new groupdocs_merger_cloud.MoveOptions();
// Input file path
options.fileInfo = new groupdocs_merger_cloud.FileInfo();
options.fileInfo.filePath = "sample.pdf";
// Output file path
options.outputPath = "move-pages.pdf";
// The page to move
options.pageNumber = 1;
// At which position to move
options.newPageNumber = 4;
// Create move request
let request = new groupdocs_merger_cloud.MoveRequest(options);
// Move page
let result = await pagesApi.move(request);
console.log("Document edited: " + result.path);
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:

// This code example demonstrates how to download a PDF file from the cloud.
// Initialize api
var fileApi = groupdocs_merger_cloud.FileApi.fromConfig(configuration);
// Create file download request
let request = new groupdocs_merger_cloud.DownloadFileRequest("move-pages.pdf", myStorage);
// Download file
let response = await fileApi.downloadFile(request);
// Save PDF file in working directory
fs.writeFile("C:\\Files\\merger\\move-pages.pdf", response, "binary", function (err) { });

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.

// This code example demonstrates how to swap pages within PDF document.
// Api initialization
let pagesApi = groupdocs_merger_cloud.PagesApi.fromKeys(clientId, clientSecret);
// Initialize SwapOptions
let options = new groupdocs_merger_cloud.SwapOptions();
// Input file path
options.fileInfo = new groupdocs_merger_cloud.FileInfo();
options.fileInfo.filePath = "sample.pdf";
// Output file path
options.outputPath = "swap-pages.pdf";
// First page number to swap
options.firstPageNumber = 2;
// Second page number to swap with
options.secondPageNumber = 4;
// Create swap request
let request = new groupdocs_merger_cloud.SwapRequest(options);
// Swap pages
let result = await pagesApi.swap(request);
console.log("Document edited: " + result.path);
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/