DocumentRestrictions.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 shows how to set restrictions on a PDF,
  15. // e.g. restricting the ability to print or copy content
  16. // from the document.
  17. // See also the DocumentPermissions sample, which shows
  18. // how to examine restrictions in a loaded PDF.
  19. public class DocumentRestrictions
  20. {
  21. public int CreatePDF(Stream stream)
  22. {
  23. // Create a new PDF document:
  24. var doc = new GcPdfDocument();
  25. var rc = Common.Util.AddNote("This document has the following restrictions:\n" +
  26. " - printing is not allowed;\n" +
  27. " - content copying is not allowed;\n" +
  28. " - document assembly is not allowed.", doc.NewPage());
  29.  
  30. // Create a Rev4 security handler and specify some restrictions:
  31. var ssh4 = new StandardSecurityHandlerRev4()
  32. {
  33. // EncryptionAlgorithm = EncryptionAlgorithm.AES,
  34. // EncryptStrings = true,
  35. PrintingPermissions = PrintingPermissions.Disabled,
  36. CopyContent = false,
  37. EditingPermissions = EditingPermissions.Disabled
  38. };
  39. // Assign the handler we created to the document so that it is used when saving the PDF:
  40. doc.Security.EncryptHandler = ssh4;
  41.  
  42. // Add a text prompting the user to check out the source code:
  43. var pg = doc.Pages.Last;
  44. var g = pg.Graphics;
  45. var tl = g.CreateTextLayout();
  46. tl.AppendLine("See the source code of this demo for details on how to use a security handler to set PDF document restrictions.");
  47. tl.MaxWidth = pg.Bounds.Width - rc.X * 2;
  48. tl.MarginLeft = rc.X;
  49. tl.MarginTop = rc.Bottom + 36;
  50. g.DrawTextLayout(tl, PointF.Empty);
  51.  
  52. // Done:
  53. doc.Save(stream);
  54. return doc.Pages.Count;
  55. }
  56. }
  57. }
  58.