Add Image stamps to PDF in Node.js

Add Image Stamp in PDF File

Stamping a PDF document is similar to stamping a paper document. A stamp in a PDF file provides additional information to the PDF file, such as protecting the PDF file for others to use and confirming the security of the contents of the PDF file. You can use the AsposePdfAddStamp function to add an image stamp to a PDF file using Node.js.

Please check the following code snippet in order to add an image stamp 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 image stamp will be added.
  3. Call AsposePdf as Promise and perform the operation for adding image stamp. Receive the object if successful.
  4. Call the function AsposePdfAddStamp.
  5. Add stamp to a PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultImage.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';
  const stamp_file = 'Aspose.jpg';
  AsposePdf().then(AsposePdfModule => {
      /*Add stamp to a PDF-file and save the "ResultImage.pdf"*/
      const json = AsposePdfModule.AsposePdfAddStamp(pdf_file, stamp_file, 0, 5, 5, 40, 40, AsposePdfModule.Rotation.on270, 0.5, "ResultAddStamp.pdf");
      console.log("AsposePdfAddStamp => %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 image stamp will be added.
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the function AsposePdfAddStamp.
  5. Add stamp to a PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultImage.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';
  const stamp_file = 'Aspose.jpg';
  /*Add stamp to a PDF-file and save the "ResultImage.pdf"*/
  const json = AsposePdfModule.AsposePdfAddStamp(pdf_file, stamp_file, 0, 5, 5, 40, 40, AsposePdfModule.Rotation.on270, 0.5, "ResultAddStamp.pdf");
  console.log("AsposePdfAddStamp => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);