Add Header and Footer to PDF in Node.js

Contents
[ ]

Aspose.PDF for Node.js via C++ allows you to add header and footer in your existing PDF file.

In case you want to add header and footer, you can use AsposePdfAddTextHeaderFooter function.

Please check the following code snippet in order to add header and footer to a PDF file 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 header and footer will be added.
  3. Call AsposePdf as Promise and perform the operation for adding header and footer. Receive the object if successful.
  4. Call the function AsposePdfAddTextHeaderFooter.
  5. Add text in Header and Footer of a PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultAddHeaderFooter.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 text in Header/Footer of a PDF-file and save the "ResultAddHeaderFooter.pdf"*/
      const json = AsposePdfModule.AsposePdfAddTextHeaderFooter(pdf_file, "Aspose.PDF for Node.js via C++ via C++", "ASPOSE", "ResultAddHeaderFooter.pdf");
      console.log("AsposePdfAddTextHeaderFooter => %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 header and footer will be added.
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the function AsposePdfAddTextHeaderFooter.
  5. Add text in Header and Footer of a PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultAddHeaderFooter.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 text in Header/Footer of a PDF-file and save the "ResultAddHeaderFooter.pdf"*/
  const json = AsposePdfModule.AsposePdfAddTextHeaderFooter(pdf_file, "Aspose.PDF for Node.js via C++ via C++", "ASPOSE", "ResultAddHeaderFooter.pdf");
  console.log("AsposePdfAddTextHeaderFooter => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);