Skip to content

Commit 5482d3f

Browse files
authored
Merge pull request #81 from aspose-pdf-cloud/pdfcloud-4867-added-snippets-links
PDFCLOUD-4867: added snippets for Links
2 parents e416f23 + 8ae5f40 commit 5482d3f

File tree

7 files changed

+265
-0
lines changed

7 files changed

+265
-0
lines changed

uses_cases/links/append_link.go

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 appendLink(pdf_api *asposepdfcloud.PdfApiService, document string, output_document string, page_num int32, link_action string, rect *asposepdfcloud.Rectangle, remote_folder string) {
10+
uploadFile(pdf_api, document)
11+
12+
args := map[string]interface{}{
13+
"folder": remote_folder,
14+
}
15+
16+
link := asposepdfcloud.Link{Href: link_action}
17+
18+
link_annotation := asposepdfcloud.LinkAnnotation{
19+
Links: []asposepdfcloud.Link{link},
20+
ActionType: asposepdfcloud.LinkActionTypeGoToURIAction,
21+
Action: link_action,
22+
Highlighting: asposepdfcloud.LinkHighlightingModeInvert,
23+
Color: &asposepdfcloud.Color{A: 0xFF, R: 0xAA, G: 0x00, B: 0x00},
24+
Rect: rect,
25+
}
26+
27+
result, httpResponse, err := pdf_api.PostPageLinkAnnotations(
28+
document, page_num, []asposepdfcloud.LinkAnnotation{link_annotation}, args,
29+
)
30+
if err != nil {
31+
fmt.Println(err.Error())
32+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
33+
fmt.Println("Unexpected error!")
34+
} else {
35+
fmt.Println(result)
36+
37+
downloadFile(pdf_api, document, output_document)
38+
}
39+
}

uses_cases/links/extract_link.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 getLink(pdf_api *asposepdfcloud.PdfApiService, document string, link_id string, remote_folder string) {
10+
uploadFile(pdf_api, document)
11+
12+
args := map[string]interface{}{
13+
"folder": remote_folder,
14+
}
15+
16+
result, httpResponse, err := pdf_api.GetLinkAnnotation(document, link_id, 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+
links := []asposepdfcloud.LinkAnnotation{*result.Link}
23+
showLinks(&links)
24+
}
25+
}

uses_cases/links/extract_links.go

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 getLinks(pdf_api *asposepdfcloud.PdfApiService, document string, page_num int32, remote_folder string) {
10+
uploadFile(pdf_api, document)
11+
12+
args := map[string]interface{}{
13+
"folder": remote_folder,
14+
}
15+
16+
result, httpResponse, err := pdf_api.GetPageLinkAnnotations(document, page_num, 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+
showLinks(&result.Links.List)
23+
}
24+
}

uses_cases/links/links_launch.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
func main() {
4+
pdfApi := initPdfApi()
5+
6+
appendLink(pdfApi, PDF_DOCUMENT, PDF_OUTPUT, PAGE_NUMBER, NEW_LINK_ACTION, &LINK_RECT, REMOTE_FOLDER)
7+
8+
getLinks(pdfApi, PDF_DOCUMENT, PAGE_NUMBER, REMOTE_FOLDER)
9+
10+
getLink(pdfApi, PDF_DOCUMENT, LINK_ID, REMOTE_FOLDER)
11+
12+
removeLink(pdfApi, PDF_DOCUMENT, PDF_OUTPUT_REMOVE, LINK_ID, REMOTE_FOLDER)
13+
14+
replaceLink(pdfApi, PDF_DOCUMENT, PDF_OUTPUT_REPLACE, LINK_ID, NEW_LINK_ACTION, REMOTE_FOLDER)
15+
}

