Skip to content

Commit 9812b08

Browse files
Update pageAppendTextStamp.py
1 parent 8893c21 commit 9812b08

File tree

1 file changed

+29
-41
lines changed

1 file changed

+29
-41
lines changed

Uses-Cases/Pages/stamp/pageAppendTextStamp.py

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -34,57 +34,45 @@ def _init_api(self, credentials_file: Path):
3434
except (FileNotFoundError, json.JSONDecodeError, ValueError) as e:
3535
logging.error(f"init_api(): Failed to load credentials: {e}")
3636

37-
def _ensure_api_initialized(self):
38-
""" Check if the API is initialized before making API calls. """
39-
if not self.pdf_api:
40-
logging.error("ensure_api_initialized(): PDF API is not initialized. Operation aborted.")
41-
return False
42-
return True
43-
4437
def upload_document(self):
4538
""" Upload a PDF document to the Aspose Cloud server. """
46-
if not self._ensure_api_initialized():
47-
return
48-
49-
file_path = Config.LOCAL_FOLDER / Config.PDF_DOCUMENT_NAME
50-
try:
51-
self.pdf_api.upload_file(Config.PDF_DOCUMENT_NAME, str(file_path))
52-
logging.info(f"upload_document(): File {Config.PDF_DOCUMENT_NAME} uploaded successfully.")
53-
except Exception as e:
54-
logging.error(f"upload_document(): Failed to upload file: {e}")
39+
if self.pdf_api:
40+
file_path = Config.LOCAL_FOLDER / Config.PDF_DOCUMENT_NAME
41+
try:
42+
self.pdf_api.upload_file(Config.PDF_DOCUMENT_NAME, str(file_path))
43+
logging.info(f"upload_document(): File {Config.PDF_DOCUMENT_NAME} uploaded successfully.")
44+
except Exception as e:
45+
logging.error(f"upload_document(): Failed to upload file: {e}")
5546

5647
def download_result(self):
5748
""" Download the processed PDF document from the Aspose Cloud server. """
58-
if not self._ensure_api_initialized():
59-
return
60-
61-
try:
62-
temp_file = self.pdf_api.download_file(Config.PDF_DOCUMENT_NAME)
63-
local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME
64-
shutil.move(temp_file, str(local_path))
65-
logging.info(f"download_result(): File successfully downloaded: {local_path}")
66-
except Exception as e:
67-
logging.error(f"download_result(): Failed to download file: {e}")
49+
if self.pdf_api:
50+
try:
51+
temp_file = self.pdf_api.download_file(Config.PDF_DOCUMENT_NAME)
52+
local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME
53+
shutil.move(temp_file, str(local_path))
54+
logging.info(f"download_result(): File successfully downloaded: {local_path}")
55+
except Exception as e:
56+
logging.error(f"download_result(): Failed to download file: {e}")
6857

6958
def add_page_text_stamp(self):
7059
""" Adds a text stamp to a specific page in a PDF document. """
60+
if self.pdf_api:
61+
page_stamp: Stamp = Stamp(
62+
type = StampType.TEXT,
63+
background = True,
64+
horizontal_alignment = HorizontalAlignment.CENTER,
65+
text_alignment = HorizontalAlignment.CENTER,
66+
value = Config.STAMP_TEXT,
67+
page_index = Config.PAGE_NUMBER,
68+
)
7169

72-
page_stamp: Stamp = Stamp(
73-
type = StampType.TEXT,
74-
background = True,
75-
horizontal_alignment = HorizontalAlignment.CENTER,
76-
text_alignment = HorizontalAlignment.CENTER,
77-
value = Config.STAMP_TEXT,
78-
page_index = Config.PAGE_NUMBER,
79-
)
80-
81-
response: AsposeResponse = self.pdf_api.put_page_add_stamp(Config.PDF_DOCUMENT_NAME, Config.PAGE_NUMBER, page_stamp)
70+
response: AsposeResponse = self.pdf_api.put_page_add_stamp(Config.PDF_DOCUMENT_NAME, Config.PAGE_NUMBER, page_stamp)
8271

83-
if response.code == 200:
84-
logging.info(f"Text stamp '{Config.STAMP_TEXT}' added to page #{Config.PAGE_NUMBER}.")
85-
else:
86-
logging.error(f"Failed to add text stamp '{Config.STAMP_TEXT}' to page #{Config.PAGE_NUMBER}.")
87-
return
72+
if response.code == 200:
73+
logging.info(f"Text stamp '{Config.STAMP_TEXT}' added to page #{Config.PAGE_NUMBER}.")
74+
else:
75+
logging.error(f"Failed to add text stamp '{Config.STAMP_TEXT}' to page #{Config.PAGE_NUMBER}.")
8876

8977
if __name__ == "__main__":
9078
pdf_pages = PdfPages()

0 commit comments

Comments
 (0)