Add page numbering to PDF in Node.js

Contents
[ ]

The page number makes it easier for the reader to locate different parts of the document The following code snippets shows how to add a page numbers to PDF pages using the AsposePdfAddPageNum function in Node.js.

Please check the following code snippet in order to add a page numbers into PDF in Node.js environment.

CommonJS:

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

ECMAScript/ES6:

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