uses_cases/links/links_main.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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_sample_add_link.pdf"
17+
PDF_OUTPUT_REMOVE = "output_sample_rem_link.pdf"
18+
PDF_OUTPUT_REPLACE = "output_sample_rep_link.pdf"
19+
NEW_LINK_ACTION = "https://reference.aspose.cloud/pdf/"
20+
PAGE_NUMBER = 2
21+
LINK_ID = "GI5UO32UN5KVESKBMN2GS33OHMYTINBMGQ4DQLBRHA2SYNBZHE"
22+
)
23+
24+
var (
25+
LINK_RECT = asposepdfcloud.Rectangle{LLX: 238, LLY: 488.622, URX: 305, URY: 498.588}
26+
)
27+
28+
func initPdfApi() *asposepdfcloud.PdfApiService {
29+
AppSID := "******"
30+
AppKey := "******"
31+
32+
pdfApi := asposepdfcloud.NewPdfApiService(AppSID, AppKey, "")
33+
return pdfApi
34+
}
35+
36+
// Upload local file to the remote folder with check errors
37+
func uploadFile(pdf_api *asposepdfcloud.PdfApiService, name string) {
38+
args := map[string]interface{}{
39+
"folder": REMOTE_FOLDER,
40+
}
41+
file, err := os.Open(filepath.Join(LOCAL_FOLDER, name))
42+
if err != nil {
43+
fmt.Println(err.Error())
44+
} else {
45+
result, httpResponse, err := pdf_api.UploadFile(path.Join(REMOTE_FOLDER, name), file, args)
46+
if err != nil {
47+
fmt.Println(err.Error())
48+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
49+
fmt.Println("Unexpected error!")
50+
} else {
51+
fmt.Println(result)
52+
}
53+
}
54+
}
55+
56+
// Download file from remote folder and save it locally with check errors
57+
func downloadFile(pdf_api *asposepdfcloud.PdfApiService, name string, output_name string) {
58+
args := map[string]interface{}{
59+
"folder": REMOTE_FOLDER,
60+
}
61+
result_data, httpResponse, err := pdf_api.DownloadFile(path.Join(REMOTE_FOLDER, name), args)
62+
if err != nil {
63+
fmt.Println(err.Error())
64+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
65+
fmt.Println("Unexpected error!")
66+
} else {
67+
fileName := path.Join(LOCAL_FOLDER, output_name)
68+
f, _ := os.Create(fileName)
69+
_, _ = f.Write(result_data)
70+
fmt.Println("File '" + fileName + "'successfully downloaded.")
71+
}
72+
}
73+
74+
func showLinks(links *[]asposepdfcloud.LinkAnnotation) {
75+
for i := 0; i < len(*links); i++ {
76+
fmt.Print("Link #")
77+
fmt.Println(i)
78+
fmt.Print("\tId == '")
79+
fmt.Print((*links)[i].Id)
80+
fmt.Println("'")
81+
fmt.Print("\tActionType == '")
82+
fmt.Print((*links)[i].ActionType)
83+
fmt.Println("'")
84+
fmt.Print("\tAction == '")
85+
fmt.Print((*links)[i].Action)
86+
fmt.Println("'")
87+
}
88+
}

uses_cases/links/remove_link.go

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 removeLink(pdf_api *asposepdfcloud.PdfApiService, document string, output_document string, link_id string, remote_folder string) {
10+
uploadFile(pdf_api, document)
11+
12+
getLink(pdf_api, document, link_id, remote_folder)
13+
14+
args := map[string]interface{}{
15+
"folder": remote_folder,
16+
}
17+
18+
result, httpResponse, err := pdf_api.DeleteLinkAnnotation(document, link_id, args)
19+
if err != nil {
20+
fmt.Println(err.Error())
21+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
22+
fmt.Println("Unexpected error!")
23+
} else {
24+
fmt.Println(result)
25+
downloadFile(pdf_api, document, output_document)
26+
}
27+
}

uses_cases/links/replace_link.go

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+
6+
asposepdfcloud "github.com/aspose-pdf-cloud/aspose-pdf-cloud-go/v25"
7+
)
8+
9+
func replaceLink(pdf_api *asposepdfcloud.PdfApiService, document string, output_document string, link_id string, link_action string, remote_folder string) {
10+
uploadFile(pdf_api, document)
11+
12+
args := map[string]interface{}{
13+
"folder": remote_folder,
14+
}
15+
16+
result, httpResponse, err := pdf_api.GetLinkAnnotation(document, link_id, 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+
links := []asposepdfcloud.LinkAnnotation{*result.Link}
23+
showLinks(&links)
24+
25+
link := asposepdfcloud.Link{Href: link_action}
26+
27+
link_annotation := asposepdfcloud.LinkAnnotation{
28+
Links: []asposepdfcloud.Link{link},
29+
ActionType: asposepdfcloud.LinkActionTypeGoToURIAction,
30+
Action: link_action,
31+
Highlighting: asposepdfcloud.LinkHighlightingModeInvert,
32+
Color: &asposepdfcloud.Color{A: 0xFF, R: 0xAA, G: 0x00, B: 0x00},
33+
Rect: result.Link.Rect,
34+
}
35+
36+
result2, httpResponse, err := pdf_api.PutLinkAnnotation(PDF_DOCUMENT, result.Link.Id, link_annotation, args)
37+
if err != nil {
38+
fmt.Println(err.Error())
39+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
40+
fmt.Println("Unexpected error!")
41+
} else {
42+
fmt.Println(result2)
43+
44+
downloadFile(pdf_api, document, output_document)
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)