Rotate PDF Pages in Node.js
Contents
[
Hide
]
This section describes how to rotate pages in an existing PDF file using Aspose.PDF for Node.js via C++.
In case you want to rotate PDF pages, you can use AsposePdfRotateAllPages function. This function uses a special parameter ‘AsposePdfModule.Rotation’ for rotation value. With which you can set how many degrees you need to rotate PDF. There are variants: None, 90, 180, or 270 degrees.
Please check the following code snippet in order to rotate PDF pages in Node.js environment.
CommonJS:
- Call
require
and importasposepdfnodejs
module asAsposePdf
variable. - Specify the name of the PDF file to rotate.
- Call
AsposePdf
as Promise and perform the operation for rotating pages. Receive the object if successful. - Call the function AsposePdfRotateAllPages.
- Rotate all PDF files. The rotation is set to 270 degrees (on270). Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultRotation.pdf”. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.
const AsposePdf = require('asposepdfnodejs');
const pdf_file = 'Aspose.pdf';
AsposePdf().then(AsposePdfModule => {
/*Rotate PDF-pages and save the "ResultRotation.pdf"*/
const json = AsposePdfModule.AsposePdfRotateAllPages(pdf_file, AsposePdfModule.Rotation.on270, "ResultRotation.pdf");
console.log("AsposePdfRotateAllPages => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
});
ECMAScript/ES6:
- Import the
asposepdfnodejs
module. - Specify the name of the PDF file to rotate.
- Initialize the AsposePdf module. Receive the object if successful.
- Call the function AsposePdfRotateAllPages.
- Rotate all PDF files. The rotation is set to 270 degrees (on270). Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultRotation.pdf”. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.
import AsposePdf from 'asposepdfnodejs';
const AsposePdfModule = await AsposePdf();
const pdf_file = 'Aspose.pdf';
/*Rotate PDF-pages and save the "ResultRotation.pdf"*/
const json = AsposePdfModule.AsposePdfRotateAllPages(pdf_file, AsposePdfModule.Rotation.on270, "ResultRotation.pdf");
console.log("AsposePdfRotateAllPages => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);