Imports SelectPdf

Namespace Controllers
    Public Class DigitalSignaturesController
        Inherits Controller

        ' GET: DigitalSignatures
        Public Function Index() As ActionResult
            Return View()
        End Function

        <HttpPost>
        Public Function SubmitAction(collection As FormCollection) As ActionResult
            ' 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
            Dim pdf As Byte() = doc.Save()

            ' close pdf document
            doc.Close()

            ' return resulted pdf document
            Dim fileResult As FileResult = New FileContentResult(pdf, "application/pdf")
            fileResult.FileDownloadName = "Document.pdf"
            Return fileResult
        End Function
    End Class
End Namespace