Skip to content

Commit b56fb5e

Browse files
authored
Merge pull request #88 from aspose-pdf-cloud/pdfcloud-4968-added-snippets-pages
PDFCLOUD-4968: added snippets for Pages
2 parents 381763c + bef9a7f commit b56fb5e

File tree

10 files changed

+336
-0
lines changed

10 files changed

+336
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path"
7+
8+
asposepdfcloud "github.com/aspose-pdf-cloud/aspose-pdf-cloud-go/v25"
9+
)
10+
11+
func SavePageAsPNG(pdf_api *asposepdfcloud.PdfApiService, document string, pageNumber int32, outputPNG string, remoteFolder string) {
12+
// Show page information of the PDF document.
13+
uploadFile(pdf_api, document)
14+
15+
args := map[string]interface{}{
16+
"folder": remoteFolder,
17+
}
18+
19+
result, httpResponse, err := pdf_api.GetPageConvertToPng(document, pageNumber, args)
20+
if err != nil {
21+
fmt.Println(err.Error())
22+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
23+
fmt.Println("Unexpected error!")
24+
} else {
25+
fileName := path.Join(LOCAL_FOLDER, outputPNG)
26+
f, _ := os.Create(fileName)
27+
_, _ = f.Write(result)
28+
fmt.Println("File '" + outputPNG + "' successfully downloaded.")
29+
}
30+
}

