Encrypt PDF Files - C# .NET 6

This tutorial shows how to encrypts PDF files in a C# .NET 6 application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to encrypt PDF files in a C# .NET 6 application
Completion Time 20 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET 6 Application
IDE Visual Studio 2022
Runtime Target .NET 6 or higher
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Encrypt PDF Files - C# .NET 6 tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If the project is not available, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added via NuGet packages.

This tutorial requires the following NuGet package:

For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Add the PDF Encryption Code

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open Program.cs. Add the following statements to the using block at the top of Program.cs.

C#
using System; 
using Leadtools; 
using Leadtools.Pdf; 

Add a new method named EncryptPdf(string inputFileName, string outputFileName) to the Program class and call it in the Main() method, as shown below. Also, add two string values to the Main() method, string inFileName = @"C:\LEADTOOLS22\Resources\Images\Leadtools.pdf"; and string outFileName = @"C:\LEADTOOLS22\Resources\Images\LeadtoolsEncrypted.pdf";.

C#
static void Main(string[] args) 
{ 
   if (!InitLEAD()) 
   { 
      Console.WriteLine("Error setting license"); 
      return; 
   } 
 
   string inFileName = @"C:\LEADTOOLS22\Resources\Images\Leadtools.pdf"; 
   string outFileName = @"C:\LEADTOOLS22\Resources\Images\LeadtoolsEncrypted.pdf"; 
   EncryptPdf(inFileName, outFileName); 
} 

Add the code below to the new method to set the PDFSecurityOptions, encrypt the PDF file, and output the new encrypted PDF to the file path set above.

C#
// Convert PDF to encrypted and disable printing 
static void EncryptPdf(string inputFileName, string outputFileName) 
{ 
   PDFFile inputFile = new PDFFile(inputFileName); 
   Console.WriteLine("Original PDF File Loaded"); 
   inputFile.SecurityOptions = new PDFSecurityOptions(); 
 
   // Set a password to open the file 
   inputFile.SecurityOptions.UserPassword = "PasswordToOpen"; 
 
   // Set a password that will be needed when changing the file permissions in the future 
   inputFile.SecurityOptions.OwnerPassword = "PermissionsPassword"; 
   // Disable printing 
   inputFile.SecurityOptions.PrintEnabled = false; 
   inputFile.SecurityOptions.HighQualityPrintEnabled = false; 
   inputFile.SecurityOptions.EncryptionMode = PDFEncryptionMode.RC128Bit; 
 
   Console.WriteLine("Security Options Set."); 
   inputFile.Convert(1, -1, outputFileName); 
   Console.WriteLine("Encrypted PDF Produced."); 
   Console.WriteLine("Checking if output PDF is encrypted:" + PDFFile.IsEncrypted(outputFileName)); 
} 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the console appears and converts the input PDF file to a new encrypted PDF file, using the specified PDFSecurityOptions.

Wrap-up

This tutorial showed how to save PDF files with encryption and how to control their security permissions to disallow printing. We also covered how to use the PDFFile and PDFSecurityOptions classes.

See Also

Help Version 22.0.2024.2.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.


Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.