Зашифровать PDF-файл
Contents
[
Hide
]
Зашифровать PDF с паролем пользователя и паролем владельца
Защита PDF‑документов важна при обмене конфиденциальным или ограниченным содержимым. Шифрование позволяет защитить документ паролями и определить, какие действия разрешено выполнять пользователям. Этот фрагмент кода показывает, как применить пароли пользователя и владельца вместе с правами доступа для защиты PDF‑файла.
- Создайте объект PdfFileSecurity.
- Привяжите входной PDF.
- Определите привилегии документа.
- Зашифруйте PDF.
- Сохраните зашифрованный PDF.
from io import FileIO
import sys
from os import path
import aspose.pdf as ap
import aspose.pdf.facades as pdf_facades
sys.path.append(path.join(path.dirname(__file__), ".."))
from config import set_license, initialize_data_dir
# Encrypt PDF with User and Owner Password
def encrypt_pdf_with_user_owner_password(infile, outfile):
"""Encrypt a PDF document using user and owner passwords."""
# Create PdfFileSecurity object
file_security = pdf_facades.PdfFileSecurity()
# Bind PDF document
file_security.bind_pdf(infile)
# Define document privileges
privilege = pdf_facades.DocumentPrivilege.forbid_all
privilege.allow_print = True
# Encrypt the PDF
file_security.encrypt_file(
"user_password", "owner_password", privilege, pdf_facades.KeySize.X128
)
# Save encrypted PDF
file_security.save(outfile)
Зашифровать PDF с разрешениями
Следующий фрагмент кода объясняет, как разрешить выбранные действия, такие как печать и копирование, ограничивая остальные.
- Инициализировать PdfFileSecurity класс.
- Привязать входной PDF.
- Настройте привилегии документа.
- Вызовите метод ’encrypt_file()'.
- Сохранить зашифрованный PDF.
from io import FileIO
import sys
from os import path
import aspose.pdf as ap
import aspose.pdf.facades as pdf_facades
sys.path.append(path.join(path.dirname(__file__), ".."))
from config import set_license, initialize_data_dir
# Encrypt PDF with Permissions
def encrypt_pdf_with_permissions(infile, outfile):
"""Encrypt a PDF document and configure specific permissions."""
# Create PdfFileSecurity object
file_security = pdf_facades.PdfFileSecurity()
# Bind PDF document
file_security.bind_pdf(infile)
# Configure privileges
privilege = pdf_facades.DocumentPrivilege.forbid_all
privilege.allow_print = True
privilege.allow_copy = True
# Encrypt the PDF
file_security.encrypt_file(
"user_password", "owner_password", privilege, pdf_facades.KeySize.X128
)
# Save encrypted PDF
file_security.save(outfile)
Зашифровать PDF с помощью алгоритма шифрования
Шифрование PDF не только защищает документы паролями, но и позволяет выбирать алгоритм шифрования и его стойкость. Выбор подходящего алгоритма обеспечивает более высокий уровень безопасности конфиденциальных документов.
- Создать объект PdfFileSecurity.
- Привязать входной PDF.
- Определить привилегии документа.
- Зашифровать PDF с помощью алгоритма.
- Сохранить зашифрованный PDF.
from io import FileIO
import sys
from os import path
import aspose.pdf as ap
import aspose.pdf.facades as pdf_facades
sys.path.append(path.join(path.dirname(__file__), ".."))
from config import set_license, initialize_data_dir
# Encrypt PDF with Encryption Algorithm
def encrypt_pdf_with_encryption_algorithm(infile, outfile):
"""Encrypt a PDF document using a specific encryption algorithm."""
# Create PdfFileSecurity object
file_security = pdf_facades.PdfFileSecurity()
# Bind PDF document
file_security.bind_pdf(infile)
# Define privileges
privilege = pdf_facades.DocumentPrivilege.forbid_all
privilege.allow_print = True
# Encrypt the PDF using AES algorithm
file_security.encrypt_file(
"user_password",
"owner_password",
privilege,
pdf_facades.KeySize.X256,
pdf_facades.Algorithm.AES,
)
# Save encrypted PDF
file_security.save(outfile)