Wie man PDF-Dateien in C# mit IronPDF signiert | IronPDF Tutorial

Ein Leitfaden für Entwickler zum digitalen Signieren von PDFs mit C#

This article was translated from English: Does it need improvement?
Translated
View the article in English

Das Hinzufügen einer Signatur zu einem PDF-Dokument ist in vielen Anwendungen eine gängige Anforderung, aber "Signieren" kann unterschiedliche Bedeutungen haben. Für einige geht es darum, eine manipulationssichere digitale Signatur mit einem Sicherheitszertifikat anzuwenden. Für andere könnte es darum gehen, ein visuelles, handschriftliches Signaturbild auf ein Dokument zu stempeln oder ein interaktives Formularfeld hinzuzufügen, damit Benutzer elektronisch unterschreiben können.

Dieser Leitfaden bietet eine umfassende Anleitung für C#-Entwickler, um all diese Aufgaben mit der IronPDF for .NET-Bibliothek umzusetzen. Wir behandeln alles, von der Anwendung einer sicheren X509Certificate2 digitalen Signatur bis hin zum Stempeln grafischer Signaturen und Erstellen interaktiver Signaturfelder, um sicherzustellen, dass Ihre PDF-Dokumente authentisch, sicher und professionell sind.

Schnellstart: Müheloses digitales Signieren eines PDFs mit IronPDF

Beginnen Sie schnell mit IronPDF, um Ihre PDF-Dokumente mit einem einfachen, geradlinigen Prozess digital zu signieren. Dieses Beispiel zeigt, wie man ein .pfx-Zertifikat verwendet, um eine PDF-Datei zu authentifizieren und zu signieren, um die Dokumentintegrität und Authentizität sicherzustellen. Befolgen Sie diese Schritte, um das digitale Signieren nahtlos in Ihre Anwendung zu integrieren und Ihren Benutzern eine sichere und zuverlässige PDF-Funktionalität bereitzustellen.

Nuget IconLegen Sie jetzt mit NuGet los, um PDFs zu erstellen:

  1. Installieren Sie IronPDF mit dem NuGet-Paketmanager.

    PM > Install-Package IronPdf

  2. Kopieren Sie diesen Codeausschnitt und führen Sie ihn aus.

    new IronPdf.Signing.PdfSignature("certificate.pfx", "password").SignPdfFile("input.pdf");
  3. Bereitstellen zum Testen in Ihrer Live-Umgebung

    Beginnen Sie noch heute mit der Nutzung von IronPDF in Ihrem Projekt – mit einer kostenlosen Testversion.
    arrow pointer

Wie wende ich eine digitale Signatur mit einem Zertifikat auf ein PDF an?

Sie können eine digitale Signatur auf ein PDF-Dokument anwenden, indem Sie eine digitale Zertifikatsdatei (wie .pfx oder .p12) verwenden, um die Authentizität und Integrität des Dokuments zu garantieren. Dieser Prozess gewährleistet, dass das Dokument seit der Unterzeichnung nicht verändert wurde.

IronPDF bietet eine einfache API für diesen Zweck und unterstützt mehrere Möglichkeiten, eine digitale Signatur anzuwenden. Das Herzstück dieser Funktionalität ist die PdfSignature Klasse, die das Zertifikat und alle zugehörigen Metadaten der Signatur kapselt.

Unterzeichnungsmethode Beschreibung
Sign Signiert eine PDF-Datei mit einem PdfSignature-Objekt, das Sie erstellen und konfigurieren.
SignWithFile Signiert eine PDF-Datei mit einer digitalen Signaturzertifikatsdatei (.pfx oder .p12), die sich auf der Festplatte befindet.
SignWithStore Signiert eine PDF-Datei mit einer digitalen Signatur aus dem Zertifikatspeicher Ihres Computers, die durch ihre Daumenabdruck-ID identifiziert wird.

Verwendung eines X509Certificate2-Objekts

Für maximale Kontrolle können Sie ein X509Certificate2-Objekt aus Ihrer Zertifikatsdatei erstellen. IronPDF entspricht vollständig dem X509Certificate2-Standard und bietet eine robuste und sichere Methode für die Implementierung digitaler Signaturen. Beim Erstellen des Zertifikatsobjekts muss darauf geachtet werden, dass die X509KeyStorageFlags auf Exportable gesetzt sind, da dies von den zugrunde liegenden kryptografischen APIs erforderlich ist.

Install-Package IronPdf
using IronPdf;
using IronPdf.Signing;
using System.Security.Cryptography.X509Certificates;

// Create a new PDF from an HTML string for demonstration.
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Signed Document</h1><p>This document has been digitally signed.</p>");

// Load the certificate from a .pfx file with its password.
// The X509KeyStorageFlags.Exportable flag is crucial for allowing the private key to be used in the signing process.
var cert = new X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable);

