For the purpose of protecting some important documents, the document opening permissions or editing and operation permissions are often set by encrypting the documents. The following example content will share the method of encrypting PDF documents by C # programming. This method introduces setting two kinds of encryption passwords for document opening password and permission password to protect documents. The document open password can only be used to open the document, and the permission password is a higher-level password, which can be used to open the document and to edit and modify the document’s permissions. According to the needs of document encryption, you can also choose one of the above two passwords to encrypt the document.

Tools:
Free Spire.PDF for .NET (free version)

Note: After downloading the library, unzip it. Add a reference assembly Spire.Pdf.dll file in the program (as shown below). At the same time, add corresponding using instructions when editing the code.

C # code example (for reference)

[Example 1 ] Encrypted PDF document

  1. using Spire . Pdf ;
  2. using Spire . Pdf . Security ;
  3. namespace Encrypt_PDF
  4. {
  5.     class Program
  6.     {
  7.         static void Main ( string [ ] args )
  8.         {
  9.             // Create an object of the PdfDocument class and load the test document
  10.             PdfDocument pdf = new PdfDocument ( ) ;
  11.             pdf . LoadFromFile ( “test.pdf” ) ;
  12.             // Set the password for opening the document and the permission password
  13.             pdf . Security . Encrypt ( “open” , “permission” , PdfPermissionsFlags . Print | PdfPermissionsFlags . CopyContent , PdfEncryptionKeySize . Key128Bit ) ;
  14.             // Save the encrypted document
  15.             pdf . SaveToFile ( “Encrypted.pdf” , FileFormat . PDF ) ;
  16.             System . Diagnostics . Process . Start ( “Encrypted.pdf” ) ;
  17.         }
  18.     }
  19. }

After running the program, you need to enter a password to open the generated document or enter a license password for operating the document.

Window prompt when opening an encrypted document:

After opening the document, you can further view the other permissions of the document:

[Example 2 ] Decrypting a PDF document

  1. using Spire . Pdf ;
  2. using Spire . Pdf . Security ;
  3. namespace DecryptPDF
  4. {
  5.     class Program
  6.     {
  7.         static void Main ( string [ ] args )
  8.         {
  9.             // Load an encrypted PDF document with a password
  10.             PdfDocument pdf = new PdfDocument ( ) ;
  11.             pdf . LoadFromFile ( “Encrypted.pdf” , “open” ) ;
  12.             // Set the open password and permission password to blank, set the document permissions, encryption level, and enter the original permission password
  13.             pdf . Security . Encrypt ( string . Empty , string . Empty , PdfPermissionsFlags . Default , PdfEncryptionKeySize . Key128Bit , “permission” ) ;
  14.             // Save the decrypted document
  15.             pdf . SaveToFile ( “Decrypted.pdf” ) ;
  16.             System . Diagnostics . Process . Start ( “Decrypted.pdf” ) ;
  17.         }
  18.     }
  19. }

Documents generated by running the program will no longer be password protected.