跳過到頁腳內容
使用 IRONPDF FOR NODE.JS
如何在 Node.js 中簽署 PDF 文件

如何在 Node.js 中簽署 PDF 文件

在現代文件管理的領域中,程式化簽署PDF已成為無數應用程式的重要功能。 Node.js,一個強大的伺服端JavaScript運行環境,為開發者提供了一個靈活的平台來整合無縫的PDF簽署功能。 無論是電子合約、法律文件,還是其他重要文件,透過Sign PDF Node.js方式,開發者可以自動化並簡化數位簽署流程。 這篇簡介探討使用Node.js簽署PDF的意義,強調其在促進安全、高效和具法律約束力的數位交易中的重要性。

在本文中,我們將討論如何使用Node.js來數位簽署PDF文件。 為此,我們將使用名為IronPDF的頂尖Node.js PDF程式庫。

1. 如何使用Node.js簽署PDF文件

  1. 安裝用於在Node.js中簽署PDF的PDF程式庫。
  2. 匯入所需的相依性。
  3. 使用fromFile方法開啟PDF文件。
  4. 使用signDigitalSignature方法簽署PDF文件。
  5. 使用isSigned方法檢查PDF文件是否已簽署。
  6. 使用signatureCount函式找到數位簽名數量。
  7. 使用saveAs方法儲存簽署的PDF文件。

2. IronPDF For Node.js

在不斷發展的網頁開發領域,Node.js應用程式中動態且無縫的PDF生成需求變得愈發重要。 推出IronPDF for Node.js — 這是一個強大的集成,結合了IronPDF龐大的PDF處理能力與Node.js的靈活性。 此創新的解決方案讓開發者可以輕鬆創建、操作和呈現高質量的PDF文件,提供從報告生成到製作動態發票的全面工具包。 這篇簡介深入探討IronPDF for Node.js的能力,強調其作為開發者尋求高效且功能豐富的PDF處理工具時的寶貴資產。

3. 安裝IronPDF程式庫到Node.js

從npm安裝IronPDF Library for Node.js以開始使用數位簽名簽署PDF文件。 在控制台上運行以下命令以安裝IronPDF程式庫。

npm install @ironsoftware/ironpdf
npm install @ironsoftware/ironpdf
SHELL

要安裝IronPDF引擎(這是使用IronPDF Library所必須的),請在控制台上運行以下命令。

npm install @ironsoftware/ironpdf-engine-windows-x64
npm install @ironsoftware/ironpdf-engine-windows-x64
SHELL

4. 程式化數位簽署PDF文件

使用IronPDF for Node.js程式化地數位簽署PDF涉及到利用程式庫的功能將數位簽名嵌入PDF文件中。 這裡有一個簡化的例子,說明您如何實現這一點:

import { PdfDocument } from "@ironsoftware/ironpdf";
import { IronPdfGlobalConfig } from "@ironsoftware/ironpdf";

// Asynchronous function to create and sign PDFs
(async function createPDFs() {
  // Input the license key
  const IronPdfConfig = {
    licenseKey: "License-Key", // Replace with your actual license
  };

  // Set the global configuration with the license key
  IronPdfGlobalConfig.setConfig(IronPdfConfig);

  // Define the digital signature parameters
  var digitalSignature = {
    signatureImage: {
      SignatureImagePath: "signature.png" // Path to the signature image
    },
    certificatePath: "WALEED.pfx", // Path to the certificate file
    certificatePassword: "nokhanok" // Password for the certificate
  };

  // Open the PDF document that will be signed
  await PdfDocument.fromFile("output.pdf").then(async (pdf) => {
    // Sign the PDF file with the digital signature
    await pdf.signDigitalSignature(digitalSignature);

    // Check if the PDF is signed successfully
    var signed = await pdf.isSigned();
    if (signed) {
      console.log("\nThe document is successfully signed");
    }

    // Save the signed PDF document
    await pdf.saveAs("sample-contract-signed.pdf");
  });
})();
import { PdfDocument } from "@ironsoftware/ironpdf";
import { IronPdfGlobalConfig } from "@ironsoftware/ironpdf";