uses_cases/pages/pages_add.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 AppendNewPage(pdf_api *asposepdfcloud.PdfApiService, document string, outputDocument string, remoteFolder string) {
10+
// Append page to the PDF document.
11+
uploadFile(pdf_api, document)
12+
13+
args := map[string]interface{}{
14+
"folder": remoteFolder,
15+
}
16+
17+
result, httpResponse, err := pdf_api.PutAddNewPage(document, args)
18+
if err != nil {
19+
fmt.Println(err.Error())
20+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
21+
fmt.Println("Unexpected error!")
22+
} else {
23+
fmt.Println("Successfully added new page. ", result.Pages.List)
24+
downloadFile(pdf_api, document, outputDocument, "append_")
25+
}
26+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"path"
6+
7+
asposepdfcloud "github.com/aspose-pdf-cloud/aspose-pdf-cloud-go/v25"
8+
)
9+
10+
func AppendPageStampImage(pdf_api *asposepdfcloud.PdfApiService, documentName string, outputDocument string, imageFileName string, pageNumber int32, width float64, height float64, remoteFolder string) {
11+
// Append image stamp to page of the PDF document.
12+
uploadFile(pdf_api, documentName)
13+
uploadFile(pdf_api, imageFileName)
14+
15+
args := map[string]interface{}{
16+
"folder": remoteFolder,
17+
}
18+
19+
stamp := asposepdfcloud.ImageStamp{
20+
Background: true,
21+
HorizontalAlignment: asposepdfcloud.HorizontalAlignmentCenter,
22+
VerticalAlignment: asposepdfcloud.VerticalAlignmentCenter,
23+
Opacity: 1,
24+
Rotate: asposepdfcloud.RotationNone,
25+
RotateAngle: 45,
26+
Width: width,
27+
Height: height,
28+
Zoom: 1,
29+
FileName: path.Join(remoteFolder, imageFileName),
30+
}
31+
_, httpResponse, err := pdf_api.PostPageImageStamps(documentName, pageNumber, []asposepdfcloud.ImageStamp{stamp}, args)
32+
if err != nil {
33+
fmt.Println(err.Error())
34+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
35+
fmt.Println("appendStampImage()): Failed to append image stamp to the document.")
36+
} else {
37+
fmt.Println("appendStampImage(): image stamp '"+imageFileName+"' appended successfully pn page", pageNumber, " to the document '"+documentName+"'.")
38+
downloadFile(pdf_api, documentName, outputDocument, "add_image_stamp_")
39+
}
40+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 AppendPageStampText(pdf_api *asposepdfcloud.PdfApiService, documentName string, pageNumber int32, outputDocument string, text_value string, remoteFolder string) {
10+
// Append text stamp to page of the PDF document.
11+
uploadFile(pdf_api, documentName)
12+
13+
args := map[string]interface{}{
14+
"folder": remoteFolder,
15+
}
16+
17+
textStamp := asposepdfcloud.TextStamp{
18+
Background: true,
19+
HorizontalAlignment: asposepdfcloud.HorizontalAlignmentCenter,
20+
VerticalAlignment: asposepdfcloud.VerticalAlignmentCenter,
21+
Opacity: 1,
22+
Rotate: asposepdfcloud.RotationNone,
23+
RotateAngle: 30,
24+
Zoom: 1,
25+
TextAlignment: asposepdfcloud.HorizontalAlignmentCenter,
26+
Value: text_value,
27+
TextState: &asposepdfcloud.TextState{FontSize: 14, FontStyle: asposepdfcloud.FontStylesBoldItalic, Font: "Arial", ForegroundColor: &asposepdfcloud.Color{A: 0xFF, R: 0xFF, G: 0x00, B: 0x00}},
28+
}
29+
30+
_, httpResponse, err := pdf_api.PostPageTextStamps(documentName, pageNumber, []asposepdfcloud.TextStamp{textStamp}, args)
31+
if err != nil {
32+
fmt.Println(err.Error())
33+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
34+
fmt.Println("appendStampText(): Failed to append text stamp to the document.")
35+
} else {
36+
fmt.Println("appendStampText(): text stamp '"+text_value+"' appended successfully on page", pageNumber, " to the document '"+documentName+"'.")
37+
downloadFile(pdf_api, documentName, outputDocument, "add_text_stamp_")
38+
}
39+
}

uses_cases/pages/pages_delete.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 DeletePage(pdf_api *asposepdfcloud.PdfApiService, document string, pageNumber int32, outputDocument string, remoteFolder string) {
10+
// Delete page of the PDF document.
11+
uploadFile(pdf_api, document)
12+
13+
args := map[string]interface{}{
14+
"folder": remoteFolder,
15+
}
16+
17+
_, httpResponse, err := pdf_api.DeletePage(document, pageNumber, args)
18+
if err != nil {
19+
fmt.Println(err.Error())
20+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
21+
fmt.Println("Unexpected error!")
22+
} else {
23+
fmt.Println("Successfully delete ", pageNumber, " page.")
24+
downloadFile(pdf_api, document, outputDocument, "delete_")
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 ExtractPageInfo(pdf_api *asposepdfcloud.PdfApiService, document string, pageNumber int32, remoteFolder string) {
10+
// Get page information of the PDF document.
11+
uploadFile(pdf_api, document)
12+
13+
args := map[string]interface{}{
14+
"folder": remoteFolder,
15+
}
16+
17+
result, httpResponse, err := pdf_api.GetPage(document, pageNumber, args)
18+
if err != nil {
19+
fmt.Println(err.Error())
20+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
21+
fmt.Println("Unexpected error!")
22+
} else {
23+
fmt.Println("Successfully extract page :", pageNumber, "Width :", result.Page.Rectangle.URX, "Height: ", result.Page.Rectangle.URY)
24+
25+
}
26+
}
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 GetPagesWordsCount(pdf_api *asposepdfcloud.PdfApiService, document string, remoteFolder string) {
10+
// Get page information of the PDF document.
11+
uploadFile(pdf_api, document)
12+
13+
args := map[string]interface{}{
14+
"folder": remoteFolder,
15+
}
16+
17+
result, httpResponse, err := pdf_api.GetWordsPerPage(document, args)
18+
if err != nil {
19+
fmt.Println(err.Error())
20+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
21+
fmt.Println("Unexpected error!")
22+
} else {
23+
for _, p := range result.WordsPerPage.List {
24+
fmt.Println("Page ", p.PageNumber, " has ", p.Count, " words.")
25+
}
26+
}
27+
}

uses_cases/pages/pages_helper.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
REMOTE_FOLDER = "Your_Temp_Pdf_Cloud"
14+
LOCAL_FOLDER = "c:\\Samples"
15+
PDF_DOCUMENT = "sample.pdf"
16+
PDF_OUTPUT = "output_pages.pdf"
17+
PNG_PAGE_OUTPUT = "output_sample_page.png"
18+
PAGE_NUMBER = 2
19+
20+
STAMP_TEXT = "NEW TEXT STAMP"
21+
22+
IMAGE_STAMP_FILE = "sample.png"
23+
IMAGE_STAMP_LLY = 800
24+
IMAGE_STAMP_WIDTH = 24
25+
IMAGE_STAMP_HEIGHT = 24
26+
27+
API_SID = "****************" // Your Application SID
28+
API_KEY = "****************" // Your Application Key
29+
)
30+
31+
func initPdfApi(appSID string, appKey string) *asposepdfcloud.PdfApiService {
32+
AppSID := appSID
33+
AppKey := appKey
34+
35+
pdfApi := asposepdfcloud.NewPdfApiService(AppSID, AppKey, "")
36+
return pdfApi
37+
}
38+
39+
// Upload local file to the remote folder with check errors
40+
func uploadFile(pdf_api *asposepdfcloud.PdfApiService, name string) {
41+
args := map[string]interface{}{
42+
"folder": REMOTE_FOLDER,
43+
}
44+
file, err := os.Open(filepath.Join(LOCAL_FOLDER, name))
45+
if err != nil {
46+
fmt.Println(err.Error())
47+
} else {
48+
_, httpResponse, err := pdf_api.UploadFile(path.Join(REMOTE_FOLDER, name), file, args)
49+
if err != nil {
50+
fmt.Println(err.Error())
51+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
52+
fmt.Println("Unexpected error!")
53+
} else {
54+
fmt.Println("File '" + name + " ' succeddfully uplouaded.")
55+
}
56+
}
57+
}
58+
59+
// Download file from remote folder and save it locally with check errors
60+
func downloadFile(pdf_api *asposepdfcloud.PdfApiService, name string, output_name string, prefix string) {
61+
args := map[string]interface{}{
62+
"folder": REMOTE_FOLDER,
63+
}
64+
result_data, httpResponse, err := pdf_api.DownloadFile(path.Join(REMOTE_FOLDER, name), args)
65+
if err != nil {
66+
fmt.Println(err.Error())
67+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
68+
fmt.Println("Unexpected error!")
69+
} else {
70+
fileName := path.Join(LOCAL_FOLDER, prefix+output_name)
71+
f, _ := os.Create(fileName)
72+
_, _ = f.Write(result_data)
73+
fmt.Println("File '" + prefix + fileName + "' successfully downloaded.")
74+
}
75+
}

uses_cases/pages/pages_launch.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
func main() {
4+
pdfApi := initPdfApi(PDF_API_SID, PDF_API_KEY)
5+
6+
AppendNewPage(pdfApi, PDF_DOCUMENT, PDF_OUTPUT, REMOTE_FOLDER)
7+
8+
ExtractPageInfo(pdfApi, PDF_DOCUMENT, PAGE_NUMBER, REMOTE_FOLDER)
9+
10+
SavePageAsPNG(pdfApi, PDF_DOCUMENT, PAGE_NUMBER, PNG_PAGE_OUTPUT, REMOTE_FOLDER)
11+
12+
MovePage(pdfApi, PDF_DOCUMENT, PAGE_NUMBER, PAGE_NUMBER+1, PDF_OUTPUT, REMOTE_FOLDER)
13+
14+
DeletePage(pdfApi, PDF_DOCUMENT, PAGE_NUMBER, PDF_OUTPUT, REMOTE_FOLDER)
15+
16+
AppendPageStampText(pdfApi, PDF_DOCUMENT, PAGE_NUMBER, PDF_OUTPUT, STAMP_TEXT, REMOTE_FOLDER)
17+
18+
AppendPageStampImage(pdfApi, PDF_DOCUMENT, PDF_OUTPUT, IMAGE_STAMP_FILE, PAGE_NUMBER, IMAGE_STAMP_WIDTH, IMAGE_STAMP_HEIGHT, REMOTE_FOLDER)
19+
20+
GetPagesWordsCount(pdfApi, PDF_DOCUMENT, REMOTE_FOLDER)
21+
}

uses_cases/pages/pages_move.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 MovePage(pdf_api *asposepdfcloud.PdfApiService, document string, pageNumber int32, newPageNumber int32, outputDocument string, remoteFolder string) {
10+
// Move page to new position in the PDF document.
11+
uploadFile(pdf_api, document)
12+
13+
args := map[string]interface{}{
14+
"folder": remoteFolder,
15+
}
16+
17+
_, httpResponse, err := pdf_api.PostMovePage(document, pageNumber, newPageNumber, args)
18+
if err != nil {
19+
fmt.Println(err.Error())
20+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
21+
fmt.Println("Unexpected error!")
22+
} else {
23+
fmt.Println("Successfully moe page fron ", pageNumber, "to", newPageNumber, "position.")
24+
downloadFile(pdf_api, document, outputDocument, "move_")
25+
}
26+
}

0 commit comments

Comments
 (0)