|
| 1 | +import shutil |
| 2 | +import json |
| 3 | +import logging |
| 4 | +import os |
| 5 | +from pathlib import Path |
| 6 | +from asposepdfcloud import ApiClient, PdfApi, HighlightAnnotation, Rectangle, Color, Point, AnnotationFlags, HorizontalAlignment, VerticalAlignment, AnnotationType |
| 7 | + |
| 8 | +# Configure logging |
| 9 | +logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") |
| 10 | + |
| 11 | + |
| 12 | +class Config: |
| 13 | + """Configuration parameters.""" |
| 14 | + CREDENTIALS_FILE = Path(r".\credentials.json") |
| 15 | + LOCAL_FOLDER = Path(r"C:\Samples") |
| 16 | + REMOTE_FOLDER = "Your_Temp_Pdf_Cloud" |
| 17 | + PDF_DOCUMENT_NAME = "sample.pdf" |
| 18 | + LOCAL_RESULT_DOCUMENT_NAME = "output_sample.pdf" |
| 19 | + PAGE_NUMBER = 1 |
| 20 | + |
| 21 | + ANNOTATION_ID = "GE5TAOZTHA2CYMRZGUWDIMBZFQZTEMA" |
| 22 | + |
| 23 | + NEW_HL_ANNOTATION_TEXT = "NEW HIGHLIGHT TEXT ANNOTATION" |
| 24 | + NEW_HL_ANNOTATION_DESCRIPTION = 'This is a sample highlight annotation' |
| 25 | + NEW_HL_ANNOTATION_SUBJECT = "Highlight Text Box Subject" |
| 26 | + NEW_HL_ANNOTATION_CONTENTS = "Highlight annotation sample contents" |
| 27 | + |
| 28 | + NEW_SO_ANNOTATION_TEXT = "NEW STRIKEOUT TEXT ANNOTATION" |
| 29 | + NEW_SO_ANNOTATION_DESCRIPTION = 'This is a sample strikeout annotation' |
| 30 | + NEW_SO_ANNOTATION_SUBJECT = "Strikeout Text Box Subject" |
| 31 | + NEW_SO_ANNOTATION_CONTENTS = "Strikeout annotation sample contents" |
| 32 | + |
| 33 | + NEW_UL_ANNOTATION_TEXT = "NEW UNDERLINE TEXT ANNOTATION" |
| 34 | + NEW_UL_ANNOTATION_DESCRIPTION = 'This is a sample underline annotation' |
| 35 | + NEW_UL_ANNOTATION_SUBJECT = "Underline Text Box Subject" |
| 36 | + NEW_UL_ANNOTATION_CONTENTS = "Underline annotation sample contents" |
| 37 | + |
| 38 | + NEW_FT_ANNOTATION_TEXT = "NEW FREE TEXT ANNOTATION" |
| 39 | + NEW_FT_ANNOTATION_DESCRIPTION = 'This is a sample annotation' |
| 40 | + NEW_FT_ANNOTATION_SUBJECT = "Free Text Box Subject" |
| 41 | + NEW_FT_ANNOTATION_CONTENTS = "Free Text annotation sample contents" |
| 42 | + |
| 43 | + REPLACED_CONTENT = 'This is a replaced sample annotation' |
| 44 | + |
| 45 | +class PdfAnnotationsHelper: |
| 46 | + def __init__(self, credentials_file: Path): |
| 47 | + self.pdf_api = None |
| 48 | + self._init_api(credentials_file) |
| 49 | + |
| 50 | + def _init_api(self, credentials_file: Path): |
| 51 | + """Initialize the API client.""" |
| 52 | + try: |
| 53 | + with credentials_file.open("r", encoding="utf-8") as file: |
| 54 | + credentials = json.load(file) |
| 55 | + api_key, app_id = credentials.get("key"), credentials.get("id") |
| 56 | + if not api_key or not app_id: |
| 57 | + raise ValueError("Error: Missing API keys in the credentials file.") |
| 58 | + self.pdf_api = PdfApi(ApiClient(api_key, app_id)) |
| 59 | + except (FileNotFoundError, json.JSONDecodeError, ValueError) as e: |
| 60 | + logging.error(f"Failed to load credentials: {e}") |
| 61 | + |
| 62 | + def uploadFile(self, fileName: str, localFolder: Path, remoteFolder: str): |
| 63 | + """ Upload a local fileName to the Aspose Cloud server. """ |
| 64 | + if self.pdf_api: |
| 65 | + file_path = localFolder / fileName |
| 66 | + try: |
| 67 | + self.pdf_api.upload_file(os.path.join(remoteFolder, fileName), file_path) |
| 68 | + logging.info(f"upload_file(): File '{fileName}' uploaded successfully.") |
| 69 | + except Exception as e: |
| 70 | + logging.error(f"upload_document(): Failed to upload file: {e}") |
| 71 | + |
| 72 | + |
| 73 | + def downloadFile(self, document: str, outputDocument: str, localFolder: Path, remoteFolder: Path, output_prefix: str): |
| 74 | + """Download the processed PDF document from the Aspose Cloud server.""" |
| 75 | + if self.pdf_api: |
| 76 | + try: |
| 77 | + temp_file = self.pdf_api.download_file(str(remoteFolder) + '/' + document) |
| 78 | + local_path = localFolder / ( output_prefix + outputDocument ) |
| 79 | + shutil.move(temp_file, str(local_path)) |
| 80 | + logging.info(f"download_result(): File successfully downloaded: {local_path}") |
| 81 | + except Exception as e: |
| 82 | + logging.error(f"download_result(): Failed to download file: {e}") |
| 83 | + |
| 84 | + def delete_popup_annotations(self, parent_annotation): |
| 85 | + """delete popup annotations for typed parent annotation in the page in the PDF document.""" |
| 86 | + if self.pdf_api: |
| 87 | + args = { |
| 88 | + "folder": Config.REMOTE_FOLDER |
| 89 | + } |
| 90 | + response = self.pdf_api.get_document_popup_annotations(Config.PDF_DOCUMENT_NAME, **args) |
| 91 | + if response.code == 200: |
| 92 | + for annotation in response.annotations.list: |
| 93 | + if annotation.parent.id == parent_annotation: |
| 94 | + self.pdf_api.delete_annotation(Config.PDF_DOCUMENT_NAME, annotation.id, **args) |
| 95 | + logging.info(f"delete_popup_annotations(): popup annotation id = '{annotation.id}' for '{annotation.contents}' deleted in the document '{Config.PDF_DOCUMENT_NAME}'.") |
| 96 | + else: |
| 97 | + logging.error(f"delete_popup_annotations(): Failed to delete popup annotation in the document. Response code: {response.code}") |
0 commit comments