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

Public Class SecurityHandlerRev6
    Public Function CreatePDF(ByVal stream As Stream) As Integer
        ' Sample passwords:
        Const ownerPassword As String = "I'm the owner"
        Const userPassword As String = "I'm a user"

        ' Step 1: Generate a document with some security attributes:
        Dim doc0 = New GcPdfDocument()
        Dim rc0 = Util.AddNote(
            "Demonstrating the use of the Standard Security Handler Revision 6.",
            doc0.NewPage())

        ' Create a Rev6 security handler and set security properties:
        Dim ssh6 = New StandardSecurityHandlerRev6() With {
            .OwnerPassword = ownerPassword,
            .UserPassword = userPassword,
            .CopyContent = False,
            .PrintingPermissions = PrintingPermissions.Disabled,
            .EncryptStrings = True
        }

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

        ' Save the PDF in a temp file. It will be loaded and examined in the next step:
        Dim fn = Path.GetTempFileName()
        doc0.Save(fn)

        ' Step 2: Load the generated PDF and examine its security attributes:
        Dim doc = New GcPdfDocument()
        Using fs = File.OpenRead(fn)
            ' 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 StandardSecurityHandlerRev6 Then
                Dim dh_ssh6 = DirectCast(dh, StandardSecurityHandlerRev6)
                ' Print out some of the permissions in the loaded PDF that was generated in Step 1:
                Dim txt = $"Some of the security attributes found in the PDF " &
                    $"that was generated using StandardSecurityHandlerRev6:{vbLf}" &
                    $"- EncryptionAlgorithm: {dh_ssh6.EncryptionAlgorithm}{vbLf}" &
                    $"- EncryptionKeyLength: {dh_ssh6.EncryptionKeyLength}{vbLf}" &
                    $"- CopyContent: {dh_ssh6.CopyContent}{vbLf}" &
                    $"- PrintingPermissions: {dh_ssh6.PrintingPermissions}{vbLf}" &
                    $"- EncryptStrings: {dh_ssh6.EncryptStrings}{vbLf}"

                Util.AddNote(
                    txt,
                    doc.Pages(0),
                    New RectangleF(72, rc0.Bottom + 36, 72 * 6, 72 * 2))

                ' This won't work, sorry:
                Dim noway = dh_ssh6.OwnerPassword
                If noway IsNot Nothing Then
                    Throw New Exception("This should not happen.")
                End If
            End If

            ' Save the new PDF. Please note that because we did not set the Security.EncryptHandler
            ' on this PDF, this document (unlike the one that was generated in Step 1) 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