Skip to content

Commit 644147b

Browse files
authored
Merge pull request #85 from aspose-pdf-cloud/pdfcloud-5011-added-snippets-rotate-resize-crop
PDFCLOUD-5011: added snippets rotate-resize-crop
2 parents b4383be + effe8f5 commit 644147b

File tree

5 files changed

+296
-0
lines changed

5 files changed

+296
-0
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
func main() {
4+
pdfApi := initPdfApi()
5+
6+
rotateDocumentPages(pdfApi, PDF_DOCUMENT, string(ROTATE_PAGES_ANGLE), ROTATE_PAGES, REMOTE_FOLDER)
7+
8+
cropDocumentPage(pdfApi, PDF_DOCUMENT, CROP_PAGE_NUMBER, CROP_LLX, CROP_LLY, CROP_WIDTH, CROP_HEIGHT, CROP_LOCAL_RESULT_DOCUMENT_NAME, LOCAL_FOLDER, REMOTE_FOLDER)
9+
10+
resizeAllPages(pdfApi, PDF_DOCUMENT, RESIZE_PDF_HTML_FILE, RESIZE_NEW_PAGE_WIDTH, RESIZE_NEW_PAGE_HEIGHT, RESIZE_RESULT_DOCUMENT_NAME, LOCAL_FOLDER, REMOTE_FOLDER)
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
asposepdfcloud "github.com/aspose-pdf-cloud/aspose-pdf-cloud-go/v25"
7+
)
8+
9+
func cropDocumentPage(pdf_api *asposepdfcloud.PdfApiService, document_name string, pageNumber int, llx int, lly int, width int, height int, outputDocument string, localFolder string, tempFolder string) {
10+
uploadFile(pdf_api, document_name)
11+
12+
getPageInfo(pdf_api, document_name, pageNumber, tempFolder)
13+
14+
imageFile := extractPdfPage(pdf_api, document_name, pageNumber, int(CROP_PAGE_WIDTH), int(CROP_PAGE_HEIGHT), localFolder, tempFolder)
15+
newPdf := createPdfDocument(pdf_api, outputDocument, width, height, tempFolder)
16+
if newPdf.Code != 200 {
17+
fmt.Println("cropPage(): Failed to create new PDF document!")
18+
} else {
19+
response := insertPageAsImage(pdf_api, outputDocument, imageFile, llx, lly, tempFolder)
20+
if response.Code == 200 {
21+
fmt.Println("cropPage(): Page successfully cropped.")
22+
downloadFile(pdf_api, outputDocument, "cropped_")
23+
} else {
24+
fmt.Println("cropPage(): Can't crop pdf document page!")
25+
}
26+
}
27+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"path/filepath"
6+
7+
asposepdfcloud "github.com/aspose-pdf-cloud/aspose-pdf-cloud-go/v25"
8+
)
9+
10+
func resizeAllPages(pdf_api *asposepdfcloud.PdfApiService, document_name string, htmlTempDoc string, width int, height int, outputDocument string, localFolder string, tempFolder string) {
11+
uploadFile(pdf_api, document_name)
12+
13+
htmlTempPath := filepath.Join(tempFolder, htmlTempDoc)
14+
15+
args := map[string]interface{}{
16+
"folder": tempFolder,
17+
"documentType": string(asposepdfcloud.HtmlDocumentTypeXhtml),
18+
"outputFormat": string(asposepdfcloud.OutputFormatFolder),
19+
}
20+
21+
_, response, err := pdf_api.PutPdfInStorageToHtml(document_name, htmlTempPath, args)
22+
23+
if err != nil {
24+
fmt.Println(err.Error())
25+
} else if response.StatusCode < 200 || response.StatusCode > 299 {
26+
fmt.Println("resizePages(): Can't convert pdf to html!")
27+
} else {
28+
fmt.Println("resizePages(): temporary file '" + htmlTempDoc + "' succesfully creaated.")
29+
}
30+
31+
args2 := map[string]interface{}{
32+
"dstFolder": tempFolder,
33+
"htmlFileName": htmlTempDoc,
34+
"height": float64(height),
35+
"width": float64(width),
36+
}
37+
38+
_, response, err = pdf_api.PutHtmlInStorageToPdf(outputDocument, htmlTempPath, args2)
39+
if err != nil {
40+
fmt.Println(err.Error())
41+
} else if response.StatusCode < 200 || response.StatusCode > 299 {
42+
fmt.Println("resizePages(): Can't convert html to pdf!")
43+
} else {
44+
fmt.Println("resizePages(): Pages successfully resized.")
45+
downloadFile(pdf_api, outputDocument, "resized_doc_")
46+
}
47+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
asposepdfcloud "github.com/aspose-pdf-cloud/aspose-pdf-cloud-go/v25"
7+
)
8+
9+
func rotateDocumentPages(pdf_api *asposepdfcloud.PdfApiService, document_name string, rotate_angle string, pages string, remote_folder string) {
10+
uploadFile(pdf_api, document_name)
11+
12+
args := map[string]interface{}{
13+
"folder": remote_folder,
14+
}
15+
16+
_, httpResponse, err := pdf_api.PostDocumentPagesRotate(document_name, rotate_angle, pages, args)
17+
if err != nil {
18+
fmt.Println(err.Error())
19+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
20+
fmt.Println("Unexpected error!")
21+
} else {
22+
downloadFile(pdf_api, document_name, "rotated_")
23+
}
24+
}

0 commit comments

Comments
 (0)