// Create a PdfSignature object using the loaded certificate.
var signature = new PdfSignature(cert);

// Apply the signature to the PDF document.
pdf.Sign(signature);

// Save the securely signed PDF document.
pdf.SaveAs("Signed.pdf");
using IronPdf;
using IronPdf.Signing;
using System.Security.Cryptography.X509Certificates;

// Create a new PDF from an HTML string for demonstration.
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Signed Document</h1><p>This document has been digitally signed.</p>");

// Load the certificate from a .pfx file with its password.
// The X509KeyStorageFlags.Exportable flag is crucial for allowing the private key to be used in the signing process.
var cert = new X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable);

// Create a PdfSignature object using the loaded certificate.
var signature = new PdfSignature(cert);

// Apply the signature to the PDF document.
pdf.Sign(signature);

// Save the securely signed PDF document.
pdf.SaveAs("Signed.pdf");
Imports IronPdf
Imports IronPdf.Signing
Imports System.Security.Cryptography.X509Certificates

' Create a new PDF from an HTML string for demonstration.
Private renderer = New ChromePdfRenderer()
Private pdf = renderer.RenderHtmlAsPdf("<h1>Signed Document</h1><p>This document has been digitally signed.</p>")

' Load the certificate from a .pfx file with its password.
' The X509KeyStorageFlags.Exportable flag is crucial for allowing the private key to be used in the signing process.
Private cert = New X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable)

' Create a PdfSignature object using the loaded certificate.
Private signature = New PdfSignature(cert)

' Apply the signature to the PDF document.
pdf.Sign(signature)

' Save the securely signed PDF document.
pdf.SaveAs("Signed.pdf")
$vbLabelText   $csharpLabel

Der oben stehende Code generiert zuerst ein einfaches PDF. Anschließend wird eine .pfx-Zertifikatsdatei in ein X509Certificate2-Objekt geladen. Dieses Objekt, das die digitale Identität darstellt, wird an den PdfSignature-Konstruktor übergeben. Schließlich wendet die pdf.Sign-Methode diese Signatur auf das Dokument an, bevor es gespeichert wird. For more information on the X509Certificate2 class, you can refer to the official Microsoft documentation.

Zusätzliche Details zu einer digitalen Signatur hinzufügen

Eine digitale Signatur kann mehr als nur das Zertifikat enthalten; Sie können reichhaltige Metadaten einbetten, um Kontext zur Signatur bereitzustellen. Dazu gehören der Ort der Signatur, der Grund, die Kontaktinformationen und ein sicherer Zeitstempel von einer vertrauenswürdigen Behörde.

Das Hinzufügen dieser Details verbessert die Prüfspur des Dokuments und liefert Prüfern wertvolle Informationen. IronPDF unterstützt auch Zeitstempelserver, die moderne SHA256 und SHA512 Hash-Algorithmen verwenden.

using IronPdf;
using IronPdf.Signing;
using IronSoftware.Drawing;
using System;

// Load an existing PDF document to be signed.
var pdf = PdfDocument.FromFile("invoice.pdf");

// Create a PdfSignature object directly from the certificate file and password.
var signature = new PdfSignature("IronSoftware.pfx", "123456");

// Add detailed metadata to the signature for a comprehensive audit trail.
signature.SignatureDate = DateTime.Now;
signature.SigningContact = "legal@ironsoftware.com";
signature.SigningLocation = "Chicago, USA";
signature.SigningReason = "Contractual Agreement";

// Add a secure timestamp from a trusted Time Stamp Authority (TSA).
// This provides cryptographic proof of the signing time.
signature.TimeStampUrl = new Uri("[http://timestamp.digicert.com](http://timestamp.digicert.com)");
signature.TimestampHashAlgorithm = TimestampHashAlgorithms.SHA256;

// Apply a visual appearance to the signature. (More on this in the next section)
signature.SignatureImage = new PdfSignatureImage("assets/visual-signature.png", 0, new Rectangle(350, 750, 200, 100));

// Sign the PDF document with the configured signature object.
pdf.Sign(signature);

// Save the final, signed PDF document.
pdf.SaveAs("DetailedSignature.pdf");
using IronPdf;
using IronPdf.Signing;
using IronSoftware.Drawing;
using System;

// Load an existing PDF document to be signed.
var pdf = PdfDocument.FromFile("invoice.pdf");

// Create a PdfSignature object directly from the certificate file and password.
var signature = new PdfSignature("IronSoftware.pfx", "123456");

// Add detailed metadata to the signature for a comprehensive audit trail.
signature.SignatureDate = DateTime.Now;
signature.SigningContact = "legal@ironsoftware.com";
signature.SigningLocation = "Chicago, USA";
signature.SigningReason = "Contractual Agreement";

