@@ -37,66 +37,51 @@ def _init_api(self, credentials_file: Path):
37
37
except (FileNotFoundError , json .JSONDecodeError , ValueError ) as e :
38
38
logging .error (f"Failed to load credentials: { e } " )
39
39
40
- def _ensure_api_initialized (self ):
41
- """Check if the API is initialized before making API calls."""
42
- if not self .pdf_api :
43
- logging .error ("PDF API is not initialized. Operation aborted." )
44
- return False
45
- return True
46
-
47
40
def upload_document (self ):
48
41
"""Upload a PDF document to the Aspose Cloud server."""
49
- if not self ._ensure_api_initialized ():
50
- return
51
-
52
- file_path = Config .LOCAL_FOLDER / Config .PDF_DOCUMENT_NAME
53
- try :
54
- self .pdf_api .upload_file (Config .PDF_DOCUMENT_NAME , str (file_path ))
55
- logging .info (f"File { Config .PDF_DOCUMENT_NAME } uploaded successfully." )
56
- except Exception as e :
57
- logging .error (f"Failed to upload file: { e } " )
42
+ if self .pdf_api :
43
+ file_path = Config .LOCAL_FOLDER / Config .PDF_DOCUMENT_NAME
44
+ try :
45
+ self .pdf_api .upload_file (Config .PDF_DOCUMENT_NAME , str (file_path ))
46
+ logging .info (f"File { Config .PDF_DOCUMENT_NAME } uploaded successfully." )
47
+ except Exception as e :
48
+ logging .error (f"Failed to upload file: { e } " )
58
49
59
50
def download_result (self ):
60
51
"""Download the processed PDF document from the Aspose Cloud server."""
61
- if not self ._ensure_api_initialized ():
62
- return
63
-
64
- try :
65
- temp_file = self .pdf_api .download_file (Config .PDF_DOCUMENT_NAME )
66
- local_path = Config .LOCAL_FOLDER / Config .LOCAL_RESULT_DOCUMENT_NAME
67
- shutil .move (temp_file , str (local_path ))
68
- logging .info (f"download_result(): File successfully downloaded: { local_path } " )
69
- except Exception as e :
70
- logging .error (f"download_result(): Failed to download file: { e } " )
52
+ if self .pdf_api :
53
+ try :
54
+ temp_file = self .pdf_api .download_file (Config .PDF_DOCUMENT_NAME )
55
+ local_path = Config .LOCAL_FOLDER / Config .LOCAL_RESULT_DOCUMENT_NAME
56
+ shutil .move (temp_file , str (local_path ))
57
+ logging .info (f"download_result(): File successfully downloaded: { local_path } " )
58
+ except Exception as e :
59
+ logging .error (f"download_result(): Failed to download file: { e } " )
71
60
72
61
def show_links_array (self , links , prefix ):
73
62
for item in links :
74
63
logging .info (f"{ prefix } Link ID: '{ item .id } ' - Link Action: '{ item .action } '" )
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 remove_link_by_id (self ):
91
- if not self .pdf_api :
92
- return
78
+ if self .pdf_api :
79
+ response : AsposeResponse = self . pdf_api . delete_link_annotation ( Config . PDF_DOCUMENT_NAME , Config . LINK_FIND_ID )
93
80
94
- response : AsposeResponse = self .pdf_api .delete_link_annotation (Config .PDF_DOCUMENT_NAME , Config .LINK_FIND_ID )
95
-
96
- if response .code == 200 :
97
- logging .info ("Link annotation with ID " + Config .LINK_FIND_ID + " has been removed." )
98
- else :
99
- logging .erro ("Failed to remove link annotation with ID " + Config .LINK_FIND_ID )
81
+ if response .code == 200 :
82
+ logging .info ("Link annotation with ID " + Config .LINK_FIND_ID + " has been removed." )
83
+ else :
84
+ logging .erro ("Failed to remove link annotation with ID " + Config .LINK_FIND_ID )
100
85
101
86
if __name__ == "__main__" :
102
87
pdf_links = PdfLinks ()
0 commit comments