This tutorial shows how to encrypt PDF files in a Python application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to encrypt PDF files in a Python application |
Completion Time | 20 minutes |
Visual Studio Project | Download tutorial project (1 KB) |
Platform | Python Console Application |
IDE | Visual Studio 2022 |
Runtime Target | Python 3.10 or higher |
Development License | Download LEADTOOLS |
Try it in another language |
|
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 - Python 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.
This tutorial requires the following DLLs:
Leadtools.dll
Leadtools.Pdf.dll
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:
With the project created, the references added, and the license set, coding can begin.
In the Solution Explorer, open Project-Name.py
and place the following references below the "Add references to LEADTOOLS" comment
# Add references to LEADTOOLS
from leadtools import LibraryLoader
LibraryLoader.add_reference("Leadtools")
from Leadtools import *
LibraryLoader.add_reference("Leadtools.Pdf")
from Leadtools.Pdf import *
Add a new method named encrypt_pdf(in_filename, out_filename)
to the Project-Name.py
file and call it in the main()
method, as shown below. Also, add two string
values to the main()
method, for the purpose of this tutorial we will use in_filename = r"C:\LEADTOOLS23\Resources\Images\Leadtools.pdf"
and out_filename = r"C:\LEADTOOLS23\Resources\Images\LeadtoolsEncrypted.pdf"
.
def main():
Support.set_license(os.path.join(DemosTools.get_root(), "C:/LEADTOOLS23/Support/Common/License"))
in_filename = r"C:\LEADTOOLS23\Resources\Images\Leadtools.pdf"
out_filename = r"C:\LEADTOOLS23\Resources\Images\LeadtoolsEncrypted.pdf"
encrypt_pdf(in_filename, out_filename)
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
def encrypt_pdf(in_filename, out_filename):
input_file = PDFFile(in_filename)
print("Original PDF File Loaded")
input_file.SecurityOptions = PDFSecurityOptions()
# Set a password to open the file
input_file.SecurityOptions.UserPassword = "PasswordToOpen"
# Set a password that will be needed when changing the file permissions in the future
input_file.SecurityOptions.OwnerPassword = "PermissionsPassword"
# Disable printing
input_file.SecurityOptions.PrintEnabled = False
input_file.SecurityOptions.HighQualityPrintEnabled = False
input_file.SecurityOptions.EncryptionMode = PDFEncryptionMode.RC128Bit
print("Security Options Set.")
input_file.Convert(1, -1, out_filename)
print("Encrypted PDF Produced.")
print("Checking if output PDF is encrypted:", PDFFile.IsEncrypted(out_filename))
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
.
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.