// Add a secure timestamp from a trusted Time Stamp Authority (TSA).
// This provides cryptographic proof of the signing time.
signature.TimeStampUrl = new Uri("[http://timestamp.digicert.com](http://timestamp.digicert.com)");
signature.TimestampHashAlgorithm = TimestampHashAlgorithms.SHA256;

// Apply a visual appearance to the signature. (More on this in the next section)
signature.SignatureImage = new PdfSignatureImage("assets/visual-signature.png", 0, new Rectangle(350, 750, 200, 100));

// Sign the PDF document with the configured signature object.
pdf.Sign(signature);

// Save the final, signed PDF document.
pdf.SaveAs("DetailedSignature.pdf");
Imports IronPdf
Imports IronPdf.Signing
Imports IronSoftware.Drawing
Imports System

' Load an existing PDF document to be signed.
Private pdf = PdfDocument.FromFile("invoice.pdf")

' Create a PdfSignature object directly from the certificate file and password.
Private signature = New PdfSignature("IronSoftware.pfx", "123456")

' Add detailed metadata to the signature for a comprehensive audit trail.
signature.SignatureDate = DateTime.Now
signature.SigningContact = "legal@ironsoftware.com"
signature.SigningLocation = "Chicago, USA"
signature.SigningReason = "Contractual Agreement"

' Add a secure timestamp from a trusted Time Stamp Authority (TSA).
' This provides cryptographic proof of the signing time.
signature.TimeStampUrl = New Uri("[http://timestamp.digicert.com](http://timestamp.digicert.com)")
signature.TimestampHashAlgorithm = TimestampHashAlgorithms.SHA256

' Apply a visual appearance to the signature. (More on this in the next section)
signature.SignatureImage = New PdfSignatureImage("assets/visual-signature.png", 0, New Rectangle(350, 750, 200, 100))

' Sign the PDF document with the configured signature object.
pdf.Sign(signature)

' Save the final, signed PDF document.
pdf.SaveAs("DetailedSignature.pdf")
$vbLabelText   $csharpLabel

Sie bemerken möglicherweise ein Warnsymbol in einigen PDF-Viewern, wenn das Signaturzertifikat nicht im vertrauenswürdigen Speicher des Systems vorhanden ist. Um ein grünes Häkchen zu bekommen, muss das Zertifikat zu den vertrauenswürdigen Identitäten des Viewers hinzugefügt werden.

Wie füge ich eine visuelle Darstellung zu einer digitalen Signatur hinzu?

Während eine digitale Signatur kryptografisch in das PDF eingebettet ist, ist es oft nützlich, eine visuelle Darstellung auf der Seite zu haben. Dies kann ein Firmenlogo, ein handschriftliches Signaturbild oder andere Grafiken sein. IronPDF macht es einfach, ein Bild zu einem PdfSignature-Objekt hinzuzufügen.

Sie können ein Bild von einer Datei oder einem Stream laden und es präzise auf jeder Seite des PDFs positionieren. Unterstützte Bildformate sind PNG, JPEG, GIF, BMP, TIFF und WebP.

using IronPdf.Signing;
using IronSoftware.Drawing;

// This example demonstrates various ways to add a visual image to a PDF signature.

// Create a PdfSignature object.
var signature = new PdfSignature("IronSoftware.pfx", "123456");

// Define the position and size for the signature image on the first page (index 0).
var signatureRectangle = new Rectangle(350, 750, 200, 100);

// Option 1: Set the SignatureImage property directly.
signature.SignatureImage = new PdfSignatureImage("assets/visual-signature.png", 0, signatureRectangle);

// Option 2: Use the LoadSignatureImageFromFile method.
signature.LoadSignatureImageFromFile("assets/visual-signature.png", 0, signatureRectangle);

// Option 3: Load an image from a stream. This is useful for images generated in memory.
AnyBitmap image = AnyBitmap.FromFile("assets/visual-signature.png");
using (var imageStream = image.ToStream())
{
    signature.LoadSignatureImageFromStream(imageStream, 0, signatureRectangle);
}

// After configuring the signature image, apply it to a PDF.
var pdf = PdfDocument.FromFile("invoice.pdf");
pdf.Sign(signature);
pdf.SaveAs("VisualSignature.pdf");
</span><br class="ProseMirror-trailingBreak">
using IronPdf.Signing;
using IronSoftware.Drawing;

// This example demonstrates various ways to add a visual image to a PDF signature.

// Create a PdfSignature object.
var signature = new PdfSignature("IronSoftware.pfx", "123456");

// Define the position and size for the signature image on the first page (index 0).
var signatureRectangle = new Rectangle(350, 750, 200, 100);

