@@ -36,37 +36,26 @@ def _init_api(self, credentials_file: Path):
36
36
except (FileNotFoundError , json .JSONDecodeError , ValueError ) as e :
37
37
logging .error (f"Failed to load credentials: { e } " )
38
38
39
- def _ensure_api_initialized (self ):
40
- """Check if the API is initialized before making API calls."""
41
- if not self .pdf_api :
42
- logging .error ("PDF API is not initialized. Operation aborted." )
43
- return False
44
- return True
45
-
46
39
def upload_document (self ):
47
40
"""Upload a PDF document to the Aspose Cloud server."""
48
- if not self ._ensure_api_initialized ():
49
- return
50
-
51
- file_path = Config .LOCAL_FOLDER / Config .PDF_DOCUMENT_NAME
52
- try :
53
- self .pdf_api .upload_file (Config .PDF_DOCUMENT_NAME , str (file_path ))
54
- logging .info (f"File { Config .PDF_DOCUMENT_NAME } uploaded successfully." )
55
- except Exception as e :
56
- logging .error (f"Failed to upload file: { e } " )
41
+ if self .pdf_api :
42
+ file_path = Config .LOCAL_FOLDER / Config .PDF_DOCUMENT_NAME
43
+ try :
44
+ self .pdf_api .upload_file (Config .PDF_DOCUMENT_NAME , str (file_path ))
45
+ logging .info (f"File { Config .PDF_DOCUMENT_NAME } uploaded successfully." )
46
+ except Exception as e :
47
+ logging .error (f"Failed to upload file: { e } " )
57
48
58
49
def download_result (self ):
59
50
"""Download the processed PDF document from the Aspose Cloud server."""
60
- if not self ._ensure_api_initialized ():
61
- return
62
-
63
- try :
64
- temp_file = self .pdf_api .download_file (Config .PDF_DOCUMENT_NAME )
65
- local_path = Config .LOCAL_FOLDER / Config .LOCAL_RESULT_DOCUMENT_NAME
66
- shutil .move (temp_file , str (local_path ))
67
- logging .info (f"download_result(): File successfully downloaded: { local_path } " )
68
- except Exception as e :
69
- logging .error (f"download_result(): Failed to download file: { e } " )
51
+ if self .pdf_api :
52
+ try :
53
+ temp_file = self .pdf_api .download_file (Config .PDF_DOCUMENT_NAME )
54
+ local_path = Config .LOCAL_FOLDER / Config .LOCAL_RESULT_DOCUMENT_NAME
55
+ shutil .move (temp_file , str (local_path ))
56
+ logging .info (f"download_result(): File successfully downloaded: { local_path } " )
57
+ except Exception as e :
58
+ logging .error (f"download_result(): Failed to download file: { e } " )
70
59
71
60
def show_links_array (self , links , prefix ):
72
61
"""Show all hyperlink annotations for a specific PDF document."""
@@ -75,52 +64,45 @@ def show_links_array(self, links, prefix):
75
64
76
65
def get_all_links (self ):
77
66
"""Get all hyperlink annotations for a specific PDF document."""
78
- if not self ._ensure_api_initialized ():
79
- return
80
-
81
- try :
82
- response = self .pdf_api .get_page_link_annotations ( Config .PDF_DOCUMENT_NAME , Config .PAGE_NUMBER )
83
- if response .code == 200 :
84
- self .show_links_array (response .links .list , "All: " )
85
- else :
86
- logging .error (f"Failed to add link to the page. Response code: { response .code } " )
87
- except Exception as e :
88
- logging .error (f"Error while adding link: No links found - { e } " )
67
+ if self .pdf_api :
68
+ try :
69
+ response = self .pdf_api .get_page_link_annotations ( Config .PDF_DOCUMENT_NAME , Config .PAGE_NUMBER )
70
+ if response .code == 200 :
71
+ self .show_links_array (response .links .list , "All: " )
72
+ else :
73
+ logging .error (f"Failed to add link to the page. Response code: { response .code } " )
74
+ except Exception as e :
75
+ logging .error (f"Error while adding link: No links found - { e } " )
89
76
90
77
def get_link_by_id (self ) -> LinkAnnotationResponse :
91
- if not self .pdf_api :
92
- return None
78
+ if self .pdf_api :
79
+ result_link : LinkAnnotationResponse = self . pdf_api . get_link_annotation ( Config . PDF_DOCUMENT_NAME , Config . LINK_FIND_ID )
93
80
94
- result_link : LinkAnnotationResponse = self .pdf_api .get_link_annotation (Config .PDF_DOCUMENT_NAME , Config .LINK_FIND_ID )
81
+ if result_link .code == 200 :
82
+ return result_link
95
83
96
- if result_link .code == 200 :
97
- return result_link
98
-
99
- print ("Link not found." )
100
- return None
84
+ print ("Link not found." )
85
+ return None
101
86
102
87
def replace_link (self ):
103
- if not self .pdf_api :
104
- return
88
+ if self .pdf_api :
89
+ link_annotation : LinkAnnotationResponse = self . get_link_by_id ()
105
90
106
- link_annotation : LinkAnnotationResponse = self .get_link_by_id ()
91
+ if not link_annotation :
92
+ return
107
93
108
- if not link_annotation :
109
- return
94
+ link_annotation .link .action = Config .NEW_LINK_ACTION
110
95
111
- link_annotation .link .action = Config .NEW_LINK_ACTION
112
-
113
- response : LinkAnnotationsResponse = self .pdf_api .put_link_annotation (
114
- Config .PDF_DOCUMENT_NAME ,
115
- Config .LINK_FIND_ID ,
116
- link_annotation .link ,
117
- )
118
-
119
- if response .code == 200 :
120
- print ("Link annotation replaced successfully." )
121
- else :
122
- print ("Failed to replace link annotation." )
96
+ response : LinkAnnotationsResponse = self .pdf_api .put_link_annotation (
97
+ Config .PDF_DOCUMENT_NAME ,
98
+ Config .LINK_FIND_ID ,
99
+ link_annotation .link ,
100
+ )
123
101
102
+ if response .code == 200 :
103
+ print ("Link annotation replaced successfully." )
104
+ else :
105
+ print ("Failed to replace link annotation." )
124
106
125
107
if __name__ == "__main__" :
126
108
pdf_links = PdfLinks ()
0 commit comments