1+ import shutil
2+ import json
3+ import logging
4+ from pathlib import Path
5+ from asposepdfcloud import ApiClient , PdfApi , Signature , SignatureType , SignatureField , Rectangle
6+
7+ # Configure logging
8+ logging .basicConfig (level = logging .INFO , format = "%(asctime)s - %(levelname)s - %(message)s" )
9+
10+
11+ class Config :
12+ """Configuration parameters."""
13+ CREDENTIALS_FILE = Path (r"C:\\Projects\\ASPOSE\\Pdf.Cloud\\Credentials\\credentials.json" )
14+ LOCAL_FOLDER = Path (r"C:\Samples" )
15+ PDF_DOCUMENT_NAME = "sample.pdf"
16+ LOCAL_RESULT_DOCUMENT_NAME = "output_sample.pdf"
17+ LOCAL_SIGNATURE_PATH = Path (r"C:\Samples\Signatures\3" )
18+ SIGNATURE_PFX = "signature.pfx"
19+ SIGNATURE_FORM_FIELD = 'Signature_1'
20+ SIGNATURE_PASSWORD = 'Password'
21+ SIGNATURE_CONTACT = 'Contact'
22+ SIGNATURE_LOCATION = 'Location'
23+ SIGNATURE_AUTHORITY = 'Issuer'
24+ SIGNATURE_DATE = '04/19/2025 12:15:00.000 PM'
25+ SIGNATURE_RECT = Rectangle (100 , 100 , 500 , 500 )
26+
27+
28+ class PdfSignatures :
29+ """Class for managing PDF signatures using Aspose PDF Cloud API."""
30+ def __init__ (self , credentials_file : Path = Config .CREDENTIALS_FILE ):
31+ self .pdf_api = None
32+ self ._init_api (credentials_file )
33+
34+ def _init_api (self , credentials_file : Path ):
35+ """Initialize the API client."""
36+ try :
37+ with credentials_file .open ("r" , encoding = "utf-8" ) as file :
38+ credentials = json .load (file )
39+ api_key , app_id = credentials .get ("key" ), credentials .get ("id" )
40+ if not api_key or not app_id :
41+ raise ValueError ("init_api(): Error: Missing API keys in the credentials file." )
42+ self .pdf_api = PdfApi (ApiClient (api_key , app_id ))
43+ except (FileNotFoundError , json .JSONDecodeError , ValueError ) as e :
44+ logging .error (f"init_api(): Failed to load credentials: { e } " )
45+
46+ def upload_file (self , local_path : Path , fileName : str ):
47+ """ Upload a local fileName to the Aspose Cloud server. """
48+ if self .pdf_api :
49+ file_path = local_path / fileName
50+ try :
51+ self .pdf_api .upload_file (fileName , str (file_path ))
52+ logging .info (f"upload_file(): File '{ fileName } ' uploaded successfully." )
53+ except Exception as e :
54+ logging .error (f"upload_document(): Failed to upload file: { e } " )
55+
56+ def upload_document (self ):
57+ """ Upload a PDF document to the Aspose Cloud server. """
58+ self .upload_file (Config .LOCAL_FOLDER , Config .PDF_DOCUMENT_NAME )
59+
60+
61+ def download_result (self ):
62+ """Download the processed PDF document from the Aspose Cloud server."""
63+ if self .pdf_api :
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 } " )
71+
72+ def append_signature (self ):
73+ """Append a new signature to the PDF document."""
74+ if self .pdf_api :
75+
76+ signature = Signature (
77+ signature_path = Config .SIGNATURE_PFX ,
78+ signature_type = SignatureType .PKCS7 ,
79+ password = Config .SIGNATURE_PASSWORD ,
80+ contact = Config .SIGNATURE_CONTACT ,
81+ location = Config .SIGNATURE_LOCATION ,
82+ visible = True ,
83+ rectangle = Config .SIGNATURE_RECT ,
84+ form_field_name = Config .SIGNATURE_FORM_FIELD ,
85+ authority = Config .SIGNATURE_AUTHORITY ,
86+ date = Config .SIGNATURE_DATE ,
87+ show_properties = False )
88+
89+ field = SignatureField (page_index = 1 )
90+ field .signature = signature
91+ field .partial_name = 'sign1'
92+ field .rect = Config .SIGNATURE_RECT
93+
94+ try :
95+ response = self .pdf_api .post_signature_field (Config .PDF_DOCUMENT_NAME , field )
96+ if response .code == 200 :
97+ logging .info (f"append_signature(): Signature '{ Config .SIGNATURE_CONTACT } ' successfully added to the document." )
98+ else :
99+ logging .error (f"append_signature(): Failed to add signature to the document. Response code: { response .code } " )
100+ except Exception as e :
101+ logging .error (f"append_signature(): Error while adding signature: { e } " )
102+
103+
104+ if __name__ == "__main__" :
105+ pdf_sign = PdfSignatures ()
106+ pdf_sign .upload_document ()
107+ pdf_sign .upload_file (Config .LOCAL_SIGNATURE_PATH , Config .SIGNATURE_PFX )
108+ pdf_sign .append_signature ()
109+ pdf_sign .download_result ()
0 commit comments