This tutorial shows how to create a C# .NET Core application that encrypts PDF files using the LEADTOOLS SDK.
|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|
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.
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.
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:
Adding LEADTOOLS NuGet references and setting a license are covered in more detail in the Add References and Set a License tutorial.
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
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
string inFileName = @"C:\LEADTOOLS22\Resources\Images\Leadtools.pdf"; and
string outFileName = @"C:\LEADTOOLS22\Resources\Images\LeadtoolsEncrypted.pdf";.
static void Main(string args)
Console.WriteLine("Error setting license");
string inFileName = @"C:\LEADTOOLS22\Resources\Images\Leadtools.pdf";
string outFileName = @"C:\LEADTOOLS22\Resources\Images\LeadtoolsEncrypted.pdf";
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.
// 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 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
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