SecurityHandlers.vb
''
'' This code is part of Document Solutions for PDF demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Pdf.Security

'' This sample demonstrates the use of Security.EncryptHandler and Security.DecryptHandler.
'' Security.DecryptHandler allows you to examine the security attributes of an existing PDF.
'' Security.EncryptHandler allows you to specify security attributes when saving a PDF.
'' DsPdf supports Standard Security Handlers revisions 2, 3 and 4 (as defined in the PDF Spec).
'' In this sample, we use StandardSecurityHandlerRev4 which provides the most options.
Public Class SecurityHandlers
    Function CreatePDF(ByVal stream As Stream) As Integer
        '' Sample passwords:
        Dim ownerPassword = "I'm the owner"
        Dim userPassword = "I'm a user"

        '' Step 1: Generate a document with some security attributes:
        Dim doc0 = New GcPdfDocument()
        Dim rc0 = Util.AddNote(
            "Demonstrating security:" + vbLf +
            "In this PDF, we specify certain encryption options," + vbLf +
            "and set owner and user passwords.",
            doc0.NewPage())

        '' Create a Rev4 security handler, set some rev4 specific props:
        Dim ssh4 = New StandardSecurityHandlerRev4() With {
            .EncryptionAlgorithm = EncryptionAlgorithm.AES,
            .EncryptStrings = True
        }
        '' StandardSecurityHandlerRev4 is derived from StandardSecurityHandlerRev3,
        '' so we can do this to make sure we touch rev3-specific properties only
        '' (the cast is for illustration only, you do not need it to set those props of course):
        If TypeOf ssh4 Is StandardSecurityHandlerRev3 Then
            Dim ssh3 = DirectCast(ssh4, StandardSecurityHandlerRev3)
            ssh3.EditingPermissions = EditingPermissions.AssembleDocument
            ssh3.PrintingPermissions = PrintingPermissions.LowResolution
        End If
        '' But StandardSecurityHandlerRev3 is NOT derived from StandardSecurityHandlerRev2,
        '' because some properties have similar meanings but different syntax, so this:
        '' if (ssh3 is StandardSecurityHandlerRev2 ssh2) { ... }
        '' will NOT work.

        '' Set some passwords:
        ssh4.OwnerPassword = ownerPassword
        ssh4.UserPassword = userPassword

        '' Assign the handler we created to the document so that it is used when saving the PDF:
        doc0.Security.EncryptHandler = ssh4

        '' Save the PDF in a temp file, so that we can load it:
        Dim fn = Path.GetTempFileName()
        doc0.Save(fn)

        '' Step 2: Load the generated PDf and examine its security attributes:
        Dim doc = New GcPdfDocument()
        Using fs As New FileStream(fn, FileMode.Open, FileAccess.Read)
            '' User password is needed to load the document:
            doc.Load(fs, userPassword)

            '' At this point we can examine doc.Security.DecryptHandler if it exists,
            '' but there is NO Security.EncryptHandler:
            If doc.Security.EncryptHandler IsNot Nothing Then
                Throw New Exception("This should not happen.")
            End If

            Dim dh = doc.Security.DecryptHandler
            If TypeOf dh Is StandardSecurityHandlerRev4 Then
                Dim dh_ssh4 = DirectCast(dh, StandardSecurityHandlerRev4)
                '' Make sure the loaded permissions are what we specified in Step 1:
                Util.AddNote(
                    String.Format("Security attributes that were in the loaded PDF's DecryptHandler:" + vbLf +
                        "EditingPermissions: {0}" + vbLf +
                        "PrintingPermissions: {1}",
                                  dh_ssh4.EditingPermissions, dh_ssh4.PrintingPermissions),
                    doc.Pages(0),
                    New RectangleF(72, rc0.Bottom + 36, 72 * 6, 72 * 2))
                '' This won't work, sorry:
                Dim noway = dh_ssh4.OwnerPassword
                If noway IsNot Nothing Then
                    Throw New Exception("No way.")
                End If

            ElseIf TypeOf dh Is StandardSecurityHandlerRev3 Then
                '' If we didn't know that we have a Rev4 handler, we would add code here,
            ElseIf TypeOf dh Is StandardSecurityHandlerRev2 Then
                '' ... and here,
            Else
                '' ... and done something in this case too.
            End If

            '' Save the new PDF - but PLEASE NOTE that because we did not set
            '' the Security.EncryptHandler, the newly saved document has no security!
            doc.Save(stream)
        End Using
        '' Clean up the temp file:
        File.Delete(fn)
        ''
        '' Done:
        Return doc.Pages.Count
    End Function
End Class