// Option 1: Set the SignatureImage property directly.
signature.SignatureImage = new PdfSignatureImage("assets/visual-signature.png", 0, signatureRectangle);

// Option 2: Use the LoadSignatureImageFromFile method.
signature.LoadSignatureImageFromFile("assets/visual-signature.png", 0, signatureRectangle);

// Option 3: Load an image from a stream. This is useful for images generated in memory.
AnyBitmap image = AnyBitmap.FromFile("assets/visual-signature.png");
using (var imageStream = image.ToStream())
{
    signature.LoadSignatureImageFromStream(imageStream, 0, signatureRectangle);
}

// After configuring the signature image, apply it to a PDF.
var pdf = PdfDocument.FromFile("invoice.pdf");
pdf.Sign(signature);
pdf.SaveAs("VisualSignature.pdf");
</span><br class="ProseMirror-trailingBreak">
Imports IronPdf.Signing
Imports IronSoftware.Drawing

' This example demonstrates various ways to add a visual image to a PDF signature.

' Create a PdfSignature object.
Private signature = New PdfSignature("IronSoftware.pfx", "123456")

' Define the position and size for the signature image on the first page (index 0).
Private signatureRectangle = New Rectangle(350, 750, 200, 100)

' Option 1: Set the SignatureImage property directly.
signature.SignatureImage = New PdfSignatureImage("assets/visual-signature.png", 0, signatureRectangle)

' Option 2: Use the LoadSignatureImageFromFile method.
signature.LoadSignatureImageFromFile("assets/visual-signature.png", 0, signatureRectangle)

' Option 3: Load an image from a stream. This is useful for images generated in memory.
Dim image As AnyBitmap = AnyBitmap.FromFile("assets/visual-signature.png")
Using imageStream = image.ToStream()
	signature.LoadSignatureImageFromStream(imageStream, 0, signatureRectangle)
End Using

' After configuring the signature image, apply it to a PDF.
Dim pdf = PdfDocument.FromFile("invoice.pdf")
pdf.Sign(signature)
pdf.SaveAs("VisualSignature.pdf")
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'</span><br class="ProseMirror-trailingBreak">
$vbLabelText   $csharpLabel

Dieser Code zeigt drei gleichwertige Möglichkeiten, eine visuelle Komponente zu einer digitalen Signatur hinzuzufügen. Egal, ob Sie ein Bild auf der Festplatte oder im Speicher haben, Sie können es einfach auf das PDF als Teil des Signaturprozesses stempeln. Dies schließt die Lücke zwischen unsichtbarer kryptografischer Sicherheit und sichtbarer Dokumentgenehmigung.

Wie kann ich die Dokumentberechtigungen nach der Signatur steuern?

Wenn Sie ein Dokument signieren, möchten Sie möglicherweise angeben, welche Änderungen, wenn überhaupt, danach erlaubt sind. Zum Beispiel möchten Sie das Dokument möglicherweise vollständig sperren oder nur Benutzern erlauben, Formulare auszufüllen. IronPDF lässt Sie diese Berechtigungen mithilfe der SignaturePermissions-Enumeration festlegen.

Das Festlegen von Signaturberechtigungen ist ein wichtiger Teil der Verwaltung des Lebenszyklus eines Dokuments. Es stellt sicher, dass die Integrität des Dokuments nach Ihrer Signatur gewahrt bleibt. Wenn ein Benutzer eine nicht erlaubte Aktion durchführt, wird die Signatur ungültig.

SignaturePermissions Mitglied Definition
NoChangesAllowed Änderungen jeglicher Art sind nicht erlaubt. Das Dokument ist effektiv gesperrt.
FormFillingAllowed Es ist nur das Ausfüllen vorhandener Formularfelder und das Unterschreiben erlaubt.
AnmerkungenUndFormularausfüllenErlaubt Ermöglicht das Ausfüllen von Formularen, das Unterschreiben und das Erstellen oder Ändern von Kommentaren.

Saving and Signing a Specific PDF Revision

PDFs can store a history of changes, much like a version control system. This is known as incremental saving. When you sign a PDF, the signature applies to a specific revision of the document. This is crucial for workflows where a document goes through multiple stages of approval.

In the following example, we load a PDF, make edits, and then sign the current revision while allowing only form-filling as a future change. We use SaveAsRevision to commit the current state to the document's history before saving the file.

using IronPdf.Signing;

// Load a PDF file with change tracking enabled.
var pdf = PdfDocument.FromFile("annual_census.pdf", ChangeTrackingModes.EnableChangeTracking);

// Placeholder for edits: You might add text, fill forms, or add annotations here.
// For example: pdf.Annotations.Add(new TextAnnotation(...));

