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

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

在現代文件管理領域,以程式設計方式簽署 PDF 文件已成為無數應用程式的必備功能。 Node.js是一個功能強大的伺服器端 JavaScript 執行環境,它為開發人員提供了一個多功能的平台,用於整合無縫的 PDF 簽名功能。 無論是電子合約、法律文件或其他重要文件,Sign PDF NodeJS 方法都能協助開發人員自動化和簡化數位簽章流程。 本介紹探討了使用 Node.js 簽署 PDF 的重要性,重點介紹了其在促進安全、高效和具有法律約束力的數位交易方面的重要性。

本文將探討如何使用Node.js對PDF文件進行數位簽章。 為此,我們將使用頂級的 Node.js PDF 庫IronPDF

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

在不斷發展的 Web 開發領域,Node.js 應用程式中動態無縫生成 PDF 的需求變得越來越重要。 IronPDF for Node.js 應運而生——它將 IronPDF 先進的 PDF 處理功能與 Node.js 的多功能性完美結合。 這項創新解決方案使開發人員能夠輕鬆建立、操作和渲染高品質的 PDF 文檔,並提供一套全面的工具包,用於執行從生成報告到製作動態發票等各種任務。 本介紹深入探討了 IronPDF for Node.js 的功能,重點介紹了它對於希望在 Node.js 專案中進行高效且功能豐富的 PDF 處理的開發人員來說,是一個寶貴的資產。

3. 安裝適用於 Node.js 的 IronPDF 庫

從 npm 安裝適用於 Node.js 的 IronPDF 庫,即可開始使用數位簽章對 PDF 文件進行簽章。 在控制台上執行以下命令來安裝 IronPDF 庫。

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

若要安裝使用 IronPDF 庫所必需的 IronPDF 引擎,請在控制台上執行以下命令。

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 庫處理 PDF 文件,重點處理名為"sample-contract-signed.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文件。 然後,它利用PdfDocument.fromFile方法,使用signatureCount非同步計算 PDF 中的數位簽章數量。 統計結果會記錄到控制台,從而提供了一種簡單有效的方法來檢索和顯示指定 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,請造訪文件頁面。 您可以在此 Node.js 範例連結中找到使用Node.js 實作 PDF 簽名的程式碼範例。 有關如何使用 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 用於計算文件中簽章的數量。

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

要安裝 IronPDF 用於在 Node.js 應用程式中處理 PDF,請執行 npm install @ironsoftware/ironpdf 。您可能還需要 npm install @ironsoftware/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 來說,工作令人滿意因為它被重視且有實際影響。