跳過到頁腳內容
使用 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文件進行數位簽章。 為此,我們將使用頂級的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 適用於 Node.js為使用者提供免費試用版。 有關商業許可的更多詳細信息,請訪問許可頁面。 若要開始使用IronPDF,請造訪文件頁面。 您可以在此Node.js範例連結中找到使用Node.js實作 PDF 簽名的程式碼範例。 有關如何使用IronPDF for Node.js的更多程式碼範例,請造訪這些範例頁面

常見問題解答

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

在 Node.js 中簽署 PDF 對於自動化和簡化數字簽名過程至關重要,特別是對於電子合同和法律文件。IronPDF 適用於 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 適用於 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 來說,工作令人滿意因為它被重視且有實際影響。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我