// Asynchronous function to create and sign PDFs
(async function createPDFs() {
  // Input the license key
  const IronPdfConfig = {
    licenseKey: "License-Key", // Replace with your actual license
  };

  // Set the global configuration with the license key
  IronPdfGlobalConfig.setConfig(IronPdfConfig);

  // Define the digital signature parameters
  var digitalSignature = {
    signatureImage: {
      SignatureImagePath: "signature.png" // Path to the signature image
    },
    certificatePath: "WALEED.pfx", // Path to the certificate file
    certificatePassword: "nokhanok" // Password for the certificate
  };

  // Open the PDF document that will be signed
  await PdfDocument.fromFile("output.pdf").then(async (pdf) => {
    // Sign the PDF file with the digital signature
    await pdf.signDigitalSignature(digitalSignature);

    // Check if the PDF is signed successfully
    var signed = await pdf.isSigned();
    if (signed) {
      console.log("\nThe document is successfully signed");
    }

    // Save the signed PDF document
    await pdf.saveAs("sample-contract-signed.pdf");
  });
})();
JAVASCRIPT

這段Node.js腳本使用IronPDF程式庫來數位簽署PDF文件。 在設置IronPDF配置並提供授權金鑰後,代碼定義了一個數位簽名,指定一個簽署圖像路徑、證書路徑以及相關密碼。

接著,它打開一個現有的PDF文件("output.pdf"),使用定義的數位簽名簽署它,檢查簽署過程是否成功,並將已簽署的文件另存為"sample-contract-signed.pdf"。 這段腳本提供了一個簡化且程式化高效的解決方案,用於在Node.js環境中使用IronPDF將數位簽名應用於PDF。

如何在Node.js中簽署PDF文件,圖1:文件已成功簽署 文件已成功簽署

4.1. 驗證已簽署PDF文件

要在Node.js中使用IronPDF驗證已簽署的PDF文件,您可以使用以下代碼片段。 這假設您擁有已簽署的PDF文件和與數位簽名相關聯的公鑰。

import { PdfDocument } from "@ironsoftware/ironpdf";
import { IronPdfGlobalConfig } from "@ironsoftware/ironpdf";

// Asynchronous function to verify signed PDFs
(async function verifyPDFs() {
  // Configure IronPDF with a license key
  const IronPdfConfig = {
    licenseKey: "License-Key", // Replace with your actual license
  };

  // Set the global configuration
  IronPdfGlobalConfig.setConfig(IronPdfConfig);

  // Open the signed PDF document
  await PdfDocument.fromFile("sample-contract-signed.pdf").then(async (pdf) => {
    // Check if the PDF is signed
    var signed = await pdf.isSigned();
    if (signed) {
      console.log("\nThe document is signed");
    }
  });
})();
import { PdfDocument } from "@ironsoftware/ironpdf";
import { IronPdfGlobalConfig } from "@ironsoftware/ironpdf";

// Asynchronous function to verify signed PDFs
(async function verifyPDFs() {
  // Configure IronPDF with a license key
  const IronPdfConfig = {
    licenseKey: "License-Key", // Replace with your actual license
  };

  // Set the global configuration
  IronPdfGlobalConfig.setConfig(IronPdfConfig);

  // Open the signed PDF document
  await PdfDocument.fromFile("sample-contract-signed.pdf").then(async (pdf) => {
    // Check if the PDF is signed
    var signed = await pdf.isSigned();
    if (signed) {
      console.log("\nThe document is signed");
    }
  });
})();
JAVASCRIPT

這段Node.js腳本使用IronPDF程式庫來處理名為"sample-contract-signed.pdf"的PDF文件。 首先,設定IronPDF配置並提供特定的授權金鑰。 接著,腳本加載PDF文件,使用isSigned方法檢查它是否已數位簽署,並記錄一條指示簽署狀態的消息。

如何在Node.js中簽署PDF文件,圖2:文件已簽署 文件已簽署

4.2. 計算數位簽名的數量

要在Node.js中使用IronPDF計算PDF文件中的數位簽名數量,您可以使用以下代碼片段:

import { PdfDocument } from "@ironsoftware/ironpdf";

// Asynchronous function to count signatures in a PDF
(async function countSignatures() {
  // Open the PDF document
  await PdfDocument.fromFile("sample-contract-signed.pdf").then(async (pdf) => {
    // Count the number of signatures in the PDF
    var numberOfSignatures = await pdf.signatureCount();
    console.log("Number of Signatures: " + numberOfSignatures);
  });
})();
import { PdfDocument } from "@ironsoftware/ironpdf";

// Asynchronous function to count signatures in a PDF
(async function countSignatures() {
  // Open the PDF document
  await PdfDocument.fromFile("sample-contract-signed.pdf").then(async (pdf) => {
    // Count the number of signatures in the PDF
    var numberOfSignatures = await pdf.signatureCount();
    console.log("Number of Signatures: " + numberOfSignatures);
  });
})();
JAVASCRIPT

