SecurityHandlers.vb
  1. ''
  2. '' This code is part of Document Solutions for PDF demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports GrapeCity.Documents.Text
  8. Imports GrapeCity.Documents.Pdf
  9. Imports GrapeCity.Documents.Pdf.Security
  10.  
  11. '' This sample demonstrates the use of Security.EncryptHandler and Security.DecryptHandler.
  12. '' Security.DecryptHandler allows you to examine the security attributes of an existing PDF.
  13. '' Security.EncryptHandler allows you to specify security attributes when saving a PDF.
  14. '' DsPdf supports Standard Security Handlers revisions 2, 3 and 4 (as defined in the PDF Spec).
  15. '' In this sample, we use StandardSecurityHandlerRev4 which provides the most options.
  16. Public Class SecurityHandlers
  17. Function CreatePDF(ByVal stream As Stream) As Integer
  18. '' Sample passwords:
  19. Dim ownerPassword = "I'm the owner"
  20. Dim userPassword = "I'm a user"
  21.  
  22. '' Step 1: Generate a document with some security attributes:
  23. Dim doc0 = New GcPdfDocument()
  24. Dim rc0 = Util.AddNote(
  25. "Demonstrating security:" + vbLf +
  26. "In this PDF, we specify certain encryption options," + vbLf +
  27. "and set owner and user passwords.",
  28. doc0.NewPage())
  29.  
  30. '' Create a Rev4 security handler, set some rev4 specific props:
  31. Dim ssh4 = New StandardSecurityHandlerRev4() With {
  32. .EncryptionAlgorithm = EncryptionAlgorithm.AES,
  33. .EncryptStrings = True
  34. }
  35. '' StandardSecurityHandlerRev4 is derived from StandardSecurityHandlerRev3,
  36. '' so we can do this to make sure we touch rev3-specific properties only
  37. '' (the cast is for illustration only, you do not need it to set those props of course):
  38. If TypeOf ssh4 Is StandardSecurityHandlerRev3 Then
  39. Dim ssh3 = DirectCast(ssh4, StandardSecurityHandlerRev3)
  40. ssh3.EditingPermissions = EditingPermissions.AssembleDocument
  41. ssh3.PrintingPermissions = PrintingPermissions.LowResolution
  42. End If
  43. '' But StandardSecurityHandlerRev3 is NOT derived from StandardSecurityHandlerRev2,
  44. '' because some properties have similar meanings but different syntax, so this:
  45. '' if (ssh3 is StandardSecurityHandlerRev2 ssh2) { ... }
  46. '' will NOT work.
  47.  
  48. '' Set some passwords:
  49. ssh4.OwnerPassword = ownerPassword
  50. ssh4.UserPassword = userPassword
  51.  
  52. '' Assign the handler we created to the document so that it is used when saving the PDF:
  53. doc0.Security.EncryptHandler = ssh4
  54.  
  55. '' Save the PDF in a temp file, so that we can load it:
  56. Dim fn = Path.GetTempFileName()
  57. doc0.Save(fn)
  58.  
  59. '' Step 2: Load the generated PDf and examine its security attributes:
  60. Dim doc = New GcPdfDocument()
  61. Using fs As New FileStream(fn, FileMode.Open, FileAccess.Read)
  62. '' User password is needed to load the document:
  63. doc.Load(fs, userPassword)
  64.  
  65. '' At this point we can examine doc.Security.DecryptHandler if it exists,
  66. '' but there is NO Security.EncryptHandler:
  67. If doc.Security.EncryptHandler IsNot Nothing Then
  68. Throw New Exception("This should not happen.")
  69. End If
  70.  
  71. Dim dh = doc.Security.DecryptHandler
  72. If TypeOf dh Is StandardSecurityHandlerRev4 Then
  73. Dim dh_ssh4 = DirectCast(dh, StandardSecurityHandlerRev4)
  74. '' Make sure the loaded permissions are what we specified in Step 1:
  75. Util.AddNote(
  76. String.Format("Security attributes that were in the loaded PDF's DecryptHandler:" + vbLf +
  77. "EditingPermissions: {0}" + vbLf +
  78. "PrintingPermissions: {1}",
  79. dh_ssh4.EditingPermissions, dh_ssh4.PrintingPermissions),
  80. doc.Pages(0),
  81. New RectangleF(72, rc0.Bottom + 36, 72 * 6, 72 * 2))
  82. '' This won't work, sorry:
  83. Dim noway = dh_ssh4.OwnerPassword
  84. If noway IsNot Nothing Then
  85. Throw New Exception("No way.")
  86. End If
  87.  
  88. ElseIf TypeOf dh Is StandardSecurityHandlerRev3 Then
  89. '' If we didn't know that we have a Rev4 handler, we would add code here,
  90. ElseIf TypeOf dh Is StandardSecurityHandlerRev2 Then
  91. '' ... and here,
  92. Else
  93. '' ... and done something in this case too.
  94. End If
  95.  
  96. '' Save the new PDF - but PLEASE NOTE that because we did not set
  97. '' the Security.EncryptHandler, the newly saved document has no security!
  98. doc.Save(stream)
  99. End Using
  100. '' Clean up the temp file:
  101. File.Delete(fn)
  102. ''
  103. '' Done:
  104. Return doc.Pages.Count
  105. End Function
  106. End Class
  107.