From 4f19ad7940fca0693de49117237b22e9673e4fb0 Mon Sep 17 00:00:00 2001 From: Dmitriy-Xawstov Date: Tue, 19 Aug 2025 19:25:46 +0300 Subject: [PATCH 1/5] Added change password in pdf snippet --- Uses-Cases/EncryptDecrypt/PasswordChange.py | 82 +++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 Uses-Cases/EncryptDecrypt/PasswordChange.py diff --git a/Uses-Cases/EncryptDecrypt/PasswordChange.py b/Uses-Cases/EncryptDecrypt/PasswordChange.py new file mode 100644 index 0000000..7fcef09 --- /dev/null +++ b/Uses-Cases/EncryptDecrypt/PasswordChange.py @@ -0,0 +1,82 @@ +import shutil +import json +import logging +from pathlib import Path +import base64 +from asposepdfcloud import ApiClient, PdfApi, CryptoAlgorithm + +# Configure logging +logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") + +class Config: + """Configuration parameters.""" + CREDENTIALS_FILE = Path(r"C:\\Projects\\ASPOSE\\Pdf.Cloud\\Credentials\\credentials.json") + LOCAL_FOLDER = Path(r"C:\Samples") + PDF_DOCUMENT_NAME = "sample_encrypted.pdf" + LOCAL_RESULT_DOCUMENT_NAME = "output_sample.pdf" + DOCUMENT_PASSWORD = 'Owner-Password' + NEW_USER_PASSWORD = "NEW-User-Password" + NEW_OWNER_PASSWORD = "NEW-Owner-Password" + +class pdfPasswordModify: + """Class for replacing password in PDF encrypted document using Aspose PDF Cloud API.""" + def __init__(self, credentials_file: Path = Config.CREDENTIALS_FILE): + self.pdf_api = None + self._init_api(credentials_file) + + def _init_api(self, credentials_file: Path): + """Initialize the API client.""" + try: + with credentials_file.open("r", encoding="utf-8") as file: + credentials = json.load(file) + api_key, app_id = credentials.get("key"), credentials.get("id") + if not api_key or not app_id: + raise ValueError("init_api(): Error: Missing API keys in the credentials file.") + self.pdf_api = PdfApi(ApiClient(api_key, app_id)) + except (FileNotFoundError, json.JSONDecodeError, ValueError) as e: + logging.error(f"init_api(): Failed to load credentials: {e}") + + def upload_document(self): + """ Upload a PDF document to the Aspose Cloud server. """ + if self.pdf_api: + file_path = Config.LOCAL_FOLDER / Config.PDF_DOCUMENT_NAME + try: + self.pdf_api.upload_file(Config.PDF_DOCUMENT_NAME, str(file_path)) + logging.info(f"upload_file(): File '{Config.PDF_DOCUMENT_NAME}' uploaded successfully.") + except Exception as e: + logging.error(f"upload_document(): Failed to upload file: {e}") + + def download_result(self): + """ Download the processed PDF document from the Aspose Cloud server. """ + if self.pdf_api: + try: + temp_file = self.pdf_api.download_file(Config.PDF_DOCUMENT_NAME) + local_path = Config.LOCAL_FOLDER / ("password_change_" + Config.LOCAL_RESULT_DOCUMENT_NAME) + shutil.move(temp_file, str(local_path)) + logging.info(f"download_result(): File successfully downloaded: {local_path}") + except Exception as e: + logging.error(f"download_result(): Failed to download file: {e}") + + def change_passwords(self): + """Decrypt the PDF document.""" + if self.pdf_api: + try: + password_encoded = base64.b64encode(bytes(Config.DOCUMENT_PASSWORD, encoding='utf-8')) + + new_owner_pwd_encoded = base64.b64encode(bytes(Config.NEW_OWNER_PASSWORD, encoding='utf-8')) + new_user_pwd_encoded = base64.b64encode(bytes(Config.NEW_USER_PASSWORD, encoding='utf-8')) + + response = self.pdf_api.post_change_password_document_in_storage(Config.PDF_DOCUMENT_NAME, password_encoded, new_user_pwd_encoded, new_owner_pwd_encoded) + if response.code == 200: + logging.info(f"change_passwords(): Password in document #{Config.PDF_DOCUMENT_NAME} successfully modified.") + else: + logging.error(f"change_passwords(): Failed to chnage passowd in document #{Config.PDF_DOCUMENT_NAME}. Response code: {response.code}") + except Exception as e: + logging.error(f"change_passwords(): Error while change passwords in document: {e}") + + +if __name__ == "__main__": + pdf_pwd_upd = pdfPasswordModify() + pdf_pwd_upd.upload_document() + pdf_pwd_upd.change_passwords() + pdf_pwd_upd.download_result() \ No newline at end of file From d4df1a3ac14da0004608fecee3b531bb13db8297 Mon Sep 17 00:00:00 2001 From: Dmitriy-Xawstov Date: Tue, 19 Aug 2025 19:27:50 +0300 Subject: [PATCH 2/5] Update PasswordChange.py --- Uses-Cases/EncryptDecrypt/PasswordChange.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Uses-Cases/EncryptDecrypt/PasswordChange.py b/Uses-Cases/EncryptDecrypt/PasswordChange.py index 7fcef09..16125f5 100644 --- a/Uses-Cases/EncryptDecrypt/PasswordChange.py +++ b/Uses-Cases/EncryptDecrypt/PasswordChange.py @@ -10,7 +10,7 @@ class Config: """Configuration parameters.""" - CREDENTIALS_FILE = Path(r"C:\\Projects\\ASPOSE\\Pdf.Cloud\\Credentials\\credentials.json") + CREDENTIALS_FILE = Path(r"..\\credentials.json") LOCAL_FOLDER = Path(r"C:\Samples") PDF_DOCUMENT_NAME = "sample_encrypted.pdf" LOCAL_RESULT_DOCUMENT_NAME = "output_sample.pdf" @@ -58,7 +58,7 @@ def download_result(self): logging.error(f"download_result(): Failed to download file: {e}") def change_passwords(self): - """Decrypt the PDF document.""" + """Change passwords in the PDF document.""" if self.pdf_api: try: password_encoded = base64.b64encode(bytes(Config.DOCUMENT_PASSWORD, encoding='utf-8')) @@ -79,4 +79,4 @@ def change_passwords(self): pdf_pwd_upd = pdfPasswordModify() pdf_pwd_upd.upload_document() pdf_pwd_upd.change_passwords() - pdf_pwd_upd.download_result() \ No newline at end of file + pdf_pwd_upd.download_result() From 1c38c37d88c8b76ecd9c4c64b106e6455a3ee27d Mon Sep 17 00:00:00 2001 From: Dmitriy-Xawstov Date: Tue, 19 Aug 2025 23:46:34 +0300 Subject: [PATCH 3/5] Update PasswordChange.py --- Uses-Cases/EncryptDecrypt/PasswordChange.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Uses-Cases/EncryptDecrypt/PasswordChange.py b/Uses-Cases/EncryptDecrypt/PasswordChange.py index 16125f5..709d828 100644 --- a/Uses-Cases/EncryptDecrypt/PasswordChange.py +++ b/Uses-Cases/EncryptDecrypt/PasswordChange.py @@ -10,7 +10,7 @@ class Config: """Configuration parameters.""" - CREDENTIALS_FILE = Path(r"..\\credentials.json") + CREDENTIALS_FILE = Path(r"C:\\Projects\\ASPOSE\\Pdf.Cloud\\Credentials\\credentials.json") LOCAL_FOLDER = Path(r"C:\Samples") PDF_DOCUMENT_NAME = "sample_encrypted.pdf" LOCAL_RESULT_DOCUMENT_NAME = "output_sample.pdf" @@ -18,7 +18,7 @@ class Config: NEW_USER_PASSWORD = "NEW-User-Password" NEW_OWNER_PASSWORD = "NEW-Owner-Password" -class pdfPasswordModify: +class pdfEncoder: """Class for replacing password in PDF encrypted document using Aspose PDF Cloud API.""" def __init__(self, credentials_file: Path = Config.CREDENTIALS_FILE): self.pdf_api = None @@ -58,7 +58,7 @@ def download_result(self): logging.error(f"download_result(): Failed to download file: {e}") def change_passwords(self): - """Change passwords in the PDF document.""" + """Decrypt the PDF document.""" if self.pdf_api: try: password_encoded = base64.b64encode(bytes(Config.DOCUMENT_PASSWORD, encoding='utf-8')) @@ -76,7 +76,7 @@ def change_passwords(self): if __name__ == "__main__": - pdf_pwd_upd = pdfPasswordModify() - pdf_pwd_upd.upload_document() - pdf_pwd_upd.change_passwords() - pdf_pwd_upd.download_result() + pdf_encoder = pdfEncoder() + pdf_encoder.upload_document() + pdf_encoder.change_passwords() + pdf_encoder.download_result() From f6c36a447165f4be7e394d18a6fdbbe98244ccfd Mon Sep 17 00:00:00 2001 From: Dmitriy-Xawstov Date: Tue, 19 Aug 2025 23:48:31 +0300 Subject: [PATCH 4/5] Update PasswordChange.py --- Uses-Cases/EncryptDecrypt/PasswordChange.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Uses-Cases/EncryptDecrypt/PasswordChange.py b/Uses-Cases/EncryptDecrypt/PasswordChange.py index 709d828..017fe78 100644 --- a/Uses-Cases/EncryptDecrypt/PasswordChange.py +++ b/Uses-Cases/EncryptDecrypt/PasswordChange.py @@ -3,7 +3,7 @@ import logging from pathlib import Path import base64 -from asposepdfcloud import ApiClient, PdfApi, CryptoAlgorithm +from asposepdfcloud import ApiClient, PdfApi # Configure logging logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") @@ -80,3 +80,4 @@ def change_passwords(self): pdf_encoder.upload_document() pdf_encoder.change_passwords() pdf_encoder.download_result() + From 8d26b3a979f3e907b2d78ecfbeb8f17cc380b342 Mon Sep 17 00:00:00 2001 From: Dmitriy-Xawstov Date: Wed, 20 Aug 2025 06:30:46 +0300 Subject: [PATCH 5/5] Update PasswordChange.py --- Uses-Cases/EncryptDecrypt/PasswordChange.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Uses-Cases/EncryptDecrypt/PasswordChange.py b/Uses-Cases/EncryptDecrypt/PasswordChange.py index 017fe78..5dea893 100644 --- a/Uses-Cases/EncryptDecrypt/PasswordChange.py +++ b/Uses-Cases/EncryptDecrypt/PasswordChange.py @@ -10,7 +10,7 @@ class Config: """Configuration parameters.""" - CREDENTIALS_FILE = Path(r"C:\\Projects\\ASPOSE\\Pdf.Cloud\\Credentials\\credentials.json") + CREDENTIALS_FILE = Path(r"..\\credentials.json") LOCAL_FOLDER = Path(r"C:\Samples") PDF_DOCUMENT_NAME = "sample_encrypted.pdf" LOCAL_RESULT_DOCUMENT_NAME = "output_sample.pdf" @@ -81,3 +81,4 @@ def change_passwords(self): pdf_encoder.change_passwords() pdf_encoder.download_result() +