Split PDF in Node.js

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:

  1. Call require and import asposepdfnodejs module as AsposePdf variable.
  2. Specify the name of the PDF files which will be split.
  3. Call AsposePdf as Promise and perform the operation for splitting file. Receive the object if successful.
  4. Call the function AsposePdfSplit2Files.
  5. Split two PDF files. It sets the variable pageToSplit to 1, indicating that the PDF file will be split at page 1.
  6. 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:

  1. Import the asposepdfnodejs module.
  2. Specify the name of the PDF files which will be split.
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the function AsposePdfSplit2Files.
  5. Split two PDF files. It sets the variable pageToSplit to 1, indicating that the PDF file will be split at page 1.
  6. 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);