// Sign the current state of the document using SignWithFile for convenience.
// We set permissions to allow further signatures and form filling.
pdf.SignWithFile(
    "assets/IronSignature.p12", 
    "password", 
    SignaturePermissions.AdditionalSignaturesAndFormFillingAllowed);

// Save the current state as a distinct revision within the PDF's history.
PdfDocument pdfWithRevision = pdf.SaveAsRevision();

// Save the final PDF with its full revision history to a new file.
pdfWithRevision.SaveAs("annual_census_signed.pdf");
using IronPdf.Signing;

// Load a PDF file with change tracking enabled.
var pdf = PdfDocument.FromFile("annual_census.pdf", ChangeTrackingModes.EnableChangeTracking);

// Placeholder for edits: You might add text, fill forms, or add annotations here.
// For example: pdf.Annotations.Add(new TextAnnotation(...));

// Sign the current state of the document using SignWithFile for convenience.
// We set permissions to allow further signatures and form filling.
pdf.SignWithFile(
    "assets/IronSignature.p12", 
    "password", 
    SignaturePermissions.AdditionalSignaturesAndFormFillingAllowed);

// Save the current state as a distinct revision within the PDF's history.
PdfDocument pdfWithRevision = pdf.SaveAsRevision();

// Save the final PDF with its full revision history to a new file.
pdfWithRevision.SaveAs("annual_census_signed.pdf");
Imports IronPdf.Signing

' Load a PDF file with change tracking enabled.
Private pdf = PdfDocument.FromFile("annual_census.pdf", ChangeTrackingModes.EnableChangeTracking)

' Placeholder for edits: You might add text, fill forms, or add annotations here.
' For example: pdf.Annotations.Add(new TextAnnotation(...));

' Sign the current state of the document using SignWithFile for convenience.
' We set permissions to allow further signatures and form filling.
pdf.SignWithFile("assets/IronSignature.p12", "password", SignaturePermissions.AdditionalSignaturesAndFormFillingAllowed)

' Save the current state as a distinct revision within the PDF's history.
Dim pdfWithRevision As PdfDocument = pdf.SaveAsRevision()

' Save the final PDF with its full revision history to a new file.
pdfWithRevision.SaveAs("annual_census_signed.pdf")
$vbLabelText   $csharpLabel

Understanding incremental saves is key to advanced PDF workflows. While a simple viewer might only show the latest version, a tool like Adobe Acrobat can reveal the entire revision history, showing who signed which version and what changes were made between signatures. IronPDF gives you full programmatic control over this process.

For businesses managing complex document workflows that require high security and compliance, a comprehensive solution may be needed. Iron Software offers Iron Suite, which includes IronPDF for signing and manipulation, plus other libraries for a wide range of document processing tasks, available for a single one-time payment.

How Can I Manage and Verify Signatures Across Revisions?

A PDF document can have multiple signatures applied across its various revisions. IronPDF provides tools to manage this history effectively.

  • Roll Back to a Previous Revision: You can revert a document to an earlier state using the GetRevision method. This will discard all changes and signatures made after that revision.
  • Verify All Signatures: The VerifySignatures method checks the validity of all signatures across all revisions of the document. It returns true only if every signature is valid and no unauthorized changes have been made.
  • Remove Signatures: The RemoveSignatures method will strip all digital signatures from every revision of the document, creating a clean, unsigned version.
// Load a PDF with a complex signature history.
var pdf = PdfDocument.FromFile("multi_signed_report.pdf");

// Verify all signatures across all revisions.
bool allSignaturesValid = pdf.VerifySignatures();
Console.WriteLine($"All signatures are valid: {allSignaturesValid}");

// Roll back to the first revision (index 0).
if (pdf.RevisionCount &gt; 1)
{
    PdfDocument firstRevision = pdf.GetRevision(0);
    firstRevision.SaveAs("report_first_revision.pdf");
}

// Create a completely unsigned version of the document.
pdf.RemoveSignatures();
pdf.SaveAs("report_unsigned.pdf");
// Load a PDF with a complex signature history.
var pdf = PdfDocument.FromFile("multi_signed_report.pdf");

// Verify all signatures across all revisions.
bool allSignaturesValid = pdf.VerifySignatures();
Console.WriteLine($"All signatures are valid: {allSignaturesValid}");

// Roll back to the first revision (index 0).
if (pdf.RevisionCount &gt; 1)
{
    PdfDocument firstRevision = pdf.GetRevision(0);
    firstRevision.SaveAs("report_first_revision.pdf");
}

// Create a completely unsigned version of the document.
pdf.RemoveSignatures();
pdf.SaveAs("report_unsigned.pdf");
' Load a PDF with a complex signature history.
Dim pdf = PdfDocument.FromFile("multi_signed_report.pdf")

' Verify all signatures across all revisions.
Dim allSignaturesValid As Boolean = pdf.VerifySignatures()
Console.WriteLine($"All signatures are valid: {allSignaturesValid}")

