SecurityHandlers.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 the use of Security.EncryptHandler and Security.DecryptHandler.
  15. // Security.DecryptHandler allows you to examine the security attributes of an existing PDF.
  16. // Security.EncryptHandler allows you to specify security attributes when saving a PDF.
  17. // DsPdf supports Standard Security Handlers revisions 2, 3, 4, 5 and 6 as defined
  18. // in the PDF Spec 1.7 and 2.0. In this sample, we use StandardSecurityHandlerRev4.
  19. //
  20. // See SecurityHandlerRev6 for a Standard Security Handler revision 6 demo.
  21. public class SecurityHandlers
  22. {
  23. public int CreatePDF(Stream stream)
  24. {
  25. // Sample passwords:
  26. const string ownerPassword = "I'm the owner";
  27. const string userPassword = "I'm a user";
  28.  
  29. // Step 1: Generate a document with some security attributes:
  30. var doc0 = new GcPdfDocument();
  31. var rc0 = Common.Util.AddNote(
  32. "Demonstrating security:\n" +
  33. "In this PDF, we specify certain encryption options,\n" +
  34. "and set owner and user passwords.",
  35. doc0.NewPage());
  36.  
  37. // Create a Rev4 security handler:
  38. var ssh4 = new StandardSecurityHandlerRev4()
  39. {
  40. // Set some rev4 specific props:
  41. EncryptionAlgorithm = EncryptionAlgorithm.AES,
  42. EncryptStrings = true,
  43. };
  44. // StandardSecurityHandlerRev4 is derived from StandardSecurityHandlerRev3,
  45. // so we can do this to make sure we touch rev3-specific properties only
  46. // (the cast is for illustration only, you do not need it to set those props of course):
  47. if (ssh4 is StandardSecurityHandlerRev3 ssh3)
  48. {
  49. ssh3.EditingPermissions = EditingPermissions.AssembleDocument;
  50. ssh3.PrintingPermissions = PrintingPermissions.LowResolution;
  51. }
  52. // But StandardSecurityHandlerRev3 is NOT derived from StandardSecurityHandlerRev2,
  53. // because some properties have similar meanings but different syntax, so this:
  54. // if (ssh3 is StandardSecurityHandlerRev2 ssh2) { ... }
  55. // will NOT work.
  56.  
  57. // Set some passwords:
  58. ssh4.OwnerPassword = ownerPassword;
  59. ssh4.UserPassword = userPassword;
  60.  
  61. // Assign the handler we created to the document so that it is used when saving the PDF:
  62. doc0.Security.EncryptHandler = ssh4;
  63.  
  64. // Save the PDF in a temp file, so that we can load it:
  65. var fn = Path.GetTempFileName();
  66. doc0.Save(fn);
  67.  
  68. // Step 2: Load the generated PDf and examine its security attributes:
  69. var doc = new GcPdfDocument();
  70. using (var fs = File.OpenRead(fn))
  71. {
  72. // User password is needed to load the document:
  73. doc.Load(fs, userPassword);
  74.  
  75. // At this point we can examine doc.Security.DecryptHandler if it exists,
  76. // but there is NO Security.EncryptHandler:
  77. if (doc.Security.EncryptHandler != null)
  78. throw new Exception("This should not happen.");
  79.  
  80. var dh = doc.Security.DecryptHandler;
  81. if (dh is StandardSecurityHandlerRev4 dh_ssh4)
  82. {
  83. // Make sure the loaded permissions are what we specified in Step 1:
  84. Common.Util.AddNote(
  85. string.Format("Security attributes that were in the loaded PDF's DecryptHandler:\n" +
  86. "EditingPermissions: {0}\n" +
  87. "PrintingPermissions: {1}",
  88. dh_ssh4.EditingPermissions, dh_ssh4.PrintingPermissions),
  89. doc.Pages[0],
  90. new RectangleF(72, rc0.Bottom + 36, 72 * 6, 72 * 2));
  91. // This won't work, sorry:
  92. var noway = dh_ssh4.OwnerPassword;
  93. if (noway != null)
  94. throw new Exception("No way.");
  95. }
  96. else if (dh is StandardSecurityHandlerRev3 dh_ssh3)
  97. {
  98. // If we didn't know that we have a Rev4 handler, we would add code here,
  99. }
  100. else if (dh is StandardSecurityHandlerRev2 dh_ssh2)
  101. {
  102. // ... and here,
  103. }
  104. else
  105. {
  106. // ... and done something in this case too.
  107. }
  108.  
  109. // Save the new PDF - but PLEASE NOTE that because we did not set
  110. // the Security.EncryptHandler, the newly saved document has no security!
  111. doc.Save(stream);
  112. }
  113. // Clean up the temp file:
  114. File.Delete(fn);
  115. // Done:
  116. return doc.Pages.Count;
  117. }
  118. }
  119. }
  120.