HTML JPG PDF XML DOCX
  Product Family
PDF

Replace Signature in PDF in Node.js SDK

API for working with Signature in PDF documents using Cloud Node.js SDK

Get Started

How to replace signature in PDF via Node.js SDK

To replace signature in PDF, we’ll use Aspose.PDF Cloud Node.js SDK. This Cloud SDK assists Node.js programmers in developing cloud-based PDF creator, annotator, editor, and converter apps using Node.js programming language via Aspose.PDF REST API. Simply create an account at Aspose for Cloud and get your application information. Once you have the App SID & key, you are ready to give the Aspose.PDF Cloud Node.js SDK.

Package Manager Console Command


     
    npm install asposepdfcloud --save
     
     

Steps to replace signature in PDF via Cloud Node.js

Aspose.PDF Cloud developers can easily load & replace signature in PDF in just a few lines of code.

  1. Upload PDF Document
  2. Create and Configure Signature
  3. Create Signature Field
  4. Replace the Signature in the PDF
  5. Download the Modified PDF
  6. Downloads the updated PDF file from the Aspose PDF Cloud storage.
 

Replace signature in PDF using Node.js


    import credentials from "../../../Credentials/credentials.json"  with { type: "json" };
    import fs from 'node:fs/promises';
    import path from 'node:path';
    import { PdfApi } from "../../src/api/api.js";
    import {Signature} from "../../src/models/signature.js";
    import {SignatureType} from "../../src/models/signatureType.js";
    import {SignatureField} from "../../src/models/signatureField.js";

    const configParams = {
        LOCAL_FOLDER: "C:\\Samples\\",
        PDF_DOCUMENT_NAME: "sample.pdf",
        LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
        LOCAL_SIGNATURE_PATH: "C:\\Samples\\Signatures\\3",
        SIGNATURE_PFX: "signature.pfx",
        SIGNATURE_FORM_FIELD: 'Signature_1',
        SIGNATURE_PASSWORD: 'Password',
        SIGNATURE_CONTACT: 'Contact',
        SIGNATURE_LOCATION: 'Location',
        SIGNATURE_AUTHORITY: 'Issuer',
        SIGNATURE_DATE: '04/19/2025 12:15:00.000 PM',
        SIGNATURE_RECT:  { lLx: 100, lLy: 100, uRx: 500, uRy: 500 }
    };

    const pdfApi = new PdfApi(credentials.id, credentials.key);

    const pdfSignatures = {
        async uploadFile (folder, fileName) {
            const fileNamePath = path.join(folder, fileName);
            const pdfFileData = await fs.readFile(fileNamePath);
            await pdfApi.uploadFile(fileName, pdfFileData);
            console.log("File '" + fileName + "' successfully uploaded!");
        },

        async uploadDocument () {
            await this.uploadFile(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
        },
                        
        async downloadResult () {
            const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
            const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
            await fs.writeFile(filePath, changedPdfData.body);
            console.log("Downloaded: " + filePath);
        },

        async replaceSignature () {
            if (pdfApi)
            {  
                
                const signature = new Signature();
                signature.authority = configParams.SIGNATURE_AUTHORITY;
                signature.contact = configParams.SIGNATURE_CONTACT;
                signature.date = configParams.SIGNATURE_DATE;
                signature.formFieldName = configParams.SIGNATURE_FORM_FIELD;
                signature.location = configParams.SIGNATURE_LOCATION;
                signature.password = configParams.SIGNATURE_PASSWORD;
                signature.rectangle = configParams.SIGNATURE_RECT;
                signature.signaturePath = configParams.SIGNATURE_PFX;
                signature.signatureType = SignatureType.PKCS7;
                signature.visible = true;

                const field = new SignatureField();
                field.pageIndex = 1;
                field.signature = signature;
                field.partialName = 'sign1';
                field.rect = configParams.SIGNATURE_RECT;

                const response = await pdfApi.putSignatureField(configParams.PDF_DOCUMENT_NAME, configParams.SIGNATURE_FORM_FIELD, field);

                if (response.body.code == 200)
                    console.log("replaceSignature(): Signature '" + configParams.SIGNATURE_CONTACT + "' successfully replaced in the document.");
                else
                    console.error("replaceSignature(): Failed to replace signature in the document. Response code: " + response.body.code);
            }
        },
    }

    async function main() {
        try {
            await pdfSignatures.uploadFile(configParams.LOCAL_SIGNATURE_PATH, configParams.SIGNATURE_PFX);
            await pdfSignatures.uploadDocument();
            await pdfSignatures.replaceSignature();
            await pdfSignatures.downloadResult();
        } catch (error) {
            console.error("Error:", error.message);
        }
    }
 

Work with Signature in PDF

Replacing a signature in a PDF is necessary when the original signature becomes outdated, incorrect, or invalid. This process ensures that the document reflects the most accurate and authorized information. Common reasons for replacement include updating signer credentials, correcting errors in the signature details, re-signing after document changes, or switching to a stronger digital certificate for enhanced security and compliance. Replacing the signature helps maintain the document’s authenticity, integrity, and legal validity. Replace Signature in PDF documents with Aspose.PDF Cloud Node.js SDK.

With our Node.js library you can:

  • Add PDF document’s header & footer in text or image format.
  • Add tables & stamps (text or image) to PDF documents.
  • Append multiple PDF documents to an existing file.
  • Work with PDF attachments, annotations, & form fields.
  • Apply encryption or decryption to PDF documents & set a password.
  • Delete all stamps & tables from a page or entire PDF document.
  • Delete a specific stamp or table from the PDF document by its ID.
  • Replace single or multiple instances of text on a PDF page or from the entire document.
  • Extensive support for converting PDF documents to various other file formats.
  • Extract various elements of PDF files & make PDF documents optimized.
  • You can try out our free Apps to test the functionality online.