|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path" |
| 7 | + "path/filepath" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + asposepdfcloud "github.com/aspose-pdf-cloud/aspose-pdf-cloud-go/v25" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + REMOTE_FOLDER = "Your_Temp_Pdf_Cloud" |
| 15 | + LOCAL_FOLDER = "c:\\Samples" |
| 16 | + PDF_DOCUMENT = "sample.pdf" |
| 17 | + PDF_OUTPUT = "output_sample.pdf" |
| 18 | + |
| 19 | + ROTATE_PAGES_ANGLE = asposepdfcloud.Rotationon90 |
| 20 | + ROTATE_PAGES = "1-3" |
| 21 | + |
| 22 | + CROP_PAGE_TEMP_FILE = "sammple_temp_file.png" |
| 23 | + CROP_LOCAL_RESULT_DOCUMENT_NAME = "output_sample.pdf" |
| 24 | + CROP_PAGE_NUMBER = 3 |
| 25 | + CROP_HEIGHT = 400 |
| 26 | + CROP_WIDTH = 300 |
| 27 | + CROP_LLX = 100 |
| 28 | + CROP_LLY = 200 |
| 29 | + |
| 30 | + RESIZE_PDF_HTML_FILE = "sammple_temp_file.html" |
| 31 | + RESIZE_RESULT_DOCUMENT_NAME = "output_sample.pdf" |
| 32 | + RESIZE_PAGE_NUMBER = 2 |
| 33 | + RESIZE_NEW_PAGE_WIDTH = 1000 |
| 34 | + RESIZE_NEW_PAGE_HEIGHT = 500 |
| 35 | +) |
| 36 | + |
| 37 | +var ( |
| 38 | + CROP_PAGE_WIDTH = float64(0) |
| 39 | + CROP_PAGE_HEIGHT = float64(0) |
| 40 | +) |
| 41 | + |
| 42 | +func initPdfApi() *asposepdfcloud.PdfApiService { |
| 43 | + // Initialize Credentials and create Pdf.Cloud service object |
| 44 | + AppSID := "******" // Your Application SID |
| 45 | + AppKey := "******" // Your Application Key |
| 46 | + |
| 47 | + pdfApi := asposepdfcloud.NewPdfApiService(AppSID, AppKey, "") |
| 48 | + return pdfApi |
| 49 | +} |
| 50 | + |
| 51 | +func uploadFile(pdf_api *asposepdfcloud.PdfApiService, name string) { |
| 52 | + args := map[string]interface{}{ |
| 53 | + "folder": REMOTE_FOLDER, |
| 54 | + } |
| 55 | + file, _ := os.Open(filepath.Join(LOCAL_FOLDER, name)) |
| 56 | + _, _, _ = pdf_api.UploadFile(filepath.Join(REMOTE_FOLDER, name), file, args) |
| 57 | + fmt.Println("File '" + name + "' successfully uploaded!") |
| 58 | +} |
| 59 | +func downloadFile(pdf_api *asposepdfcloud.PdfApiService, name string, output_prefix string) { |
| 60 | + args := map[string]interface{}{ |
| 61 | + "folder": REMOTE_FOLDER, |
| 62 | + } |
| 63 | + result_data, _, _ := pdf_api.DownloadFile(path.Join(REMOTE_FOLDER, name), args) |
| 64 | + fileName := path.Join(LOCAL_FOLDER, output_prefix+PDF_OUTPUT) |
| 65 | + f, _ := os.Create(fileName) |
| 66 | + _, _ = f.Write(result_data) |
| 67 | + fmt.Println("Result file'" + fileName + "' successfully downloaded!") |
| 68 | +} |
| 69 | +func getPageInfo(pdf_api *asposepdfcloud.PdfApiService, document_name string, pageNumber int, tempFolder string) { |
| 70 | + args := map[string]interface{}{ |
| 71 | + "folder": tempFolder, |
| 72 | + } |
| 73 | + page, response, err := pdf_api.GetPage(document_name, int32(pageNumber), args) |
| 74 | + |
| 75 | + if response.StatusCode == 200 { |
| 76 | + showPages([]*asposepdfcloud.Page{page.Page}, "page") |
| 77 | + CROP_PAGE_HEIGHT = page.Page.Rectangle.URY - page.Page.Rectangle.LLY |
| 78 | + CROP_PAGE_WIDTH = page.Page.Rectangle.URX - page.Page.Rectangle.LLX |
| 79 | + } else { |
| 80 | + fmt.Println("Unexpected error : can't get pages!!! Error: " + err.Error()) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func showPages(pages []*asposepdfcloud.Page, prefix string) { |
| 85 | + if len(pages) > 0 { |
| 86 | + for _, page := range pages { |
| 87 | + fmt.Println(prefix + " => id: '" + strconv.FormatInt(int64(page.Id), 10) + "', lLx: '" + strconv.FormatFloat(page.Rectangle.LLX, 'f', -1, 64) + "', lLY: '" + strconv.FormatFloat(page.Rectangle.LLY, 'f', -1, 64) + "', uRX: '" + strconv.FormatFloat(page.Rectangle.URX, 'f', -1, 64) + "', uRY: '" + strconv.FormatFloat(page.Rectangle.URY, 'f', -1, 64) + "'") |
| 88 | + } |
| 89 | + } else { |
| 90 | + fmt.Println("showPages() error: array of pages is empty!") |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func extractPdfPage(pdf_api *asposepdfcloud.PdfApiService, document_name string, pageNumber int, width int, height int, localFolder string, tempFolder string) string { |
| 95 | + args := map[string]interface{}{ |
| 96 | + "folder": tempFolder, |
| 97 | + "width": int32(width), |
| 98 | + "height": int32(height), |
| 99 | + } |
| 100 | + |
| 101 | + image, response, err := pdf_api.GetPageConvertToPng(document_name, int32(pageNumber), args) |
| 102 | + if err != nil { |
| 103 | + fmt.Println(err.Error()) |
| 104 | + return "" |
| 105 | + } else if response.StatusCode != 200 { |
| 106 | + fmt.Println("extractPdfPage(): Faild to convert page to image!") |
| 107 | + return "" |
| 108 | + } |
| 109 | + imageFile := document_name + ".png" |
| 110 | + |
| 111 | + filePath := filepath.Join(localFolder, imageFile) |
| 112 | + os.WriteFile(filePath, image, 0777) |
| 113 | + |
| 114 | + uploadFile(pdf_api, imageFile) |
| 115 | + |
| 116 | + fmt.Println("Page #" + strconv.FormatInt(int64(pageNumber), 10) + " extracted as image.") |
| 117 | + return imageFile |
| 118 | +} |
| 119 | + |
| 120 | +func createPdfDocument(pdf_api *asposepdfcloud.PdfApiService, document_name string, width int, height int, tempFolder string) *asposepdfcloud.DocumentResponse { |
| 121 | + args := map[string]interface{}{ |
| 122 | + "folder": tempFolder, |
| 123 | + } |
| 124 | + |
| 125 | + config := asposepdfcloud.DocumentConfig{ |
| 126 | + PagesCount: 1, |
| 127 | + DocumentProperties: &asposepdfcloud.DocumentProperties{ |
| 128 | + List: []asposepdfcloud.DocumentProperty{ |
| 129 | + { |
| 130 | + BuiltIn: false, |
| 131 | + Name: "prop1", |
| 132 | + Value: "Val1", |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + DisplayProperties: &asposepdfcloud.DisplayProperties{ |
| 137 | + CenterWindow: true, |
| 138 | + HideMenuBar: true, |
| 139 | + }, |
| 140 | + DefaultPageConfig: &asposepdfcloud.DefaultPageConfig{ |
| 141 | + Height: float64(height), |
| 142 | + Width: float64(width), |
| 143 | + }, |
| 144 | + } |
| 145 | + |
| 146 | + result, httpResponse, err := pdf_api.PostCreateDocument(document_name, config, args) |
| 147 | + if err != nil { |
| 148 | + fmt.Println(err.Error()) |
| 149 | + return nil |
| 150 | + } else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 { |
| 151 | + fmt.Println("Unexpected error on create new document!") |
| 152 | + return nil |
| 153 | + } else { |
| 154 | + return &result |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func insertPageAsImage(pdf_api *asposepdfcloud.PdfApiService, document_name string, imageFileValue string, llx int, lly int, tempFolder string) *asposepdfcloud.AsposeResponse { |
| 159 | + args := map[string]interface{}{ |
| 160 | + "folder": tempFolder, |
| 161 | + } |
| 162 | + |
| 163 | + stamp := asposepdfcloud.ImageStamp{ |
| 164 | + Background: true, |
| 165 | + HorizontalAlignment: asposepdfcloud.HorizontalAlignmentNone, |
| 166 | + VerticalAlignment: asposepdfcloud.VerticalAlignmentNone, |
| 167 | + Opacity: 1, |
| 168 | + Rotate: asposepdfcloud.RotationNone, |
| 169 | + RotateAngle: 0, |
| 170 | + XIndent: -float64(llx), |
| 171 | + YIndent: -float64(lly), |
| 172 | + Zoom: 1, |
| 173 | + FileName: path.Join(tempFolder, imageFileValue), |
| 174 | + } |
| 175 | + stamps := []asposepdfcloud.ImageStamp{stamp} |
| 176 | + |
| 177 | + result, response, err := pdf_api.PostPageImageStamps(document_name, 1, stamps, args) |
| 178 | + if err != nil { |
| 179 | + fmt.Println(err.Error()) |
| 180 | + return nil |
| 181 | + } else if response.StatusCode < 200 || response.StatusCode > 299 { |
| 182 | + fmt.Println("Unexpected error on inserting image to the document!") |
| 183 | + return nil |
| 184 | + } else { |
| 185 | + return &result |
| 186 | + } |
| 187 | +} |
0 commit comments