Documents for PDF .NET Edition
Features / Security
In This Topic
    Security
    In This Topic

    PDF security can be maintained by controlling access to PDF documents by encrypting PDF and setting permission levels that will prevent unauthorized users from stealing information in your PDF document. For more information on PDF security, see PDF specification 1.7 (Section 7.6.3).

    The GcPdf library supports some of the standard security options in the PDF file format. The following section describes the different types of security features.

    Encrypt PDF

    PDF documents with sensitive or confidential information require encryption to restrict access to intruders. GcPdf provides Security class to encrypt a document and decline access to unauthorized users.

    To encrypt a PDF file using Standard Security Handler Revision 4:

    1. Create an object of StandardSecurityHandlerRev4 class.
    2. Set the required properties of the StandardSecurityHandlerRev4 object, such as passwords, encryption algorithm, etc.
    3. Pass the object to the EncryptHandler property of the Security class to encrypt the PDF document.
      C#
      Copy Code
      public void CreatePDF(Stream stream)
      {
          GcPdfDocument doc = new GcPdfDocument();
          var page = doc.NewPage();
          var g = page.Graphics;
          const float In = 150;
          //Add Encryption
          var std = new StandardSecurityHandlerRev4();
          std.OwnerPassword = "abc";
          std.UserPassword = "qwe";
          // Set EncyptionAlgorithm
          std.EncryptionAlgorithm = EncryptionAlgorithm.RC4;
          std.EncryptionKeyLength = 128;
          // Set the EncryptHandler property.
          doc.Security.EncryptHandler = std;
          // Render text using DrawString method
          g.DrawString("Welcome to GrapeCity, Inc", new TextFormat()
          { Font = StandardFonts.TimesBold, FontSize = 12 }, new PointF(In, In));
          // Save document
          doc.Save(stream);
      }
      
    Back to Top

    GcPdf also supports Standard Security Handler Revision 6 (defined in the PDF 2.0 specification) which uses AES encryption with 256 bit key length.

    To encrypt a PDF file using Standard Security Handler Revision 6:

    1. Create an object of StandardSecurityHandlerRev6 class.
    2. Set the required properties of the StandardSecurityHandlerRev6 object, such as password, printing permission, etc.
    3. Pass the object to the EncryptHandler property of the Security class to encrypt the PDF document.
      C#
      Copy Code
      public void CreatePDF(Stream stream)
      {
          GcPdfDocument doc = new GcPdfDocument();
          var page = doc.NewPage();
          var g = page.Graphics;
          const float In = 150;
          //Add Encryption
          var ssh = new StandardSecurityHandlerRev6();
          ssh.OwnerPassword = "password";
          ssh.PrintingPermissions = PrintingPermissions.Enabled;
          // Set the EncryptHandler property
          doc.Security.EncryptHandler = ssh;
          // Render text using DrawString method
          g.DrawString("Welcome to GrapeCity, Inc", new TextFormat()
          { Font = StandardFonts.TimesBold, FontSize = 12 }, new PointF(In, In));
          // Save document
          doc.Save(stream);
      }
      
    Back to Top

    Set Permissions

    Setting permissions restricts users from copying, printing and editing the contents in a PDF document. The Security class of the GcPdf library allows a user to set up permissions in a PDF document.

    To set permissions in a PDF document:

    1. Create an object of StandardSecurityHandlerRev3 class.
    2. Use the required properties of the StandardSecurityHandlerRev3 object to set the permissions such as editing, printing, etc.
    3. Pass the object to the EncryptHandler property of the Security class.
      C#
      Copy Code
      public void CreatePDF(Stream stream)
      {
          GcPdfDocument doc = new GcPdfDocument();
          var page = doc.NewPage();
          var g = page.Graphics;
          int In = 72;
      
          // Create a security handler variable
          var std = new StandardSecurityHandlerRev3();
          std.EditingPermissions = EditingPermissions.Enabled;
          std.OwnerPassword = "abc";
          std.UserPassword = "qwe";
      
          // Set permissions
          std.EditingPermissions = EditingPermissions.Enabled;
          std.CopyContentPermissions = CopyContentPermissions.Enabled;
          std.PrintingPermissions = PrintingPermissions.Disabled;
          doc.Security.EncryptHandler = std;
      
          // Render text using DrawString method
          g.DrawString("Welcome to GrapeCity, Inc.", new TextFormat()
          { Font = StandardFonts.TimesBold, FontSize = 12 }, new PointF(In, In));
      
          // Save document
          doc.Save(stream);
      }
      
    Back to Top

    For more information on applying security using GcPdf, see GcPdf sample browser.