Optimize PDF using Aspose.PDF for Node.js via C++

Optimize PDF Document

Toolkit by Aspose.PDF for Node.js via C++ allows you to optimize PDF content for the Node.js environment.

Optimization, or linearization refers to the process of making a PDF file suitable for online browsing using a web browser.

In case you want to optimize PDF, you can use AsposePdfOptimize function. Please check the following code snippet in order to optimize PDF files in Node.js environment.

CommonJS:

  1. Call require and import asposepdfnodejs module as AsposePdf variable.
  2. Specify the name of the PDF file which will be optimized.
  3. Call AsposePdf as Promise and perform the operation for optimizing file. Receive the object if successful.
  4. Call the function AsposePdfOptimize.
  5. Optimize a PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultOptimize.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 => {
      /*Optimize a PDF-file and save the "ResultOptimize.pdf"*/
      const json = AsposePdfModule.AsposePdfOptimize(pdf_file, "ResultOptimize.pdf");
      console.log("AsposePdfOptimize => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
  });

ECMAScript/ES6:

  1. Import the asposepdfnodejs module.
  2. Specify the name of the PDF file which will be optimized.
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the function AsposePdfOptimize.
  5. Optimize a PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultOptimize.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';
  /*Optimize a PDF-file and save the "ResultOptimize.pdf"*/
  const json = AsposePdfModule.AsposePdfOptimize(pdf_file, "ResultOptimize.pdf");
  console.log("AsposePdfOptimize => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);