SelectPdf for .NET - Digital Signatures - VB.NET / ASP.NET Sample

Digital signatures can be added to a PDF document using PdfDigitalSignatureElement objects. Adding a digital signature requires a PKCS#12 certificate (provided as a .pfx or a .p12 file).

Sample certificate file:
Certificate

The following sample shows how to add a digital signature to a pdf document using SelectPdf Library for .NET.

IMPORTANT: Please note that the certificate used is a self signed certificate generated for testing purposes only. The signature will appear as not valid in a pdf viewer. Using a valid certificate will result in a valid digital signature.


Sample Code Vb.Net



Public Class digital_signatures
    Inherits System.Web.UI.Page

    Protected Sub BtnCreatePdf_Click(sender As Object, e As EventArgs)
        ' create a new pdf document
        Dim doc As New PdfDocument()

        ' add a new page to the document
        Dim page As PdfPage = doc.AddPage()

        ' get image path 
        ' the image will be used to display the digital signature over it
        Dim imgFile As String = Server.MapPath("~/files/logo.png")

        ' get certificate path
        Dim certFile As String = Server.MapPath("~/files/selectpdf.pfx")

        ' define a rendering result object
        Dim result As PdfRenderingResult

        ' create image element from file path 
        Dim img As New PdfImageElement(0, 0, imgFile)
        result = page.Add(img)

        ' get the #PKCS12 certificate from file
        Dim certificates As PdfDigitalCertificatesCollection = _
            PdfDigitalCertificatesStore.GetCertificates(certFile, "selectpdf")
        Dim certificate As PdfDigitalCertificate = certificates(0)

        ' create the digital signature object
        Dim signature As New PdfDigitalSignatureElement( _
            result.PdfPageLastRectangle, certificate)
        signature.Reason = "SelectPdf"
        signature.ContactInfo = "SelectPdf"
        signature.Location = "SelectPdf"
        page.Add(signature)

        ' save pdf document
        doc.Save(Response, False, "Sample.pdf")

        ' close pdf document
        doc.Close()
    End Sub

End Class