如何在 ASP.NET Core 中新增 PDF 數位簽章
在 ASP.NET Core 中為 PDF 添加數位簽章可以保護文件完整性,確認簽署者的身份,並使文件在大多數司法管轄區具有法律效力。 使用 IronPDF,您可以使用憑證檔案在伺服器端對 PDF 進行簽名,新增可見的簽名影像,並嵌入互動式簽名表單欄位——所有這些都只需幾行 C# 程式碼即可完成。
從 NuGet 安裝庫,取得您的 .pfx 證書,並依照下列範例建立可用於生產的文件簽章工作流程。
Install-Package IronPdf
dotnet add package IronPdf
Install-Package IronPdf
dotnet add package IronPdf
您也可以透過 Visual Studio 中的 NuGet 套件管理器安裝它,或直接從IronPDF 產品頁面下載它。 立即開始免費試用,無需任何承諾即可體驗所有簽名功能。
什麼是PDF數位簽名?為什麼它如此重要?
數位簽章是一種加密機制,它將簽署者的身分與特定版本的文件綁定在一起。 它與簡單的電子簽名(例如打字簽名或掃描圖像)不同,因為它使用公鑰加密技術來創建可驗證、防篡改的印章。
當有人對 PDF 檔案進行數位簽章時,簽章軟體會產生文件內容的雜湊值,並使用簽署者的私鑰對該雜湊值進行加密。 產生的簽章會與簽署者的公鑰憑證一起嵌入 PDF 檔案中。 任何支援數位簽章的 PDF 閱讀器(包括 Adobe Acrobat Reader 和現代瀏覽器內建的 PDF 檢視器)都可以使用公鑰解密雜湊值,並將其與新計算的文件雜湊值進行比較。 如果雜湊值匹配,則該文件是真實且未被篡改的。
數位簽章為何具有法律意義
在大多數國家,數位簽章的文件與手寫紙本合約具有同等的法律效力。 歐盟的eIDAS 法規和美國的ESIGN 法案都承認,在滿足某些條件的情況下,電子簽名具有法律約束力。 基於憑證的數位簽章滿足了這些要求中的最高等級。
數位簽章在 ASP.NET Core 中的作用
在 ASP.NET Core 應用程式中使用伺服器端簽名,可以在處理過程中自動對文件進行簽名-無需客戶端軟體。 合約、發票、合規報告和保密協議都可以在產生後立即簽署,簽署後的文件可以直接返回給使用者或儲存在文件管理系統中。 IronPDF 處理從 HTML 到 PDF 渲染到加密簽名的整個流程,因此您可以實現工作流程而無需觸及底層 PDF 規範細節。
如何在 ASP.NET Core 專案中安裝 IronPDF?
獲得可用簽名環境的最快方法是使用 NuGet 套件。 在 Visual Studio 中開啟套件管理員控制台並執行:
Install-Package IronPdf
dotnet add package IronPdf
Install-Package IronPdf
dotnet add package IronPdf
或者,在解決方案資源管理器中右鍵單擊項目,選擇"管理 NuGet 套件",搜尋 IronPdf,然後按一下"安裝"。 安裝完成後,在控制器或服務類別的頂部新增以下 using 指令:
using IronPdf;
using IronPdf.Signing;
using IronPdf;
using IronPdf.Signing;
Imports IronPdf
Imports IronPdf.Signing
IronPDF 的目標平台是 .NET 8 和 .NET 9/10,因此它能夠自然地融入現代 ASP.NET Core 專案。 有關首次設定的詳細步驟,請參閱IronPDF 快速入門指南。
如何在 ASP.NET Core 中使用憑證檔案對 PDF 進行簽署?
最常見的簽章方法是使用 .pfx 或 .p12 憑證檔案。這些文件將私鑰和公鑰憑證鏈打包到一個受密碼保護的檔案中。您可以從受信任的憑證授權單位 (CA)(例如DigiCert或GlobalSign )取得此類文件,或產生自簽名憑證用於開發和測試。
以下範例從 HTML 產生 PDF,並套用基於憑證的數位簽章:
using IronPdf;
using IronPdf.Signing;
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapPost("/sign-with-certificate", (IWebHostEnvironment env) =>
{
// Generate a PDF from HTML content
var renderer = new ChromePdfRenderer();
var document = renderer.RenderHtmlAsPdf("<h1>Contract Agreement</h1><p>Terms and conditions...</p>");
// Locate the PFX certificate on the server
string certPath = Path.Combine(env.ContentRootPath, "Certificates", "certificate.pfx");
// Build the signature object
var signature = new PdfSignature(certPath, "yourPassword")
{
SigningContact = "legal@yourcompany.com",
SigningLocation = "Chicago, IL, USA",
SigningReason = "Document Approval"
};
// Apply the signature and save
document.Sign(signature);
string outputPath = Path.Combine(Path.GetTempPath(), "signed-contract.pdf");
document.SaveAs(outputPath);
return Results.File(outputPath, "application/pdf", "signed-contract.pdf");
});
app.Run();
using IronPdf;
using IronPdf.Signing;
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapPost("/sign-with-certificate", (IWebHostEnvironment env) =>
{
// Generate a PDF from HTML content
var renderer = new ChromePdfRenderer();
var document = renderer.RenderHtmlAsPdf("<h1>Contract Agreement</h1><p>Terms and conditions...</p>");
// Locate the PFX certificate on the server
string certPath = Path.Combine(env.ContentRootPath, "Certificates", "certificate.pfx");
// Build the signature object
var signature = new PdfSignature(certPath, "yourPassword")
{
SigningContact = "legal@yourcompany.com",
SigningLocation = "Chicago, IL, USA",
SigningReason = "Document Approval"
};
// Apply the signature and save
document.Sign(signature);
string outputPath = Path.Combine(Path.GetTempPath(), "signed-contract.pdf");
document.SaveAs(outputPath);
return Results.File(outputPath, "application/pdf", "signed-contract.pdf");
});
app.Run();
Imports IronPdf
Imports IronPdf.Signing
Imports Microsoft.AspNetCore.Mvc
Dim builder = WebApplication.CreateBuilder(args)
Dim app = builder.Build()
app.MapPost("/sign-with-certificate", Function(env As IWebHostEnvironment)
' Generate a PDF from HTML content
Dim renderer = New ChromePdfRenderer()
Dim document = renderer.RenderHtmlAsPdf("<h1>Contract Agreement</h1><p>Terms and conditions...</p>")
' Locate the PFX certificate on the server
Dim certPath As String = Path.Combine(env.ContentRootPath, "Certificates", "certificate.pfx")
' Build the signature object
Dim signature = New PdfSignature(certPath, "yourPassword") With {
.SigningContact = "legal@yourcompany.com",
.SigningLocation = "Chicago, IL, USA",
.SigningReason = "Document Approval"
}
' Apply the signature and save
document.Sign(signature)
Dim outputPath As String = Path.Combine(Path.GetTempPath(), "signed-contract.pdf")
document.SaveAs(outputPath)
Return Results.File(outputPath, "application/pdf", "signed-contract.pdf")
End Function)
app.Run()
ChromePdfRenderer 將任何有效的 HTML 字串或 URL 轉換為 PDF。 PdfSignature 建構函式接受憑證路徑和密碼,選用屬性(SigningContact、SigningLocation、SigningReason)新增 PDF 檢視器在簽章面板中顯示的元資料。 Sign 方法嵌入了加密簽名,而 SaveAs 將簽名檔案寫入磁碟。
從記憶體返回已簽署文件
如果您不想將臨時文件寫入磁碟,請將 PDF 儲存到 MemoryStream 並直接從控制器操作返回它:
var stream = new MemoryStream();
document.SaveAs(stream);
stream.Position = 0;
return Results.File(stream, "application/pdf", "signed-contract.pdf");
var stream = new MemoryStream();
document.SaveAs(stream);
stream.Position = 0;
return Results.File(stream, "application/pdf", "signed-contract.pdf");
Dim stream As New MemoryStream()
document.SaveAs(stream)
stream.Position = 0
Return Results.File(stream, "application/pdf", "signed-contract.pdf")
這種方法適用於高吞吐量 API,因為臨時檔案管理會增加不必要的開銷。
有關完整的屬性參考,請參閱PdfSignature API 文件。
如何在PDF中加入可見的簽名圖片?
加密簽名可以保護文件的完整性,但在渲染後的 PDF 中是不可見的。 許多工作流程——尤其是涉及合約或信函的工作流程——還需要在紙上列印可見的表示形式,例如掃描的手寫簽名或公司印章。
IronPDF 透過 LoadSignatureImageFromFile 方法支援此功能。 此方法接受影像路徑、頁面索引以及定義可見簽名位置和尺寸的 IronSoftware.Drawing.Rectangle:
using IronPdf;
using IronPdf.Signing;
using IronSoftware.Drawing;
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapPost("/sign-with-visible-image", (IWebHostEnvironment env) =>
{
// Load an existing PDF (for example, an invoice template)
string pdfPath = Path.Combine(env.ContentRootPath, "Documents", "invoice.pdf");
var document = PdfDocument.FromFile(pdfPath);
// Paths to the certificate and the signature image
string certPath = Path.Combine(env.ContentRootPath, "Certificates", "certificate.pfx");
string imagePath = Path.Combine(env.ContentRootPath, "Images", "signature.png");
// Define where the visible signature appears (x, y, width, height in points)
var signatureArea = new Rectangle(50, 680, 200, 80);
var signature = new PdfSignature(certPath, "yourPassword");
signature.LoadSignatureImageFromFile(imagePath, pageIndex: 0, signatureArea);
document.Sign(signature);
string outputPath = Path.Combine(Path.GetTempPath(), "signed-invoice.pdf");
document.SaveAs(outputPath);
return Results.File(outputPath, "application/pdf", "signed-invoice.pdf");
});
app.Run();
using IronPdf;
using IronPdf.Signing;
using IronSoftware.Drawing;
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapPost("/sign-with-visible-image", (IWebHostEnvironment env) =>
{
// Load an existing PDF (for example, an invoice template)
string pdfPath = Path.Combine(env.ContentRootPath, "Documents", "invoice.pdf");
var document = PdfDocument.FromFile(pdfPath);
// Paths to the certificate and the signature image
string certPath = Path.Combine(env.ContentRootPath, "Certificates", "certificate.pfx");
string imagePath = Path.Combine(env.ContentRootPath, "Images", "signature.png");
// Define where the visible signature appears (x, y, width, height in points)
var signatureArea = new Rectangle(50, 680, 200, 80);
var signature = new PdfSignature(certPath, "yourPassword");
signature.LoadSignatureImageFromFile(imagePath, pageIndex: 0, signatureArea);
document.Sign(signature);
string outputPath = Path.Combine(Path.GetTempPath(), "signed-invoice.pdf");
document.SaveAs(outputPath);
return Results.File(outputPath, "application/pdf", "signed-invoice.pdf");
});
app.Run();
Imports IronPdf
Imports IronPdf.Signing
Imports IronSoftware.Drawing
Imports Microsoft.AspNetCore.Mvc
Dim builder = WebApplication.CreateBuilder(args)
Dim app = builder.Build()
app.MapPost("/sign-with-visible-image", Function(env As IWebHostEnvironment)
' Load an existing PDF (for example, an invoice template)
Dim pdfPath As String = Path.Combine(env.ContentRootPath, "Documents", "invoice.pdf")
Dim document = PdfDocument.FromFile(pdfPath)
' Paths to the certificate and the signature image
Dim certPath As String = Path.Combine(env.ContentRootPath, "Certificates", "certificate.pfx")
Dim imagePath As String = Path.Combine(env.ContentRootPath, "Images", "signature.png")
' Define where the visible signature appears (x, y, width, height in points)
Dim signatureArea As New Rectangle(50, 680, 200, 80)
Dim signature As New PdfSignature(certPath, "yourPassword")
signature.LoadSignatureImageFromFile(imagePath, pageIndex:=0, signatureArea)
document.Sign(signature)
Dim outputPath As String = Path.Combine(Path.GetTempPath(), "signed-invoice.pdf")
document.SaveAs(outputPath)
Return Results.File(outputPath, "application/pdf", "signed-invoice.pdf")
End Function)
app.Run()
可見的簽名影像會合成到您提供的指定頁面上的座標位置。加密簽名會同時應用於整個文檔,因此您只需一次操作即可獲得安全性和視覺確認。
如果圖像存在於記憶體中(例如,從資料庫或雲端儲存中取得),請改用 LoadSignatureImageFromStream。 如需深入了解視覺手語選項,請參閱PDF 手語使用指南。
如何為外部簽署者建立簽名表單欄位?
在某些工作流程中,文件由您的系統創建,但必須由外部方(客戶、合作夥伴或監管機構)簽署。 與其事先從對方收集證明,不如在 PDF 中嵌入一個專門的簽名表單字段,然後將文件發送給他們。 收件者使用 Adobe Acrobat Reader 或其他相容的檢視器開啟 PDF 文件,點擊簽名字段,然後套用自己的憑證或電子簽名。
using IronPdf;
using IronPdf.Forms;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/generate-signable-form", (IWebHostEnvironment env) =>
{
// Render the document that requires a client signature
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(@"
<h1>Client Service Agreement</h1>
<p>Please review the terms below and sign in the field provided.</p>
<p>By signing, you confirm acceptance of all listed terms and conditions.</p>
");
// Define the signature field: name, page, x, y, width, height (in points)
var signatureField = new SignatureFormField(
"ClientSignature",
pageIndex: 0,
x: 50,
y: 600,
width: 300,
height: 100
);
pdf.Form.Add(signatureField);
string outputPath = Path.Combine(Path.GetTempPath(), "client-agreement.pdf");
pdf.SaveAs(outputPath);
return Results.File(outputPath, "application/pdf", "client-agreement.pdf");
});
app.Run();
using IronPdf;
using IronPdf.Forms;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/generate-signable-form", (IWebHostEnvironment env) =>
{
// Render the document that requires a client signature
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(@"
<h1>Client Service Agreement</h1>
<p>Please review the terms below and sign in the field provided.</p>
<p>By signing, you confirm acceptance of all listed terms and conditions.</p>
");
// Define the signature field: name, page, x, y, width, height (in points)
var signatureField = new SignatureFormField(
"ClientSignature",
pageIndex: 0,
x: 50,
y: 600,
width: 300,
height: 100
);
pdf.Form.Add(signatureField);
string outputPath = Path.Combine(Path.GetTempPath(), "client-agreement.pdf");
pdf.SaveAs(outputPath);
return Results.File(outputPath, "application/pdf", "client-agreement.pdf");
});
app.Run();
Imports IronPdf
Imports IronPdf.Forms
Imports Microsoft.AspNetCore.Builder
Imports Microsoft.AspNetCore.Hosting
Imports Microsoft.Extensions.Hosting
Dim builder = WebApplication.CreateBuilder(args)
Dim app = builder.Build()
app.MapGet("/generate-signable-form", Function(env As IWebHostEnvironment)
' Render the document that requires a client signature
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("
<h1>Client Service Agreement</h1>
<p>Please review the terms below and sign in the field provided.</p>
<p>By signing, you confirm acceptance of all listed terms and conditions.</p>
")
' Define the signature field: name, page, x, y, width, height (in points)
Dim signatureField = New SignatureFormField(
"ClientSignature",
pageIndex:=0,
x:=50,
y:=600,
width:=300,
height:=100
)
pdf.Form.Add(signatureField)
Dim outputPath As String = Path.Combine(Path.GetTempPath(), "client-agreement.pdf")
pdf.SaveAs(outputPath)
Return Results.File(outputPath, "application/pdf", "client-agreement.pdf")
End Function)
app.Run()
SignatureFormField 建構函數參數直接對應到頁面上欄位的位置。 當收件者開啟 PDF 檔案時,他們會看到一個清晰標示的方框,用於填寫他們的簽名。 填妥的表格隨後可以返回到您的系統中,您可以加載該表格,驗證嵌入的簽名,並將其存檔。
有關 PDF 表單處理(包括讀取已提交的表單資料)的更多信息,請參閱PDF 表單編輯指南。
如何透過程式驗證數位簽章?
文件簽署並寄回後,您可能需要核實簽名是否仍然有效,以及文件是否已更改。 IronPDF 透過 PdfDocument 物件公開簽名驗證:
using IronPdf;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/verify-signature", (IWebHostEnvironment env) =>
{
string signedPath = Path.Combine(env.ContentRootPath, "Documents", "signed-contract.pdf");
var document = PdfDocument.FromFile(signedPath);
// Retrieve all embedded signatures
var signatures = document.GetSignatures();
foreach (var sig in signatures)
{
bool isValid = sig.VerifySignature();
string status = isValid
? $"Valid -- signed by {sig.SignerName} on {sig.SigningTime:D}"
: "INVALID -- document may have been tampered with";
Console.WriteLine(status);
}
return Results.Ok(new { SignatureCount = signatures.Count });
});
app.Run();
using IronPdf;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/verify-signature", (IWebHostEnvironment env) =>
{
string signedPath = Path.Combine(env.ContentRootPath, "Documents", "signed-contract.pdf");
var document = PdfDocument.FromFile(signedPath);
// Retrieve all embedded signatures
var signatures = document.GetSignatures();
foreach (var sig in signatures)
{
bool isValid = sig.VerifySignature();
string status = isValid
? $"Valid -- signed by {sig.SignerName} on {sig.SigningTime:D}"
: "INVALID -- document may have been tampered with";
Console.WriteLine(status);
}
return Results.Ok(new { SignatureCount = signatures.Count });
});
app.Run();
Imports IronPdf
Imports Microsoft.AspNetCore.Builder
Imports Microsoft.AspNetCore.Hosting
Imports Microsoft.Extensions.Hosting
Dim builder = WebApplication.CreateBuilder(args)
Dim app = builder.Build()
app.MapGet("/verify-signature", Function(env As IWebHostEnvironment)
Dim signedPath As String = Path.Combine(env.ContentRootPath, "Documents", "signed-contract.pdf")
Dim document = PdfDocument.FromFile(signedPath)
' Retrieve all embedded signatures
Dim signatures = document.GetSignatures()
For Each sig In signatures
Dim isValid As Boolean = sig.VerifySignature()
Dim status As String = If(isValid,
$"Valid -- signed by {sig.SignerName} on {sig.SigningTime:D}",
"INVALID -- document may have been tampered with")
Console.WriteLine(status)
Next
Return Results.Ok(New With {.SignatureCount = signatures.Count})
End Function)
app.Run()
GetSignatures() 傳回 PDF 中嵌入的所有數位簽章的清單。 每個 PdfDigitalSignature 物件都會公開 VerifySignature()、簽署者姓名、簽名時間戳記和憑證鏈。 這些資訊足以建立審計追蹤或文件管理儀表板,以標記任何簽名損壞或過期的 PDF 文件。
生產環境中如何處理證書管理?
在開發過程中,將 .pfx 檔案儲存在檔案系統中是可行的,但不適合生產環境。 憑證文件包含私鑰,如果該文件洩露,則使用該金鑰簽署的每個文件都將面臨風險。
使用 Azure Key Vault
Azure Key Vault可讓您儲存和使用證書,而無需將私鑰離開金鑰庫。 .NET SDK 提供了一個 CertificateClient,用於下載公共憑證資訊。 對於將私鑰保存在金鑰保管庫中的實際簽章操作,您可以使用Azure.Security.KeyVault.Keys套件在伺服器端執行加密操作。
使用環境變數和金鑰
對於較小的項目,可以將憑證以 Base64 編碼字串的形式儲存在環境變數或ASP.NET Core Secrets Manager中,並在執行時間進行解碼:
string certBase64 = Environment.GetEnvironmentVariable("PDF_SIGNING_CERT")
?? throw new InvalidOperationException("PDF_SIGNING_CERT environment variable is not set.");
byte[] certBytes = Convert.FromBase64String(certBase64);
string certPassword = Environment.GetEnvironmentVariable("PDF_SIGNING_CERT_PASSWORD")
?? throw new InvalidOperationException("PDF_SIGNING_CERT_PASSWORD environment variable is not set.");
var signature = new PdfSignature(certBytes, certPassword);
string certBase64 = Environment.GetEnvironmentVariable("PDF_SIGNING_CERT")
?? throw new InvalidOperationException("PDF_SIGNING_CERT environment variable is not set.");
byte[] certBytes = Convert.FromBase64String(certBase64);
string certPassword = Environment.GetEnvironmentVariable("PDF_SIGNING_CERT_PASSWORD")
?? throw new InvalidOperationException("PDF_SIGNING_CERT_PASSWORD environment variable is not set.");
var signature = new PdfSignature(certBytes, certPassword);
Imports System
Dim certBase64 As String = Environment.GetEnvironmentVariable("PDF_SIGNING_CERT")
If certBase64 Is Nothing Then
Throw New InvalidOperationException("PDF_SIGNING_CERT environment variable is not set.")
End If
Dim certBytes As Byte() = Convert.FromBase64String(certBase64)
Dim certPassword As String = Environment.GetEnvironmentVariable("PDF_SIGNING_CERT_PASSWORD")
If certPassword Is Nothing Then
Throw New InvalidOperationException("PDF_SIGNING_CERT_PASSWORD environment variable is not set.")
End If
Dim signature = New PdfSignature(certBytes, certPassword)
這種模式使憑證脫離原始程式碼控制,並使輪換變得簡單——更新環境變數並重新啟動服務。
如何有效率地大量簽署多個PDF文件?
當您需要一次簽署數十份或數百份文件時(例如,在月末簽署整批發票),只需加載一次證書,並在所有文件中重複使用 PdfSignature 對象,即可減少開銷:
using IronPdf;
using IronPdf.Signing;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapPost("/batch-sign", (IWebHostEnvironment env) =>
{
string certPath = Path.Combine(env.ContentRootPath, "Certificates", "certificate.pfx");
var signature = new PdfSignature(certPath, "yourPassword")
{
SigningReason = "Batch Invoice Approval"
};
string[] invoicePaths = Directory.GetFiles(
Path.Combine(env.ContentRootPath, "Invoices"),
"*.pdf"
);
string outputDir = Path.Combine(env.ContentRootPath, "Signed");
Directory.CreateDirectory(outputDir);
foreach (string invoicePath in invoicePaths)
{
var doc = PdfDocument.FromFile(invoicePath);
doc.Sign(signature);
string outputFile = Path.Combine(outputDir, Path.GetFileName(invoicePath));
doc.SaveAs(outputFile);
}
return Results.Ok(new { SignedCount = invoicePaths.Length });
});
app.Run();
using IronPdf;
using IronPdf.Signing;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapPost("/batch-sign", (IWebHostEnvironment env) =>
{
string certPath = Path.Combine(env.ContentRootPath, "Certificates", "certificate.pfx");
var signature = new PdfSignature(certPath, "yourPassword")
{
SigningReason = "Batch Invoice Approval"
};
string[] invoicePaths = Directory.GetFiles(
Path.Combine(env.ContentRootPath, "Invoices"),
"*.pdf"
);
string outputDir = Path.Combine(env.ContentRootPath, "Signed");
Directory.CreateDirectory(outputDir);
foreach (string invoicePath in invoicePaths)
{
var doc = PdfDocument.FromFile(invoicePath);
doc.Sign(signature);
string outputFile = Path.Combine(outputDir, Path.GetFileName(invoicePath));
doc.SaveAs(outputFile);
}
return Results.Ok(new { SignedCount = invoicePaths.Length });
});
app.Run();
Imports IronPdf
Imports IronPdf.Signing
Dim builder = WebApplication.CreateBuilder(args)
Dim app = builder.Build()
app.MapPost("/batch-sign", Function(env As IWebHostEnvironment)
Dim certPath As String = Path.Combine(env.ContentRootPath, "Certificates", "certificate.pfx")
Dim signature As New PdfSignature(certPath, "yourPassword") With {
.SigningReason = "Batch Invoice Approval"
}
Dim invoicePaths As String() = Directory.GetFiles(
Path.Combine(env.ContentRootPath, "Invoices"),
"*.pdf"
)
Dim outputDir As String = Path.Combine(env.ContentRootPath, "Signed")
Directory.CreateDirectory(outputDir)
For Each invoicePath As String In invoicePaths
Dim doc = PdfDocument.FromFile(invoicePath)
doc.Sign(signature)
Dim outputFile As String = Path.Combine(outputDir, Path.GetFileName(invoicePath))
doc.SaveAs(outputFile)
Next
Return Results.Ok(New With {.SignedCount = invoicePaths.Length})
End Function)
app.Run()
在循環之外建立一次 PdfSignature 物件表示憑證檔案只會被讀取和解析一次。 每次迭代都會載入、簽名並儲存一個單獨的 PDF 檔案。 對於非常大的批次,請考慮使用 Parallel.ForEach 並行處理文件 -- 當每個 PdfDocument 實例隔離到單一執行緒時,IronPDF 的簽名操作是執行緒安全的。
如何排除常見的簽章錯誤?
"未找到證書"或"密碼無效"
使用 File.Exists(certPath) 仔細檢查憑證路徑,然後再傳遞給 PdfSignature。 證書密碼區分大小寫,且必須完全符合。 在開發過程中,通常會產生一個帶有簡單密碼的自簽名憑證; 在生產環境中,將密碼視為機密訊息,並從機密管理器中載入。
PDF檢視器中顯示"簽名無效"
Adobe Acrobat 中的"無效簽章"警告通常表示以下三種情況之一:(1)檢視器的憑證儲存區不信任該憑證;(2)簽章後文件已被修改;(3)簽章憑證已過期。 對於生產環境,請從受信任的 CA 取得證書,並確保系統時鐘同步。 對於開發工作,Adobe 提供了臨時信任自簽名憑證的說明。
呼叫 Sign() 函數後出現"文檔未簽署"的錯誤。
呼叫 document.Sign(signature) 會將文件標記為待簽名,但只有當您呼叫 SaveAs 或儲存到流中時,簽名才會最終完成。 請確保在 Sign 之後呼叫 save 方法,並驗證輸出檔案路徑是否可寫入。
有關完整的 API 詳細資訊和故障排除資源,請造訪IronPDF 文件中心和IronPDF 物件參考。 如果您需要協助, IronPDF 支援頁面會將您與工程團隊連結。
下一步計劃是什麼?
一旦了解了三個核心操作,ASP.NET Core 中的數位 PDF 簽章就變得非常簡單:載入憑證、呼叫 Sign 和儲存結果。 IronPDF 處理繁重的加密工作,讓您可以專注於應用程式的業務邏輯。
若要繼續建立您的文件工作流程,請探索以下相關主題:
-如何在 ASP.NET Core 中從 HTML 產生 PDF——大多數簽名工作流程的基礎 如何添加 PDF 密碼和權限——結合簽名和加密以實現最高安全性 如何合併和拆分 PDF 文件-在簽署前組裝多個文檔包 IronPDF 授權選項-選擇適合您部署需求的方案
- IronPDF NuGet 套件-- 查看最新版本和更新日誌
立即開始免費試用 IronPDF ,不到一小時即可產生您的第一個簽名 PDF 檔案。 如果您有任何問題或遇到特殊情況, Iron Software 支援團隊隨時為您提供協助,協助您建立可靠且符合法律規定的文件簽署工作流程。
常見問題解答
什麼是 ASP.NET Core 中的數位簽章?
ASP.NET Core 中的數位簽章就像是用來驗證 PDF 文件真實性和完整性的數位蠟封。它可以確保文件具有法律效力且未被篡改。
如何使用 IronPDF 為 PDF 文件新增數位簽章?
您可以使用 IronPDF 為 PDF 文件加入數位簽章,方法是加入證書並將其設定為簽署文件,以確保文件既安全又可驗證。
為什麼數位簽章對我的商業文件很重要?
數位簽章非常重要,因為它們可以驗證合約或發票等文件的真實性,並保持不變,從而保護您的企業免受潛在的法律風險。
我可以用 IronPDF 在 PDF 中建立互動式表格欄位嗎?
是的,IronPDF 可讓您在 PDF 中建立互動式表單欄位,這可以在 ASP.NET Core 應用程式中加強使用者互動並簡化文件流程。
是否可以在我的 PDF 文件中加入可見簽名?
是的,使用 IronPDF,您可以在 PDF 文件中添加可視簽名,讓收件人清楚瞭解文件已安全簽署和驗證。
哪些類型的憑證可用於 PDF 的數位簽章?
您可以在 PDF 中使用各種類型的憑證進行數位簽章,包括自簽憑證和由可信賴的憑證授權機構簽發的憑證,這取決於所需的安全性和信任等級。
IronPDF 如何確保 PDF 文件未被篡改?
IronPDF 透過使用數位簽章來驗證文件的完整性與真實性,確保 PDF 文件未被竄改,並在簽章後若有變更時提醒收件者。
我可以在 ASP.NET Core 應用程式中自動執行數位簽章程序嗎?
是的,您可以使用 IronPDF 在 ASP.NET Core 應用程式中自動執行數位簽章程序,它可以進行批次處理並整合至現有的工作流程中。