這個簡潔的Node.js腳本使用IronPDF程式庫打開名為"sample-contract-signed.pdf"的PDF文件。 利用signatureCount。 結果計數被記錄到控制台中,為檢索和顯示指定PDF文件中存在的數位簽名數量提供了一種直接有效的方法。此代碼展示了IronPDF讓開發者能夠與PDF文件互動並程式化地提取有價值信息的簡單性。

如何在Node.js中簽署PDF文件,圖3:簽名數量 簽名數量

5. 結論

總之,Node.js和IronPDF結合,證明是解決PDF文件管理領域中各種挑戰的一個強大解決方案。 從初步探討Node.js中程式化簽署PDF的重要性,到詳細介紹如何利用IronPDF進行動態PDF生成和數位簽名應用,本指南旨在為開發者配備高效文件處理的基本工具。

IronPDF程式庫的安裝過程和數位簽署與驗證PDF以及計算數位簽名的實用示範,強調了這種組合提供的靈活性和簡單性。 通過無縫結合Node.js和IronPDF的優勢,開發者可以提升處理PDF文件的能力,確保在多樣化的應用場景中安全且簡化的操作。

IronPDF for Node.js為其用戶提供免費試用。 有關商業授權的更多詳細資訊,請造訪授權頁面。 要開始使用IronPDF,請造訪文件頁面。 Sign PDF Node.js的代碼範例可以在此Node.js範例鏈接中找到。 有更多關於如何使用IronPDF for Node.js的代碼範例,請訪問這些範例頁面

常見問題解答

在 Node.js 中簽署 PDF 的意義是什麼?

在 Node.js 中簽署 PDF 對於自動化和簡化數字簽名過程至關重要,特別是對於電子合同和法律文件。IronPDF for Node.js 提供安全簽署和管理 PDF 的功能。

如何在 Node.js 中以程式化方式簽署 PDF?

您可以使用 IronPDF 在 Node.js 中以程式化方式簽署 PDF。首先安裝 IronPDF 庫,然後使用 fromFile 方法加載您的 PDF,使用 signDigitalSignature 應用數字簽名,並在保存文件前使用 isSigned 進行驗證。

在 Node.js 中使用哪些方法來管理 PDF 簽名?

在 Node.js 中,IronPDF 提供多種方法來管理 PDF 簽名,包括用於應用簽名的 signDigitalSignature,用於驗證的 isSigned,以及用於計算文件中簽名數量的 signatureCount

如何安裝 IronPDF 以在 Node.js 應用程式中處理 PDF?

要安裝 IronPDF 以在 Node.js 應用程式中處理 PDF,請運行 npm install @Iron Software/ironpdf。您可能還需要 npm install @Iron Software/ironpdf-engine-windows-x64 用於 IronPDF 引擎。

Node.js PDF 庫的特點是什麼?

IronPDF for Node.js 提供包括動態 PDF 生成、文本和圖像操作、文件合併、拆分、加密和數字簽名管理等功能,使其成為 PDF 處理的全面解決方案。

我可以使用 Node.js 驗證 PDF 是否被數字簽署嗎?

是的,使用 Node.js 中的 IronPDF,您可以通過使用 isSigned 方法來驗證 PDF 是否已通過數字簽名,以確保文檔的真實性。

Node.js PDF 庫是否提供免費試用?

是的,IronPDF for Node.js 提供免費試用,允許開發人員在購買前探索其功能和能力以管理 PDF。

我可以在哪裡找到有關在 Node.js 中使用 PDF 庫的文件和示例?

您可以在 IronPDF 的官方網站上找到有關在 Node.js 中使用 IronPDF 的完整文檔和示例,該網站提供詳細的指南和教程。

在 Node.js 中使用 IronPDF 簽署 PDF 有什麼好處?

IronPDF 使在 Node.js 中簽署 PDF 的過程簡化,提供易於使用的方法來應用和驗證簽名,確保開發人員的文件管理安全且高效。

Darrius Serrant
全棧軟件工程師 (WebOps)

Darrius Serrant 擁有邁阿密大學計算機科學學士學位,目前任職於 Iron Software 的全栈 WebOps 市場營銷工程師。從小就迷上編碼,他認為計算既神秘又可接近,是創意和解決問題的完美媒介。

在 Iron Software,Darrius 喜歡創造新事物,並簡化複雜概念以便於理解。作為我們的駐場開發者之一,他也自願教學生,分享他的專業知識給下一代。

對 Darrius 來說,工作令人滿意因為它被重視且有實際影響。

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me