SecurityHandlerRev6.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Text;
  10. using GrapeCity.Documents.Pdf.Security;
  11.  
  12. namespace DsPdfWeb.Demos
  13. {
  14. // This sample demonstrates how to create an encrypted PDF
  15. // using the Standard Security Handler Revision 6.
  16. //
  17. // Standard Security Handler Revision 6 was added in PDF 2.0.
  18. // It uses AES 256 encryption, same as the Standard Security Handler Revision 5
  19. // described in "Adobe Supplement to the ISO 32000 BaseVersion: 1.7 ExtensionLevel: 3",
  20. // but a new more complex algorithm is used to generate an encryption key.
  21. //
  22. // See SecurityHandlers for examples of using other security handlers.
  23. public class SecurityHandlerRev6
  24. {
  25. public int CreatePDF(Stream stream)
  26. {
  27. // Sample passwords:
  28. const string ownerPassword = "I'm the owner";
  29. const string userPassword = "I'm a user";
  30.  
  31. // Step 1: Generate a document with some security attributes:
  32. var doc0 = new GcPdfDocument();
  33. var rc0 = Common.Util.AddNote(
  34. "Demonstrating the use of the Standard Security Handler Revision 6.",
  35. doc0.NewPage());
  36.  
  37. // Create a Rev6 security handler and set security properties:
  38. var ssh6 = new StandardSecurityHandlerRev6()
  39. {
  40. OwnerPassword = ownerPassword,
  41. UserPassword = userPassword,
  42. CopyContent = false,
  43. PrintingPermissions = PrintingPermissions.Disabled,
  44. EncryptStrings = true,
  45. };
  46.  
  47. // Assign the handler we created to the document so that it is used when saving the PDF:
  48. doc0.Security.EncryptHandler = ssh6;
  49.  
  50. // Save the PDF in a temp file. It will be loaded and examined in the next step:
  51. var fn = Path.GetTempFileName();
  52. doc0.Save(fn);
  53.  
  54. // Step 2: Load the generated PDF and examine its security attributes:
  55. var doc = new GcPdfDocument();
  56. using (var fs = File.OpenRead(fn))
  57. {
  58. // User password is needed to load the document:
  59. doc.Load(fs, userPassword);
  60.  
  61. // At this point we can examine doc.Security.DecryptHandler if it exists,
  62. // but there is NO Security.EncryptHandler:
  63. if (doc.Security.EncryptHandler != null)
  64. throw new Exception("This should not happen.");
  65.  
  66. var dh = doc.Security.DecryptHandler;
  67. if (dh is StandardSecurityHandlerRev6 dh_ssh6)
  68. {
  69. // Print out some of the permissions in the loaded PDF that was generated in Step 1:
  70. var txt = $"Some of the security attributes found in the PDF " +
  71. $"that was generated using StandardSecurityHandlerRev6:\n" +
  72. $"- EncryptionAlgorithm: {dh_ssh6.EncryptionAlgorithm}\n" +
  73. $"- EncryptionKeyLength: {dh_ssh6.EncryptionKeyLength}\n" +
  74. $"- CopyContent: {dh_ssh6.CopyContent}\n" +
  75. $"- PrintingPermissions: {dh_ssh6.PrintingPermissions}\n" +
  76. $"- EncryptStrings: {dh_ssh6.EncryptStrings}\n" +
  77. $"";
  78.  
  79. Common.Util.AddNote(
  80. txt,
  81. doc.Pages[0],
  82. new RectangleF(72, rc0.Bottom + 36, 72 * 6, 72 * 2));
  83.  
  84. // This won't work, sorry:
  85. var noway = dh_ssh6.OwnerPassword;
  86. if (noway != null)
  87. throw new Exception("This should not happen.");
  88. }
  89.  
  90. // Save the new PDF. Please note that because we did not set the Security.EncryptHandler
  91. // on this PDF, this document (unlike the one that was generated in Step 1) has no security:
  92. doc.Save(stream);
  93. }
  94. // Clean up the temp file:
  95. File.Delete(fn);
  96. // Done:
  97. return doc.Pages.Count;
  98. }
  99. }
  100. }
  101.