' Roll back to the first revision (index 0).
If pdf.RevisionCount And gt; 1 Then
	Dim firstRevision As PdfDocument = pdf.GetRevision(0)
	firstRevision.SaveAs("report_first_revision.pdf")
End If

' Create a completely unsigned version of the document.
pdf.RemoveSignatures()
pdf.SaveAs("report_unsigned.pdf")
$vbLabelText   $csharpLabel

How Do I Stamp a Handwritten Signature onto a PDF?

Sometimes, you don't need the cryptographic security of a digital signature but simply want to apply a visual, electronic signature, like a scanned handwritten signature. This is often referred to as stamping. IronPDF can do this using its Watermark or Stamp features.

Let's start with a sample invoice PDF and a .png image of a handwritten signature.

The original invoice PDF before stamping a signature.

Here is the signature image we will apply:

An image of an handwritten signature A sample handwritten signature image.

The following code uses the Watermark property to stamp this image onto the bottom-right corner of the PDF.

using IronPdf.Editing;

// Load the existing PDF document.
var pdf = PdfDocument.FromFile("invoice.pdf");

// Create an HtmlStamp containing our signature image.
var signatureStamp = new HtmlStamp("&lt;img src='assets/signature.png'/&gt;")
{
    // Configure the stamp's position and appearance.
    VerticalAlignment = VerticalAlignment.Bottom,
    HorizontalAlignment = HorizontalAlignment.Right,
    Margin = 10, // Add some space from the edge.
    Opacity = 90 // Make it slightly transparent.
};

// Apply the stamp to all pages of the PDF.
pdf.ApplyStamp(signatureStamp);

// Save the modified PDF document.
pdf.SaveAs("official_invoice.pdf");
using IronPdf.Editing;

// Load the existing PDF document.
var pdf = PdfDocument.FromFile("invoice.pdf");

// Create an HtmlStamp containing our signature image.
var signatureStamp = new HtmlStamp("&lt;img src='assets/signature.png'/&gt;")
{
    // Configure the stamp's position and appearance.
    VerticalAlignment = VerticalAlignment.Bottom,
    HorizontalAlignment = HorizontalAlignment.Right,
    Margin = 10, // Add some space from the edge.
    Opacity = 90 // Make it slightly transparent.
};

// Apply the stamp to all pages of the PDF.
pdf.ApplyStamp(signatureStamp);

// Save the modified PDF document.
pdf.SaveAs("official_invoice.pdf");
Imports IronPdf.Editing

' Load the existing PDF document.
Private pdf = PdfDocument.FromFile("invoice.pdf")

' Create an HtmlStamp containing our signature image.
Dim signatureStamp = New HtmlStamp("&lt;img src='assets/signature.png'/&gt;")
If True Then
	' Configure the stamp's position and appearance.
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: VerticalAlignment = VerticalAlignment.Bottom, HorizontalAlignment = HorizontalAlignment.Right, Margin = 10, Opacity = 90
	10, Opacity = 90 ' Make it slightly transparent.
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: VerticalAlignment = VerticalAlignment.Bottom, HorizontalAlignment = HorizontalAlignment.Right, Margin = 10, Opacity
	HorizontalAlignment.Right, Margin = 10, Opacity
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: VerticalAlignment = VerticalAlignment.Bottom, HorizontalAlignment = HorizontalAlignment.Right, Margin
	VerticalAlignment.Bottom, HorizontalAlignment = HorizontalAlignment.Right, Margin
	VerticalAlignment = VerticalAlignment.Bottom, HorizontalAlignment
End If

' Apply the stamp to all pages of the PDF.
pdf.ApplyStamp(signatureStamp)

' Save the modified PDF document.
pdf.SaveAs("official_invoice.pdf")
$vbLabelText   $csharpLabel

Stamped PDF Result

After running the code, the signature image is stamped onto the document, creating a visually signed invoice.

The final PDF with the handwritten signature image stamped in the bottom-right corner.

How Can I Add an Interactive Signature Field to a PDF?

For documents that need to be signed by an end-user in a PDF viewer like Adobe Acrobat, you can add an interactive signature form field. This creates an empty, clickable area that prompts the user to apply their own digital signature.

You can create a SignatureFormField and add it to the PDF's form collection. You have precise control over its location and size on the page.

using IronPdf.Forms;
using IronSoftware.Drawing;

// Create a new PDF to add the signature field to.
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("&lt;h1&gt;Please Sign Below&lt;/h1&gt;");

// Define the properties for the signature form field.
string fieldName = "ClientSignature";
int pageIndex = 0; // Add to the first page.
var fieldRect = new Rectangle(50, 200, 300, 100); // Position: (x, y), Size: (width, height)

// Create the SignatureFormField object.
var signatureField = new SignatureFormField(fieldName, pageIndex, fieldRect);

// Add the signature field to the PDF's form.
pdf.Form.Add(signatureField);

// Save the PDF with the new interactive signature field.
pdf.SaveAs("interactive_signature.pdf");
using IronPdf.Forms;
using IronSoftware.Drawing;

// Create a new PDF to add the signature field to.
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("&lt;h1&gt;Please Sign Below&lt;/h1&gt;");

// Define the properties for the signature form field.
string fieldName = "ClientSignature";
int pageIndex = 0; // Add to the first page.
var fieldRect = new Rectangle(50, 200, 300, 100); // Position: (x, y), Size: (width, height)

// Create the SignatureFormField object.
var signatureField = new SignatureFormField(fieldName, pageIndex, fieldRect);

// Add the signature field to the PDF's form.
pdf.Form.Add(signatureField);

// Save the PDF with the new interactive signature field.
pdf.SaveAs("interactive_signature.pdf");
Imports IronPdf.Forms
Imports IronSoftware.Drawing

' Create a new PDF to add the signature field to.
Private renderer = New ChromePdfRenderer()
Private pdf = renderer.RenderHtmlAsPdf("&lt;h1&gt;Please Sign Below&lt;/h1&gt;")

' Define the properties for the signature form field.
Private fieldName As String = "ClientSignature"
Private pageIndex As Integer = 0 ' Add to the first page.
Private fieldRect = New Rectangle(50, 200, 300, 100) ' Position: (x, y), Size: (width, height)

' Create the SignatureFormField object.
Private signatureField = New SignatureFormField(fieldName, pageIndex, fieldRect)

' Add the signature field to the PDF's form.
pdf.Form.Add(signatureField)

' Save the PDF with the new interactive signature field.
pdf.SaveAs("interactive_signature.pdf")
$vbLabelText   $csharpLabel

When a user opens this PDF, they will see a clickable field, allowing them to complete the signing process using their own digital ID. You can learn more about creating and managing interactive forms in our How-To guide on creating PDF forms.

Unsigned signature An unsigned, interactive signature field added programmatically to a PDF document.

Retrieving the Signer Name from Verified Signatures

To obtain the common name of the certificate owner who signed a signature, we can use the VerifiedSignature class to access the SignerName property. Below is a code snippet demonstrating how to accomplish this.

:path=/static-assets/pdf/content-code-examples/how-to/signing-find-signer-name.cs
using IronPdf;
using System;

// Import the Signed PDF report
var pdf = PdfDocument.FromFile("multi_signed_report.pdf");

// Using GetVerifiedSignatures() obtain a list of `VerifiedSignature` objects from the PDF
pdf.GetVerifiedSignatures().ForEach(signature =>
{
    // Print out the SignerName of each `VerifiedSignature` object
    Console.WriteLine($"SignatureName: {signature.SignerName}");
});
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Nach dem Importieren der signierten PDF-Datei verwenden wir die Methode GetVerifiedSignatures, um eine Liste von VerifiedSignature-Objekten innerhalb des Berichts abzurufen und den SignerName für jede Signatur zu drucken.

Hinweis:Bitte beachten Sie, dass dieser Wert aus dem Subject Distinguished Name (SubjectDN) des Zertifikats extrahiert wird und null zurückgibt, wenn das CN-Feld nicht vorhanden ist.

Nächste Schritte

Diese Anleitung hat die leistungsstarken und flexiblen PDF-Signierfunktionen von IronPDF gezeigt. Ganz gleich, ob Sie sichere digitale Signaturen mit detaillierten Metadaten anwenden, Dokumentrevisionen verwalten, visuelle Signaturen stempeln oder interaktive Formulare erstellen möchten, IronPDF bietet eine umfassende und entwicklerfreundliche API, um diese Aufgabe zu bewältigen.

Um weitere Informationen zu erhalten, können Sie die IronPDF-Bibliothek für .NET herunterladen und eine kostenlose Testlizenz erhalten, um alle Funktionen in Ihren Projekten zu testen.

Möchten Sie sehen, was Sie sonst noch tun können? Besuchen Sie unsere Tutorial-Seite hier: PDFs signieren und sichern

Häufig gestellte Fragen

Wie kann ich eine digitale Signatur auf ein PDF mit einer Zertifikatsdatei in C# anwenden?

Um eine digitale Signatur auf ein PDF mit einer Zertifikatsdatei in C# anzuwenden, können Sie IronPDF nutzen. Laden Sie Ihr Zertifikat in ein X509Certificate2-Objekt und verwenden Sie die PdfSignature-Klasse von IronPDF, um es mit der Sign-Methode auf Ihr Dokument anzuwenden.

Was sind die Vorteile der Verwendung einer digitalen Signatur für PDF-Dokumente?

Digitale Signaturen bieten Authentifizierung und Integrität für PDF-Dokumente. Durch die Verwendung von IronPDF können Sie sicherstellen, dass Ihr Dokument von einer verifizierten Quelle signiert wurde und seit dem Aufbringen der Signatur nicht verändert wurde.

Wie kann ich eine handschriftliche Signatur zu einem PDF in C# hinzufügen?

IronPDF erlaubt es Ihnen, eine handschriftliche Signatur hinzuzufügen, indem Sie ein Bild Ihrer Unterschrift auf das PDF stempeln. Sie können die ApplyStamp-Methode mit einem HtmlStamp verwenden und die Bilddatei angeben, um dies zu erreichen.

Wie ist der Ablauf zum Hinzufügen eines interaktiven Signaturfeldes zu einem PDF?

Um ein interaktives Signaturfeld zu einem PDF hinzuzufügen, erstellen Sie ein SignatureFormField-Objekt mit dem gewünschten Namen, Seite und Standort. Fügen Sie dieses Feld der PdfDocument.Form-Sammlung mit IronPDF hinzu, bevor Sie das Dokument speichern.

Kann ich Berechtigungen auf einem signierten PDF steuern?

Ja, beim Signieren eines PDFs mit IronPDF können Sie SignaturePermissions angeben, um zu steuern, welche Änderungen nach der Signierung erlaubt sind. Optionen sind NoChangesAllowed, FormFillingAllowed und AnnotationsAndFormFillingAllowed.

Wie überprüfe ich die Authentizität der digitalen Signatur eines PDFs?

IronPDF bietet eine VerifySignatures-Methode, die die Gültigkeit aller Signaturen in einem PDF überprüft. Sie gibt einen booleschen Wert zurück, der angibt, ob alle Signaturen authentisch und nicht manipuliert sind.

Was ist das inkrementelle Speichern in PDFs und warum ist es nützlich?

Das inkrementelle Speichern in PDFs ermöglicht es, mehrere Dokumentversionen in einer einzigen Datei zu speichern. Diese Funktion ist nützlich, um eine Historie der Änderungen zu führen und sicherzustellen, dass jede digitale Signatur der korrekten Dokumentversion zum Zeitpunkt der Signierung entspricht.

Wie kann ich Metadaten zu einer digitalen Signatur in einem PDF hinzufügen?

Mit IronPDF können Sie digitale Signaturen mit Metadaten wie dem Signaturgrund, Standort und Zeitstempel anreichern. Dies kann beim Erstellen eines PdfSignature-Objekts festgelegt werden, um eine detaillierte Dokumentation des Signaturprozesses zu ermöglichen.

Kann ich die digitalen Signaturfunktionen von IronPDF in .NET 10-Anwendungen verwenden?

Ja. IronPDF unterstützt .NET 10 vollständig, sodass der gleiche auf X509Certificate2 basierende digitale Signaturworkflow, der in diesem Artikel gezeigt wird, in .NET 10-Konsolen-, Web- und Dienstanwendungen funktioniert. Erstellen Sie einfach ein .NET 10-Projekt, installieren Sie das neueste IronPdf-Paket von NuGet und verwenden Sie den Signiercode erneut, um unsichtbare oder sichtbare Signaturen, Signaturbilder und Signaturformularfelder anzuwenden.

Jacob Mellor, Chief Technology Officer @ Team Iron
Chief Technology Officer

Jacob Mellor ist Chief Technology Officer bei Iron Software und ein visionärer Ingenieur, der führend in der C# PDF-Technologie ist. Als ursprünglicher Entwickler der Iron Software-Kerncodebasis hat er die Produktarchitektur des Unternehmens seit seiner Gründung gestaltet und zusammen mit CEO Cameron Rimington in ein Unternehmen ...

Weiterlesen
Rezensiert von
Jeff Fritz
Jeffrey T. Fritz
Principal Program Manager - .NET Community Team
Jeff ist außerdem Principal Program Manager für das .NET- und Visual Studio-Team. Er ist der ausführende Produzent der .NET Conf Virtual Conference Series und moderiert ‚Fritz and Friends‘, einen Livestream für Entwickler, der zweimal wöchentlich ausgestrahlt wird. Dort spricht er über Technik und schreibt gemeinsam mit den Zuschauern Code. Jeff schreibt Workshops, Präsentationen und plant Inhalte für die größten Microsoft-Entwicklerveranstaltungen, einschließlich Microsoft Build, Microsoft Ignite, .NET Conf und dem Microsoft MVP Summit.
Bereit anzufangen?
Nuget Downloads 16,493,056 | Version: 2025.11 gerade veröffentlicht