Split PDF in Node.js
Contents
[
Hide
]
Split PDF into two files using Node.js
In case you want to split a single PDF into parts, you can use AsposePdfSplit2Files function. Please check the following code snippet in order to split two PDFs in Node.js environment.
CommonJS:
- Call
require
and importasposepdfnodejs
module asAsposePdf
variable. - Specify the name of the PDF files which will be split.
- Call
AsposePdf
as Promise and perform the operation for splitting file. Receive the object if successful. - Call the function AsposePdfSplit2Files.
- Split two PDF files. It sets the variable pageToSplit to 1, indicating that the PDF file will be split at page 1.
- Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultSplit1.pdf”, and “ResultSplit2.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 => {
/*Set number a page to split*/
const pageToSplit = 1;
/*Split to two PDF-files and save the "ResultSplit1.pdf", "ResultSplit2.pdf"*/
const json = AsposePdfModule.AsposePdfSplit2Files(pdf_file, pageToSplit, "ResultSplit1.pdf", "ResultSplit2.pdf");
console.log("AsposePdfSplit2Files => %O", json.errorCode == 0 ? [json.fileNameResult1, json.fileNameResult2] : json.errorText);
});
ECMAScript/ES6:
- Import the
asposepdfnodejs
module. - Specify the name of the PDF files which will be split.
- Initialize the AsposePdf module. Receive the object if successful.
- Call the function AsposePdfSplit2Files.
- Split two PDF files. It sets the variable pageToSplit to 1, indicating that the PDF file will be split at page 1.
- Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultSplit1.pdf”, and “ResultSplit2.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';
/*Set number a page to split*/
const pageToSplit = 1;
/*Split to two PDF-files and save the "ResultSplit1.pdf", "ResultSplit2.pdf"*/
const json = AsposePdfModule.AsposePdfSplit2Files(pdf_file, pageToSplit, "ResultSplit1.pdf", "ResultSplit2.pdf");
console.log("AsposePdfSplit2Files => %O", json.errorCode == 0 ? [json.fileNameResult1, json.fileNameResult2] : json.errorText);