|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + asposepdfcloud "github.com/aspose-pdf-cloud/aspose-pdf-cloud-go/v25" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + LOCAL_FOLDER = "C:\\Samples" |
| 14 | + REMOTE_FOLDER = "Your_Temp_Pdf_Cloud" |
| 15 | + PDF_DOCUMENT_NAME = "sample.pdf" |
| 16 | + PDF_OUTPUT = "output_sample.pdf" |
| 17 | + PAGE_NUMBER = 1 |
| 18 | + |
| 19 | + ANNOTATION_ID = "GE5TAOZTHA2CYMRZGUWDIMBZFQZTEMA" |
| 20 | + |
| 21 | + NEW_HL_ANNOTATION_TEXT = "NEW HIGHLIGHT TEXT ANNOTATION" |
| 22 | + NEW_HL_ANNOTATION_DESCRIPTION = "This is a sample highlight annotation" |
| 23 | + NEW_HL_ANNOTATION_SUBJECT = "Highlight Text Box Subject" |
| 24 | + NEW_HL_ANNOTATION_CONTENTS = "Highlight annotation sample contents" |
| 25 | + |
| 26 | + NEW_SO_ANNOTATION_TEXT = "NEW STRIKEOUT TEXT ANNOTATION" |
| 27 | + NEW_SO_ANNOTATION_DESCRIPTION = "This is a sample strikeout annotation" |
| 28 | + NEW_SO_ANNOTATION_SUBJECT = "Strikeout Text Box Subject" |
| 29 | + NEW_SO_ANNOTATION_CONTENTS = "Strikeout annotation sample contents" |
| 30 | + |
| 31 | + NEW_UL_ANNOTATION_TEXT = "NEW UNDERLINE TEXT ANNOTATION" |
| 32 | + NEW_UL_ANNOTATION_DESCRIPTION = "This is a sample underline annotation" |
| 33 | + NEW_UL_ANNOTATION_SUBJECT = "Underline Text Box Subject" |
| 34 | + NEW_UL_ANNOTATION_CONTENTS = "Underline annotation sample contents" |
| 35 | + |
| 36 | + NEW_FT_ANNOTATION_TEXT = "NEW FREE TEXT ANNOTATION" |
| 37 | + NEW_FT_ANNOTATION_DESCRIPTION = "This is a sample annotation" |
| 38 | + NEW_FT_ANNOTATION_SUBJECT = "Free Text Box Subject" |
| 39 | + NEW_FT_ANNOTATION_CONTENTS = "Free Text annotation sample contents" |
| 40 | + |
| 41 | + REPLACED_CONTENT = "This is a replaced sample annotation" |
| 42 | +) |
| 43 | + |
| 44 | +func InitPdfApi() *asposepdfcloud.PdfApiService { |
| 45 | + // Initialize Credentials and create Pdf.Cloud service object |
| 46 | + AppSID := "*********" // Your Application SID |
| 47 | + AppKey := "*********" // Your Application Key |
| 48 | + |
| 49 | + pdfApi := asposepdfcloud.NewPdfApiService(AppSID, AppKey, "") |
| 50 | + return pdfApi |
| 51 | +} |
| 52 | + |
| 53 | +func UploadFile(pdf_api *asposepdfcloud.PdfApiService, name string) { |
| 54 | + // Upload local file to the Pdf.Cloud folder |
| 55 | + args := map[string]interface{}{ |
| 56 | + "folder": REMOTE_FOLDER, |
| 57 | + } |
| 58 | + file, _ := os.Open(filepath.Join(LOCAL_FOLDER, name)) |
| 59 | + _, httpResponse, err := pdf_api.UploadFile(filepath.Join(REMOTE_FOLDER, name), file, args) |
| 60 | + if err != nil { |
| 61 | + fmt.Println(err.Error()) |
| 62 | + } else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 { |
| 63 | + fmt.Println("Unexpected error!") |
| 64 | + } else { |
| 65 | + fmt.Println("Result file'" + name + "' successfully uploaded!") |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func SaveByteArrayToFile(local_folder string, file_name string, data []byte) { |
| 70 | + // Save byte array data to the local folder |
| 71 | + fileName := path.Join(local_folder, file_name) |
| 72 | + f, err := os.Create(fileName) |
| 73 | + if err != nil { |
| 74 | + fmt.Println(err) |
| 75 | + } else { |
| 76 | + size, err := f.Write(data) |
| 77 | + if err != nil || size == 0 { |
| 78 | + fmt.Println("Failures in downloading result!") |
| 79 | + } else { |
| 80 | + fmt.Println("Result file'" + fileName + "' successfully downloaded!") |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func DownloadFile(pdf_api *asposepdfcloud.PdfApiService, name string, output_name string, prefix string) { |
| 86 | + // Download modified Pdf document to local folder from the Pdf.Cloud folder |
| 87 | + args := map[string]interface{}{ |
| 88 | + "folder": REMOTE_FOLDER, |
| 89 | + } |
| 90 | + result_data, _, _ := pdf_api.DownloadFile(path.Join(REMOTE_FOLDER, name), args) |
| 91 | + SaveByteArrayToFile(LOCAL_FOLDER, prefix+output_name, result_data) |
| 92 | +} |
| 93 | + |
| 94 | +func DeletePopupAnnotations(pdf_api *asposepdfcloud.PdfApiService, document_name string, parent_annotation string) { |
| 95 | + // Delete popup annotations for typed parent annotation in the page in the PDF document. |
| 96 | + args := map[string]interface{}{ |
| 97 | + "folder": REMOTE_FOLDER, |
| 98 | + } |
| 99 | + result, httpResponse, err := pdf_api.GetDocumentPopupAnnotations(document_name, args) |
| 100 | + if err != nil { |
| 101 | + fmt.Println(err.Error()) |
| 102 | + } else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 { |
| 103 | + fmt.Println("Unexpected error!") |
| 104 | + } else { |
| 105 | + for _, annotation := range result.Annotations.List { |
| 106 | + if annotation.Parent.Id == parent_annotation { |
| 107 | + _, response, err2 := pdf_api.DeleteAnnotation(document_name, annotation.Id, args) |
| 108 | + if err2 != nil { |
| 109 | + fmt.Println(err2) |
| 110 | + } else if response.StatusCode < 200 || response.StatusCode > 299 { |
| 111 | + fmt.Println("delete_popup_annotations(): Failed to delete popup annotation in the document.") |
| 112 | + } else { |
| 113 | + fmt.Println("delete_popup_annotations(): popup annotation id = '" + annotation.Id + "' for '" + annotation.Contents + "' deleted in the document '" + PDF_DOCUMENT_NAME + "'.") |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments