Skip to content

Instantly share code, notes, and snippets.

@groupdocs-cloud-gists
Last active February 8, 2022 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groupdocs-cloud-gists/90ff1c02228b22feb6d96cefd92e5a53 to your computer and use it in GitHub Desktop.
Save groupdocs-cloud-gists/90ff1c02228b22feb6d96cefd92e5a53 to your computer and use it in GitHub Desktop.
Rearrange PDF Pages using REST API in Node.js
// 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";
// 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) { });
// 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);
// 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);
// 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);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment