How to sign and verify updates to a PDF document
This code sample shows how you can sign PDF documents and how to handle updates to form data.
using (FileStream sourceFile = new FileStream(@"..\..\form.pdf", FileMode.Open, FileAccess.Read))
{
// open the form
Document document = new Document(sourceFile);
// file out the data fields
TextField projectNr = document.Fields["projectNumber"] as TextField;
projectNr.Value = "FF-235";
TextField sName = document.Fields["studentName"] as TextField;
sName.Value = "Bob Stapleton";
TextField sDate = document.Fields["studentDate"] as TextField;
sDate.Value = "April 18, 2007";
// retrieve the signature field
SignatureField sigField = document.Fields["studentSignature"] as SignatureField;
// open certicate store.
Pkcs12Store store = null;
using (FileStream storeFile = new FileStream(@"..\..\BobStapleton.pfx", FileMode.Open, FileAccess.Read))
{
store = new Pkcs12Store(storeFile, "studentpassword");
}
// let the factory decide which type should be used.
SignatureHandler handler = StandardSignatureHandler.Create(store);
// associate the handler with the signature field
sigField.SignatureHandler = handler;
// set optional info.
sigField.ContactInfo = "+31 (0)77 4748677";
sigField.Location = "The Netherlands";
sigField.Reason = "I hereby declare!";
// save the signed document - while saving, the handler
// will be called to sign the saved content
using (FileStream outFile = new FileStream(@"..\..\signedByStudent.pdf", FileMode.Create, FileAccess.ReadWrite))
{
document.Write(outFile);
}
}
// -----------------------------------------------------
// TEACHER FILLS OUT FINAL FIELDS AND SIGNS THE DOCUMENT
// -----------------------------------------------------
using (FileStream sourceFile = new FileStream(@"..\..\signedByStudent.pdf", FileMode.Open, FileAccess.Read))
{
// open the form
Document document = new Document(sourceFile);
// file out the data fields
TextField tName = document.Fields["teacherName"] as TextField;
tName.Value = "Max Boulton";
TextField tDate = document.Fields["teacherDate"] as TextField;
tDate.Value = "April 18, 2007";
// retrieve the signature field
SignatureField sigField = document.Fields["teacherSignature"] as SignatureField;
// open certicate store.
Pkcs12Store store = null;
using (FileStream storeFile = new FileStream(@"..\..\MaxBoulton.pfx", FileMode.Open, FileAccess.Read))
{
store = new Pkcs12Store(storeFile, "teacherpassword");
}
// let the factory decide which type should be used.
SignatureHandler handler = StandardSignatureHandler.Create(store);
// associate the handler with the signature field
sigField.SignatureHandler = handler;
// set optional info.
sigField.ContactInfo = "+31 (0)77 4748677";
sigField.Location = "The Netherlands";
sigField.Reason = "I hereby declare!";
// save the signed document - while saving, the handler
// will be called to sign the saved content
using (FileStream outFile = new FileStream(@"..\..\signedByTeacher.pdf", FileMode.Create, FileAccess.ReadWrite))
{
document.Write(outFile, DocumentWriteMode.AppendUpdate);
}
}
// ----------------------------------------------------
// ENUMERATE UPDATES IN DOCUMENT THAS BEEN SIGNED TWICE
// ----------------------------------------------------
using (FileStream sourceFile = new FileStream(@"..\..\signedByTeacher.pdf", FileMode.Open, FileAccess.Read))
{
// open the form
Document document = new Document(sourceFile);
// count the number of updates
//Console.WriteLine( "This document has {0} updates.", document.Updates.Count );
// save each update as a new PDF document
foreach (Update update in document.Updates)
{
string name = string.Format( @"..\..\signedByTeacher_{0}.pdf", update.Index );
using (FileStream updateFile = new FileStream(name, FileMode.Create, FileAccess.Write))
{
update.Write(updateFile);
}
}
}
// -----------------------------------------------------------
// ENUMERATE SIGNATURE FIELDS AND SAVE SIGNED UPDATE PER FIELD
// -----------------------------------------------------------
using (FileStream sourceFile = new FileStream(@"..\..\signedByTeacher.pdf", FileMode.Open, FileAccess.Read))
{
// open the form
Document document = new Document(sourceFile);
foreach (Field field in document.Fields)
{
// is this a signature field?
SignatureField sigField = field as SignatureField;
if (null != sigField)
{
// has it been signed?
if (sigField.IsSigned)
{
// save the update and name it after the field
string name = string.Format(@"..\..\{0}.pdf", sigField.FullName);
using (FileStream updateFile = new FileStream(name, FileMode.Create, FileAccess.Write))
{
sigField.SignedUpdate.Write(updateFile);
}
}
}
}
}
// ----------------------------------
// DUMP STATE OF EACH SIGNATURE FIELD
// ----------------------------------
using (FileStream inFile = new FileStream(@"..\..\signedByTeacher.pdf", FileMode.Open, FileAccess.Read))
{
// open form
Document document = new Document(inFile);
foreach (Field field in document.Fields)
{
// is this a signature field?
SignatureField sigField = field as SignatureField;
if (null != sigField)
{
Console.WriteLine("Field '{0}'", sigField.FullName);
// has it been signed?
if (sigField.IsSigned)
{
// verify, based on the default handlers.
bool verified = sigField.Verify();
Console.WriteLine(" -- {0}", verified ? "Verified" : "Not verified");
if (verified)
{
// has the document been modified after signing?
bool modified = sigField.DocumentModifiedAfterSigning;
Console.WriteLine(" -- {0}", modified ? "Modified after signing" : "Not modified after signing");
}
}
else
{
Console.WriteLine(" -- Not signed", sigField.FullName);
}
}
}
}
' -------------------------------------------
' STUDENT FILLS OUT FIELDS AND SIGNS DOCUMENT
' -------------------------------------------
Using sourceFile As New FileStream("..\..\form.pdf", FileMode.Open, FileAccess.Read)
' open the form
Dim document As New Document(sourceFile)
' file out the data fields
Dim projectNr As TextField = TryCast(document.Fields("projectNumber"), TextField)
projectNr.Value = "FF-235"
Dim sName As TextField = TryCast(document.Fields("studentName"), TextField)
sName.Value = "Bob Stapleton"
Dim sDate As TextField = TryCast(document.Fields("studentDate"), TextField)
sDate.Value = "April 18, 2007"
' retrieve the signature field
Dim sigField As SignatureField = TryCast(document.Fields("studentSignature"), SignatureField)
' open certicate store.
Dim store As Pkcs12Store = Nothing
Using storeFile As New FileStream("..\..\BobStapleton.pfx", FileMode.Open, FileAccess.Read)
store = New Pkcs12Store(storeFile, "studentpassword")
End Using
' let the factory decide which type should be used.
Dim handler As SignatureHandler = StandardSignatureHandler.Create(store)
' associate the handler with the signature field
sigField.SignatureHandler = handler
' set optional info.
sigField.ContactInfo = "+31 (0)77 4748677"
sigField.Location = "The Netherlands"
sigField.Reason = "I hereby declare!"
' save the signed document - while saving, the handler
' will be called to sign the saved content
Using outFile As New FileStream("..\..\signedByStudent.pdf", FileMode.Create, FileAccess.ReadWrite)
document.Write(outFile)
End Using
End Using
' -----------------------------------------------------
' TEACHER FILLS OUT FINAL FIELDS AND SIGNS THE DOCUMENT
' -----------------------------------------------------
Using sourceFile As New FileStream("..\..\signedByStudent.pdf", FileMode.Open, FileAccess.Read)
' open the form
Dim document As New Document(sourceFile)
' file out the data fields
Dim tName As TextField = TryCast(document.Fields("teacherName"), TextField)
tName.Value = "Max Boulton"
Dim tDate As TextField = TryCast(document.Fields("teacherDate"), TextField)
tDate.Value = "April 18, 2007"
' retrieve the signature field
Dim sigField As SignatureField = TryCast(document.Fields("teacherSignature"), SignatureField)
' open certicate store.
Dim store As Pkcs12Store = Nothing
Using storeFile As New FileStream("..\..\MaxBoulton.pfx", FileMode.Open, FileAccess.Read)
store = New Pkcs12Store(storeFile, "teacherpassword")
End Using
' let the factory decide which type should be used.
Dim handler As SignatureHandler = StandardSignatureHandler.Create(store)
' associate the handler with the signature field
sigField.SignatureHandler = handler
' set optional info.
sigField.ContactInfo = "+31 (0)77 4748677"
sigField.Location = "The Netherlands"
sigField.Reason = "I hereby declare!"
' save the signed document - while saving, the handler
' will be called to sign the saved content
Using outFile As New FileStream("..\..\signedByTeacher.pdf", FileMode.Create, FileAccess.ReadWrite)
document.Write(outFile, DocumentWriteMode.AppendUpdate)
End Using
End Using
' ----------------------------------------------------
' ENUMERATE UPDATES IN DOCUMENT THAS BEEN SIGNED TWICE
' ----------------------------------------------------
Using sourceFile As New FileStream("..\..\signedByTeacher.pdf", FileMode.Open, FileAccess.Read)
' open the form
Dim document As New Document(sourceFile)
' count the number of updates
'Console.WriteLine( "This document has {0} updates.", document.Updates.Count );
' save each update as a new PDF document
For Each update As Update In document.Updates
Dim name As String = String.Format("..\..\signedByTeacher_{0}.pdf", update.Index)
Using updateFile As New FileStream(name, FileMode.Create, FileAccess.Write)
update.Write(updateFile)
End Using
Next
End Using
' -----------------------------------------------------------
' ENUMERATE SIGNATURE FIELDS AND SAVE SIGNED UPDATE PER FIELD
' -----------------------------------------------------------
Using sourceFile As New FileStream("..\..\signedByTeacher.pdf", FileMode.Open, FileAccess.Read)
' open the form
Dim document As New Document(sourceFile)
For Each field As Field In document.Fields
' is this a signature field?
Dim sigField As SignatureField = TryCast(field, SignatureField)
If sigField IsNot Nothing Then
' has it been signed?
If sigField.IsSigned Then
' save the update and name it after the field
Dim name As String = String.Format("..\..\{0}.pdf", sigField.FullName)
Using updateFile As New FileStream(name, FileMode.Create, FileAccess.Write)
sigField.SignedUpdate.Write(updateFile)
End Using
End If
End If
Next
End Using
' ----------------------------------
' DUMP STATE OF EACH SIGNATURE FIELD
' ----------------------------------
Using inFile As New FileStream("..\..\signedByTeacher.pdf", FileMode.Open, FileAccess.Read)
' open form
Dim document As New Document(inFile)
For Each field As Field In document.Fields
' is this a signature field?
Dim sigField As SignatureField = TryCast(field, SignatureField)
If sigField IsNot Nothing Then
Console.WriteLine("Field '{0}'", sigField.FullName)
' has it been signed?
If sigField.IsSigned Then
' verify, based on the default handlers.
Dim verified As Boolean = sigField.Verify()
Console.WriteLine(" -- {0}", IIf(verified, "Verified", "Not verified"))
If verified Then
' has the document been modified after signing?
Dim modified As Boolean = sigField.DocumentModifiedAfterSigning
Console.WriteLine(" -- {0}", IIf(modified, "Modified after signing", "Not modified after signing"))
End If
Else
Console.WriteLine(" -- Not signed", sigField.FullName)
End If
End If
Next
End Using
No results
Add Long Term Validation (LTV) data to an existing signature
Render PDF to multipage color TIFF
Render PDF page to Skia surface
Render PDF page as PNG
How to downscale all images in a PDF
How to generate and export certificates
How to downscale all images in a PDF
Add Stamp to PDF
How to use a system font for rendering text
Customize the GUI interaction of a radio button
Customize the UI interaction of a check box
PDF to grayscale TIFF
How to reduce PDF file size
How do I create graphics with Icc based colors
Highlight fields in PDF
Add a note to PDF
Display PDF in a WPF app and stay responsive – the code
Draw interactively on a PDF page
Resize PDF pages
Verify a custom digital PDF signature
C# Print PDF documents from a WPF application
Extract glyph boxes from PDF
Use TrueType font collections
Layout text with MultilineTextShape
Calculate the height of a paragraph in PDF
Merge PDF files in C# .NET
How do I extract page destinations from bookmarks?
Clip PDF page content in C#
How do I use PDFControls.NET in a WPF application
Fill PDF form
Extract glyphs and sort by reading order
Add bookmarks to PDF
How to scale content of PDF
Create rectangles with rounded corners
Create text with decorations
Create layers in PDF and draw on each layer
Multipage TIFF to PDF
TIFF to PDF C#
Crop content on a PDF page
How to embed files in a PDF document
Remove graphics from PDF
Change the color inside a PDF
Create PDF in C#
Text formatting
Import FDF into PDF
Flatten PDF form
Digitally sign a PDF form in C# or VB.NET
Vector graphics in PDF
Translate PDF page content
Extract graphics from PDF
Determine the content bounding box
How to add page numbers to your PDF
Create / impose PDF 2-up
Search text in PDF
Append multiple PDF documents
Convert PDF to plain text
Flatten Markup Annotation
Add text field to PDF
Extract embedded files from PDF
Extract images from PDF
Add a Diagonal Watermark to PDF in C#
Fit image to PDF page
Add simple html text to PDF
Add multiline text to a PDF document
Add single-line text to PDF
Create a new digitally signed PDF document
PDF Viewer Preferences
Change page orientation PDF
Split PDF pages in C# and VB.NET
Append two or more existing PDF files
Change colors of black-and-white TIFF after converting from PDF
Determine if a PDF only contains images
Add footer to PDF
Convert SVG to PDF
Fill in a PDF form using MVC
C# render pdf in browser using MVC
Convert XHTML to PDF
Add hyperlink to PDF
Rotate a PDF page
Change the formatting of a numeric field
How to mirror PDF pages and other shapes
Fill in a template PDF document
How to add autosized text to PDF
Create formfields in PDF documents
Export FDF from PDF form
Add a link with an internal destination to PDF
Remove PDF security settings
Add a link to PDF with an external destination
How to sign and verify updates to a PDF document
Convert PDF to PNG using WPF
Embed TrueType font
Override MouseWheel event
Convert PDF to an image using a dither matrix
Font mapping
Convert PDF with layers to image
C# Print PDF Document
Render PDF with ResolveFont event handler
Render PDF to EMF
Convert PDF to XPS
How to create a thumbnail viewer
How to create a tiling for shapes in PDF
Add footer with left and right aligned text on same line
Convert PDF to JPG in C#
EMF to PDF as vector image
EMF to PDF as raster image
Replace field with image
Add a rubber stamp annotation with a custom icon
Create a text annotation in PDF with rich text
XhtmlParagraph and TrueType fonts
What is the resulting fontsize in PDF for rich text used in a SimpleXhtmlShape
Read and write meta data from PDF
Create a custom signature handler to sign and verify PDF documents
Merge PDF
Stitch PDF documents
Download and convert image to PDF
Convert TXT to PDF
Add barcodes to PDF
Convert PDF to multipage TIFF in C# .NET
Convert multiple PDF pages to bitmap
Render a PDF to bitmap
Bulleted list from XML and XSL
Tagged PDF
PDFKit.NET 5.0 – detailed changes to the API
Create tagged PDF
PDFKit.NET 5.0 and .NET Core
PDFKit.NET 5.0 and Xamarin
Dynamic XFA
PDFKit.NET 5.0 .NET Standard API
.NET Core console app on MacOS
Add tags to existing PDF
Read PDF tags
Merge XDP data with dynamic XFA form
Fill XFA form and export XDP data
Fill and save dynamic XFA form
Use PDFKit.NET 5.0 with a Xamarin.Forms app
Use multiple licenses
Licensing and .NET Standard
Reduce PDF size
Generate PDF form from XML
Generate PDF with local images from XML with Xamarin.iOS
Disable submit button after submitting
Write Document to HttpResponse