'''' This code is part of Document Solutions for PDF .NET demos.'' Copyright (c) MESCIUS inc. All rights reserved.''ImportsSystem.IOImportsSystem.DrawingImportsGrapeCity.Documents.PdfImportsGrapeCity.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.PublicClassSecurityHandlersFunctionCreatePDF(ByVal stream AsStream) AsInteger'' 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 = NewGcPdfDocument()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 = NewStandardSecurityHandlerRev4() 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):IfTypeOf ssh4 IsStandardSecurityHandlerRev3ThenDim ssh3 = DirectCast(ssh4, StandardSecurityHandlerRev3) ssh3.EditingPermissions = EditingPermissions.AssembleDocument ssh3.PrintingPermissions = PrintingPermissions.LowResolutionEndIf'' 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 = NewGcPdfDocument()Using fs AsNewFileStream(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.EncryptHandlerIsNotNothingThenThrowNew Exception("This should not happen.")EndIf Dim dh = doc.Security.DecryptHandlerIfTypeOf dh IsStandardSecurityHandlerRev4ThenDim 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),NewRectangleF(72, rc0.Bottom + 36, 72 * 6, 72 * 2))'' This won't work, sorry:Dim noway = dh_ssh4.OwnerPasswordIf noway IsNotNothingThenThrowNew Exception("No way.")EndIf ElseIfTypeOf dh IsStandardSecurityHandlerRev3Then'' If we didn't know that we have a Rev4 handler, we would add code here,ElseIfTypeOf dh IsStandardSecurityHandlerRev2Then'' ... and here,Else'' ... and done something in this case too.EndIf '' 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)EndUsing'' Clean up the temp file:File.Delete(fn)'''' Done:Return doc.Pages.CountEndFunctionEndClass