2
2
import json
3
3
import logging
4
4
from pathlib import Path
5
- from asposepdfcloud import ApiClient , PdfApi , Link , Color , Bookmark
5
+ from asposepdfcloud import ApiClient , PdfApi , Color , Bookmark
6
6
7
7
# Configure logging
8
8
logging .basicConfig (level = logging .INFO , format = "%(asctime)s - %(levelname)s - %(message)s" )
@@ -13,7 +13,7 @@ class Config:
13
13
LOCAL_FOLDER = Path (r"C:\Samples" )
14
14
PDF_DOCUMENT_NAME = "sample.pdf"
15
15
LOCAL_RESULT_DOCUMENT_NAME = "output_sample.pdf"
16
- NEW_BOOKMARK_TITLE = "• Productivity improvement"
16
+ NEW_BOOKMARK_TITLE = "• Increased performance.." #"• Productivity improvement"
17
17
PARENT_BOOKMARK_FOR_APPEND = "" #The parent bookmark path. Specify an empty string when adding a bookmark to the root.
18
18
NEW_BOOKMARK_PAGE_NUMBER = 3
19
19
BOOKMARK_PAGE_POSITION_X = 89
@@ -38,65 +38,52 @@ def _init_api(self, credentials_file: Path):
38
38
except (FileNotFoundError , json .JSONDecodeError , ValueError ) as e :
39
39
logging .error (f"init_api(): Failed to load credentials: { e } " )
40
40
41
- def _ensure_api_initialized (self ):
42
- """Check if the API is initialized before making API calls."""
43
- if not self .pdf_api :
44
- logging .error ("ensure_api_initialized(): PDF API is not initialized. Operation aborted." )
45
- return False
46
- return True
47
-
48
41
def upload_document (self ):
49
42
"""Upload a PDF document to the Aspose Cloud server."""
50
- if not self ._ensure_api_initialized ():
51
- return
52
-
53
- file_path = Config .LOCAL_FOLDER / Config .PDF_DOCUMENT_NAME
54
- try :
55
- self .pdf_api .upload_file (Config .PDF_DOCUMENT_NAME , str (file_path ))
56
- logging .info (f"upload_document(): File { Config .PDF_DOCUMENT_NAME } uploaded successfully." )
57
- except Exception as e :
58
- logging .error (f"upload_document(): Failed to upload file: { e } " )
43
+ if self .pdf_api :
44
+ file_path = Config .LOCAL_FOLDER / Config .PDF_DOCUMENT_NAME
45
+ try :
46
+ self .pdf_api .upload_file (Config .PDF_DOCUMENT_NAME , str (file_path ))
47
+ logging .info (f"upload_document(): File { Config .PDF_DOCUMENT_NAME } uploaded successfully." )
48
+ except Exception as e :
49
+ logging .error (f"upload_document(): Failed to upload file: { e } " )
59
50
60
51
def download_result (self ):
61
52
"""Download the processed PDF document from the Aspose Cloud server."""
62
- if not self ._ensure_api_initialized ():
63
- return
64
-
65
- try :
66
- temp_file = self .pdf_api .download_file (Config .PDF_DOCUMENT_NAME )
67
- local_path = Config .LOCAL_FOLDER / Config .LOCAL_RESULT_DOCUMENT_NAME
68
- shutil .move (temp_file , str (local_path ))
69
- logging .info (f"download_result(): File successfully downloaded: { local_path } " )
70
- except Exception as e :
71
- logging .error (f"download_result(): Failed to download file: { e } " )
53
+ if self .pdf_api :
54
+ try :
55
+ temp_file = self .pdf_api .download_file (Config .PDF_DOCUMENT_NAME )
56
+ local_path = Config .LOCAL_FOLDER / Config .LOCAL_RESULT_DOCUMENT_NAME
57
+ shutil .move (temp_file , str (local_path ))
58
+ logging .info (f"download_result(): File successfully downloaded: { local_path } " )
59
+ except Exception as e :
60
+ logging .error (f"download_result(): Failed to download file: { e } " )
72
61
73
62
def append_bookmark_link (self ):
74
63
"""Append a new bookmark link to a specific page in the PDF document."""
75
- if not self ._ensure_api_initialized ():
76
- return
77
-
78
- newBookmark = Bookmark (
79
- title = Config .NEW_BOOKMARK_TITLE ,
80
- italic = True ,
81
- bold = True ,
82
- color = Color (a = 255 ,r = 0 ,g = 255 ,b = 0 ),
83
- level = 1 ,
84
- page_display_left = Config .BOOKMARK_PAGE_POSITION_X ,
85
- page_display_top = Config .BOOKMARK_PAGE_POSITION_Y ,
86
- page_display_zoom = 2 ,
87
- page_number = Config .NEW_BOOKMARK_PAGE_NUMBER
88
- )
89
-
90
- try :
91
- response = self .pdf_api .post_bookmark (
92
- Config .PDF_DOCUMENT_NAME , Config .PARENT_BOOKMARK_FOR_APPEND , [newBookmark ]
64
+ if self .pdf_api :
65
+ newBookmark = Bookmark (
66
+ title = Config .NEW_BOOKMARK_TITLE ,
67
+ italic = True ,
68
+ bold = True ,
69
+ color = Color (a = 255 ,r = 0 ,g = 255 ,b = 0 ),
70
+ level = 1 ,
71
+ page_display_left = Config .BOOKMARK_PAGE_POSITION_X ,
72
+ page_display_top = Config .BOOKMARK_PAGE_POSITION_Y ,
73
+ page_display_zoom = 2 ,
74
+ page_number = Config .NEW_BOOKMARK_PAGE_NUMBER
93
75
)
94
- if response .code == 200 :
95
- logging .info (f"append_bookmark_link(): Bookmark '{ response .bookmarks .list [0 ].action } '->'{ Config .NEW_BOOKMARK_TITLE } ' added to page #{ Config .NEW_BOOKMARK_PAGE_NUMBER } ." )
96
- else :
97
- logging .error (f"append_bookmark_link(): Failed to add bookmark '{ Config .NEW_BOOKMARK_TITLE } ' to the page #{ Config .NEW_BOOKMARK_PAGE_NUMBER } . Response code: { response .code } " )
98
- except Exception as e :
99
- logging .error (f"append_bookmark_link(): Error while adding bookmark: { e } " )
76
+
77
+ try :
78
+ response = self .pdf_api .post_bookmark (
79
+ Config .PDF_DOCUMENT_NAME , Config .PARENT_BOOKMARK_FOR_APPEND , [newBookmark ]
80
+ )
81
+ if response .code == 200 :
82
+ logging .info (f"append_bookmark_link(): Bookmark '{ response .bookmarks .list [0 ].action } '->'{ Config .NEW_BOOKMARK_TITLE } ' added to page #{ Config .NEW_BOOKMARK_PAGE_NUMBER } ." )
83
+ else :
84
+ logging .error (f"append_bookmark_link(): Failed to add bookmark '{ Config .NEW_BOOKMARK_TITLE } ' to the page #{ Config .NEW_BOOKMARK_PAGE_NUMBER } . Response code: { response .code } " )
85
+ except Exception as e :
86
+ logging .error (f"append_bookmark_link(): Error while adding bookmark: { e } " )
100
87
101
88
102
89
if __name__ == "__main__" :
0 commit comments