This example demonstrates how to sign a digital signature to a new PDF file.
Like a conventional handwritten signature, identifies the person signing a document. Unlike a handwritten signature, a digital signature is difficult to forge because it contains encrypted information that is unique to the signer and easily verified. It is usually password protected and can be stored on your computer in PKCS #12 file format (Personal Information Exchange File, PFX).
We create a new document with digital signature
Signature have some properties
ContactInfo
LocationInfo
Reason
Document certification status
Clicking generate button will lauch pdf with digital aignature which can be validated
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using OfficeComponent.Pdf;
using OfficeComponent.Pdf.Graphics;
using OfficeComponent.Pdf.Security;
namespace OfficeComponent.Samples
{
class DigitalSignatureSignNewPDFExample : ExampleBase
{
public bool Certificated { get; set; }
public DigitalSignatureSignNewPDFExample(string commonDataPath, string outputDir)
: base(commonDataPath, outputDir)
{
}
public DigitalSignatureSignNewPDFExample(string commonDataPath, string outputDir, string xmlFile)
: base(commonDataPath, outputDir, xmlFile)
{
}
public override string Execute()
{
// Create a new instance of PdfDocument class.
PdfDocument doc = new PdfDocument();
PdfDigitalSignature signature = null;
PdfBitmap bmp = null;
PdfGraphics g;
PdfPage page = doc.Pages.Add();
PdfSolidBrush brush = new PdfSolidBrush(Color.Black);
PdfPen pen = new PdfPen(brush, 0.2f);
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12, PdfFontStyle.Regular);
try
{
PdfCertificate pdfCert = new PdfCertificate(CommonDataPath + "\\Key.pfx", "password");
signature = new PdfDigitalSignature(page, pdfCert, "Signature");
bmp = new PdfBitmap(CommonDataPath + "\\Logo.gif");
signature.Bounds = new RectangleF(new PointF(5, 5), bmp.PhysicalDimension);
signature.ContactInfo = "contact@domain.com";
signature.LocationInfo = "California";
signature.Reason = "I am author of this document.";
if (Certificated)
signature.Certificated = true;
else
signature.Certificated = false;
g = signature.Appearence.Normal.Graphics;
}
catch (System.ArgumentNullException exc)
{
g = signature.Appearence.Normal.Graphics;
ShowWarning("Warning Certificate not found \"Cannot sign This Document\": " + exc.Message);
//Draw the Text at specified location.
g.DrawString("Warning this document is not signed", font, brush, new PointF(0, 20));
g.DrawString("Create a self signed Digital ID to sign this document", font, brush, new PointF(20, 40));
g.DrawLine(pen, new PointF(0, 100), new PointF(page.GetClientSize().Width, 200));
g.DrawLine(pen, new PointF(0, 200), new PointF(page.GetClientSize().Width, 100));
}
string validto = "Valid To: " + signature.Certificate.ValidTo.ToString();
string validfrom = "Valid From: " + signature.Certificate.ValidFrom.ToString();
g.DrawImage(bmp, 0, 0);
doc.Pages[0].Graphics.DrawString("Certificate Info", font, pen, brush, 0, 70);
doc.Pages[0].Graphics.DrawString(validfrom, font, pen, brush, 20, 90);
doc.Pages[0].Graphics.DrawString(validto, font, pen, brush, 20, 110);
doc.Pages[0].Graphics.DrawString("- Click on the signature on this page to validate Signature\n- Click document status icon on the bottom left of the acrobat reader to check Document Status", font, pen, brush, 0, 150);
// Save the PDF file.
var outputPath = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + ".pdf");
doc.Save(outputPath);
doc.Close(true);
return outputPath;
}
}
}
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text
Imports OfficeComponent.Pdf
Imports OfficeComponent.Pdf.Graphics
Imports OfficeComponent.Pdf.Security
Namespace OfficeComponent.Samples
Friend Class DigitalSignatureSignNewPDFExample
Inherits ExampleBase
Private privateCertificated As Boolean
Public Property Certificated() As Boolean
Get
Return privateCertificated
End Get
Set(ByVal value As Boolean)
privateCertificated = value
End Set
End Property
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
MyBase.New(commonDataPath, outputDir)
End Sub
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
MyBase.New(commonDataPath, outputDir, xmlFile)
End Sub
Public Overrides Function Execute() As String
' Create a new instance of PdfDocument class.
Dim doc As New PdfDocument()
Dim signature As PdfDigitalSignature = Nothing
Dim bmp As PdfBitmap = Nothing
Dim g As PdfGraphics
Dim page As PdfPage = doc.Pages.Add()
Dim brush As New PdfSolidBrush(Color.Black)
Dim pen As New PdfPen(brush, 0.2F)
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 12, PdfFontStyle.Regular)
Try
Dim pdfCert As New PdfCertificate(CommonDataPath & "\Key.pfx", "password")
signature = New PdfDigitalSignature(page, pdfCert, "Signature")
bmp = New PdfBitmap(CommonDataPath & "\Logo.gif")
signature.Bounds = New RectangleF(New PointF(5, 5), bmp.PhysicalDimension)
signature.ContactInfo = "contact@domain.com"
signature.LocationInfo = "California"
signature.Reason = "I am author of this document."
If Certificated Then
signature.Certificated = True
Else
signature.Certificated = False
End If
g = signature.Appearence.Normal.Graphics
Catch exc As System.ArgumentNullException
g = signature.Appearence.Normal.Graphics
ShowWarning("Warning Certificate not found ""Cannot sign This Document"": " & exc.Message)
'Draw the Text at specified location.
g.DrawString("Warning this document is not signed", font, brush, New PointF(0, 20))
g.DrawString("Create a self signed Digital ID to sign this document", font, brush, New PointF(20, 40))
g.DrawLine(pen, New PointF(0, 100), New PointF(page.GetClientSize().Width, 200))
g.DrawLine(pen, New PointF(0, 200), New PointF(page.GetClientSize().Width, 100))
End Try
Dim validto As String = "Valid To: " & signature.Certificate.ValidTo.ToString()
Dim validfrom As String = "Valid From: " & signature.Certificate.ValidFrom.ToString()
g.DrawImage(bmp, 0, 0)
doc.Pages(0).Graphics.DrawString("Certificate Info", font, pen, brush, 0, 70)
doc.Pages(0).Graphics.DrawString(validfrom, font, pen, brush, 20, 90)
doc.Pages(0).Graphics.DrawString(validto, font, pen, brush, 20, 110)
doc.Pages(0).Graphics.DrawString("- Click on the signature on this page to validate Signature" & vbLf & "- Click document status icon on the bottom left of the acrobat reader to check Document Status", font, pen, brush, 0, 150)
' Save the PDF file.
Dim outputPath = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & ".pdf")
doc.Save(outputPath)
doc.Close(True)
Return outputPath
End Function
End Class
End Namespace