Encrypt PDF Files - C# .NET Core

This tutorial shows how to create a C# .NET Core application that encrypts PDF files using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to encrypt PDF files in a C# .NET Core application
Completion Time 20 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET Core Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS

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 Core 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:

Note

Adding LEADTOOLS NuGet references and setting a license are covered in more detail in the Add References and Set a License tutorial.

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:\LEADTOOLS21\Resources\Images\Leadtools.pdf"; and string outFileName = @"C:\LEADTOOLS21\Resources\Images\LeadtoolsEncrypted.pdf";.

C#
static void Main(string[] args) 
{ 
   if (!SetLicense()) 
   { 
      Console.WriteLine("Error setting license"); 
      return; 
   } 
 
   string inFileName = @"C:\LEADTOOLS21\Resources\Images\Leadtools.pdf"; 
   string outFileName = @"C:\LEADTOOLS21\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 21.0.2023.3.1
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

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