Optimize PDF using Aspose.PDF for Node.js via C++
Contents
[
Hide
]
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:
- Call
require
and importasposepdfnodejs
module asAsposePdf
variable. - Specify the name of the PDF file which will be optimized.
- Call
AsposePdf
as Promise and perform the operation for optimizing file. Receive the object if successful. - Call the function AsposePdfOptimize.
- 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:
- Import the
asposepdfnodejs
module. - Specify the name of the PDF file which will be optimized.
- Initialize the AsposePdf module. Receive the object if successful.
- Call the function AsposePdfOptimize.
- 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);