GroupDocs.Merger for Python via .NET lets you protect a PDF document with an open password and simultaneously restrict specific operations — for example, prevent printing or modification — using PdfSecurityOptions combined with PdfSecurityPermissions.
Steps to add a password with permissions to a PDF
Instantiate the Merger class with the path to the source PDF.
Create a PdfSecurityOptions instance with the desired password string.
Set the permissions property to one or more PdfSecurityPermissions enum values (e.g. DENY_PRINTING).
Call merger.add_password() and pass the PdfSecurityOptions object.
Call merger.save() with the output file path.
fromgroupdocs.mergerimportMergerfromgroupdocs.merger.domain.optionsimportPdfSecurityOptions,PdfSecurityPermissionsdefadd_password_to_pdf_with_permissions():# Load the source PDF documentwithMerger("./input.pdf")asmerger:# Create PDF-specific security options with an open passwordsecurity_options=PdfSecurityOptions("p@ss")# Deny printing so the recipient cannot print the documentsecurity_options.permissions=PdfSecurityPermissions.DENY_PRINTING# Apply the password and permissionsmerger.add_password(security_options)# Save the protected PDFmerger.save("./output.pdf")if__name__=="__main__":add_password_to_pdf_with_permissions()
input.pdf is a sample file used in this example. Click here to download it.
PdfSecurityPermissions values can be combined using the bitwise OR operator if you need to deny multiple operations at once.
Explanation
Load Source PDF: The Merger class is instantiated with the PDF to protect.
Configure Security Options: PdfSecurityOptions("p@ss") creates an options object that both sets the open password and allows fine-grained permission control.
Set Permissions: Assigning PdfSecurityPermissions.DENY_PRINTING to security_options.permissions prevents the recipient from printing the document after opening it.
Apply and Save: merger.add_password() encrypts the document; merger.save() writes the secured PDF to disk.