Imports SelectPdf

Namespace Controllers
    Public Class AdvancedPdfSecurityController
        Inherits Controller

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

        <HttpPost>
        Public Function SubmitAction(collection As FormCollection) As ActionResult
            ' the initial file
            Dim file As String = Server.MapPath("~/files/doc1.pdf")

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

            ' load the pdf document using the advanced security manager
            Dim security As New PdfSecurityManager()
            security.Load(file)

            ' encryption algorithm and key length
            security.EncryptionAlgorithm = PdfEncryptionAlgorithm.AES
            security.EncryptionKeySize = PdfEncryptionKeySize.EncryptKey256Bit

            ' set document passwords
            security.OwnerPassword = "test1"
            security.UserPassword = "test2"

            'set document permissions
            security.CanAssembleDocument = False
            security.CanCopyContent = True
            security.CanEditAnnotations = True
            security.CanEditContent = True
            security.CanFillFields = True
            security.CanPrint = True

            ' add the digital signature
            security.Sign(certFile, "selectpdf")

            ' save pdf document
            Dim pdf As Byte() = security.Save()

            ' close pdf document
            security.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