Encrypt a PDF document

<< Click to Display Table of Contents >>

Navigation:  Gnostice Document Studio .NET > Going Deeper > Document Engines > Working with PDF > Enhancement >

Encrypt a PDF document

In this section we present example code to encrypt a PDF document. Before proceeding further please make sure that the prerequisite steps as listed in the Document Engines parent topic are followed.

 

The example code below shows how to password-protect a PDF file so that the password is required to open the document.

 

using (var pdfDoc = new PDF())
{
   // Load an existing PDF
   pdfDoc.LoadDocument(@"statement.pdf");
 
   // Enable document encryption
   pdfDoc.Security.Enabled = true;
   pdfDoc.Security.Level = PDFEncryptionLevel.AES_256bit;
 
   // Set password to open the document
   pdfDoc.Security.UserPassword = "user";
 
   // Save the encrypted document to a file
   pdfDoc.Save(@"encrypted_statement.pdf");
}

 

The example code below shows how to password-protect a PDF file so that restrictions can be placed on the document.

 

using (var pdfDoc = new PDF())
{
   // Load an existing PDF
   pdfDoc.LoadDocument(@"statement.pdf");
 
   // Enable document encryption
   pdfDoc.Security.Enabled = true;
   pdfDoc.Security.Level = PDFEncryptionLevel.AES_256bit;
 
   // Set password to place restrictions on the document
   pdfDoc.Security.OwnerPassword = "owner";
 
   // Specify allowed document rights
   pdfDoc.Security.UserPermissions =
       PDFUserPermissions.Commenting |
       PDFUserPermissions.DocAssembly |
       PDFUserPermissions.LowResPrint;
 
   // Save the encrypted document to a file
   pdfDoc.Save(@"encrypted_statement.pdf");
}