Skip to content

Commit ec0f1f8

Browse files
authored
Merge pull request #86 from aspose-pdf-cloud/pdfcloud-4966-added-snippets-annotations
PDFCLOUD-4966: added snippets for Annotations
2 parents 644147b + 0a53f69 commit ec0f1f8

11 files changed

+474
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
func main() {
4+
pdfApi := InitPdfApi()
5+
6+
AppendHighlightAnnotation(pdfApi, PDF_DOCUMENT_NAME, PAGE_NUMBER, PDF_OUTPUT, "add_highlight_annotation_", REMOTE_FOLDER)
7+
8+
AppendStrikeoutAnnotation(pdfApi, PDF_DOCUMENT_NAME, PAGE_NUMBER, PDF_OUTPUT, "add_strikeout_annotation_", REMOTE_FOLDER)
9+
10+
AppendTextAnnotation(pdfApi, PDF_DOCUMENT_NAME, PAGE_NUMBER, PDF_OUTPUT, "add_freetext_annotation_", REMOTE_FOLDER)
11+
12+
AppendUnderlineAnnotation(pdfApi, PDF_DOCUMENT_NAME, PAGE_NUMBER, PDF_OUTPUT, "add_underline_annotation_", REMOTE_FOLDER)
13+
14+
annotation_id := RequestAnnotations(pdfApi, PDF_DOCUMENT_NAME, PAGE_NUMBER, REMOTE_FOLDER)
15+
RequestAnnotationById(pdfApi, PDF_DOCUMENT_NAME, annotation_id, REMOTE_FOLDER)
16+
17+
DeleteAnnotation(pdfApi, PDF_DOCUMENT_NAME, ANNOTATION_ID, PDF_OUTPUT)
18+
19+
DeletePageAnnotations(pdfApi, PDF_DOCUMENT_NAME, PAGE_NUMBER, PDF_OUTPUT)
20+
21+
ModifyAnnotation(pdfApi, PDF_DOCUMENT_NAME, PDF_OUTPUT, ANNOTATION_ID, REMOTE_FOLDER)
22+
}
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 DeletePageAnnotations(pdf_api *asposepdfcloud.PdfApiService, document_name string, page_num int32, output_name string) {
10+
// Delete annotation from the PDF document.
11+
UploadFile(pdf_api, document_name)
12+
args := map[string]interface{}{
13+
"folder": REMOTE_FOLDER,
14+
}
15+
16+
_, httpResponse, err := pdf_api.DeletePageAnnotations(document_name, page_num, args)
17+
if err != nil {
18+
fmt.Println(err.Error())
19+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
20+
fmt.Println("DeletePageAnnotations(): Failed to delete annotation from the document.")
21+
} else {
22+
fmt.Println("DeletePageAnnotations(): annotations on page '", page_num, "' deleted from the document '"+document_name+"'.")
23+
DownloadFile(pdf_api, document_name, output_name, "del_page_annotations_")
24+
}
25+
}
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 DeleteAnnotation(pdf_api *asposepdfcloud.PdfApiService, document_name string, annotation_id string, output_name string) {
10+
// Delete annotation from the PDF document.
11+
UploadFile(pdf_api, document_name)
12+
args := map[string]interface{}{
13+
"folder": REMOTE_FOLDER,
14+
}
15+
16+
_, httpResponse, err := pdf_api.DeleteAnnotation(document_name, annotation_id, args)
17+
if err != nil {
18+
fmt.Println(err.Error())
19+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
20+
fmt.Println("DeleteAnnotation(): Failed to delete annotation from the document page.")
21+
} else {
22+
DeletePopupAnnotations(pdf_api, document_name, annotation_id)
23+
fmt.Println("DdeleteAnnotation(): annotation '" + annotation_id + "' deleted from the document '" + document_name + "'.")
24+
DownloadFile(pdf_api, document_name, output_name, "del_annotation_")
25+
}
26+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 RequestAnnotationById(pdf_api *asposepdfcloud.PdfApiService, document_name string, annotation_id string, remote_folder string) {
10+
// Get annotation by Id in the PDF document.
11+
UploadFile(pdf_api, document_name)
12+
args := map[string]interface{}{
13+
"folder": remote_folder,
14+
}
15+
result, httpResponse, err := pdf_api.GetTextAnnotation(document_name, annotation_id, args)
16+
if err != nil {
17+
fmt.Println(err.Error())
18+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
19+
fmt.Println("RequestAnnotationById(): Failed to request text annotation from the document page.")
20+
} else {
21+
fmt.Println("RequestAnnotationById(): annotation '" + annotation_id + "' successfully found '" + result.Annotation.Contents + "' in the document '" + document_name + "'.")
22+
}
23+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 RequestAnnotations(pdf_api *asposepdfcloud.PdfApiService, document_name string, page_num int32, remote_folder string) string {
10+
// Get annotations from the page in the PDF document.
11+
UploadFile(pdf_api, document_name)
12+
args := map[string]interface{}{
13+
"folder": remote_folder,
14+
}
15+
annotation_result := ""
16+
result, httpResponse, err := pdf_api.GetPageAnnotations(document_name, page_num, args)
17+
if err != nil {
18+
fmt.Println(err.Error())
19+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
20+
fmt.Println("RequestAnnotations(): Failed to delete annotation from the document page.")
21+
} else {
22+
for _, annotation := range result.Annotations.List {
23+
if annotation.AnnotationType == asposepdfcloud.AnnotationTypeText {
24+
fmt.Println("RequestAnnotations(): annotation id=", annotation.Id, " with '"+annotation.Contents+"' content get from the document '"+document_name+"' on ", annotation.PageIndex, " page.")
25+
annotation_result := annotation.Id
26+
return annotation_result
27+
}
28+
}
29+
}
30+
fmt.Println("RequestAnnotations(): Failed to get annotation in the document.")
31+
return annotation_result
32+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 AppendHighlightAnnotation(pdf_api *asposepdfcloud.PdfApiService, document_name string, page_num int32, output_document string, prefix string, remote_folder string) {
10+
// Append a new highlight text annotation to the PDF document.
11+
UploadFile(pdf_api, document_name)
12+
args := map[string]interface{}{
13+
"folder": remote_folder,
14+
}
15+
new_annotation := asposepdfcloud.HighlightAnnotation{
16+
Name: "Highlight_Text_Annotation",
17+
Rect: &asposepdfcloud.Rectangle{LLX: 100, LLY: 350, URX: 450, URY: 400},
18+
Flags: []asposepdfcloud.AnnotationFlags{asposepdfcloud.AnnotationFlagsDefault},
19+
HorizontalAlignment: asposepdfcloud.HorizontalAlignmentLeft,
20+
VerticalAlignment: asposepdfcloud.VerticalAlignmentTop,
21+
RichText: NEW_HL_ANNOTATION_TEXT,
22+
Subject: NEW_HL_ANNOTATION_SUBJECT,
23+
Contents: NEW_HL_ANNOTATION_CONTENTS,
24+
Title: NEW_HL_ANNOTATION_DESCRIPTION,
25+
ZIndex: 1,
26+
Color: &asposepdfcloud.Color{A: 0xFF, R: 0x00, G: 0xFF, B: 0x00},
27+
QuadPoints: []asposepdfcloud.Point{
28+
{X: 10, Y: 10},
29+
{X: 20, Y: 10},
30+
{X: 10, Y: 20},
31+
{X: 10, Y: 10},
32+
},
33+
Modified: "03/27/2025 00:00:00.000 AM",
34+
}
35+
_, httpResponse, err := pdf_api.PostPageHighlightAnnotations(document_name, page_num, []asposepdfcloud.HighlightAnnotation{new_annotation}, args)
36+
if err != nil {
37+
fmt.Println(err.Error())
38+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
39+
fmt.Println("AppendHighlightAnnotation(): Failed to append annotation to the document page.")
40+
} else {
41+
fmt.Println("AppendHighlightAnnotation(): annotation '" + NEW_HL_ANNOTATION_TEXT + "' added to the document '" + document_name + "'.")
42+
DownloadFile(pdf_api, document_name, output_document, prefix)
43+
}
44+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 AppendStrikeoutAnnotation(pdf_api *asposepdfcloud.PdfApiService, document_name string, page_num int32, output_document string, prefix string, remote_folder string) {
10+
// Append a new strikeout text annotation to the PDF document.
11+
UploadFile(pdf_api, document_name)
12+
args := map[string]interface{}{
13+
"folder": remote_folder,
14+
}
15+
16+
new_annotation := asposepdfcloud.StrikeOutAnnotation{
17+
Name: "Highlight_Text_Annotation",
18+
Rect: &asposepdfcloud.Rectangle{LLX: 100, LLY: 350, URX: 450, URY: 400},
19+
Flags: []asposepdfcloud.AnnotationFlags{asposepdfcloud.AnnotationFlagsDefault},
20+
HorizontalAlignment: asposepdfcloud.HorizontalAlignmentLeft,
21+
VerticalAlignment: asposepdfcloud.VerticalAlignmentTop,
22+
RichText: NEW_SO_ANNOTATION_TEXT,
23+
Subject: NEW_SO_ANNOTATION_SUBJECT,
24+
Contents: NEW_SO_ANNOTATION_CONTENTS,
25+
Title: NEW_SO_ANNOTATION_DESCRIPTION,
26+
ZIndex: 1,
27+
Color: &asposepdfcloud.Color{A: 0xFF, R: 0x00, G: 0xFF, B: 0x00},
28+
QuadPoints: []asposepdfcloud.Point{
29+
{X: 10, Y: 10},
30+
{X: 20, Y: 10},
31+
{X: 10, Y: 20},
32+
{X: 10, Y: 10},
33+
},
34+
Modified: "03/27/2025 00:00:00.000 AM",
35+
}
36+
37+
_, httpResponse, err := pdf_api.PostPageStrikeOutAnnotations(document_name, page_num, []asposepdfcloud.StrikeOutAnnotation{new_annotation}, args)
38+
if err != nil {
39+
fmt.Println(err.Error())
40+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
41+
fmt.Println("AppendStrikeoutAnnotation(): Failed to append annotation to the document page.")
42+
} else {
43+
fmt.Println("AppendStrikeoutAnnotation(): annotation '" + NEW_SO_ANNOTATION_TEXT + "' added to the document '" + document_name + "'.")
44+
DownloadFile(pdf_api, document_name, output_document, prefix)
45+
}
46+
}
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 AppendTextAnnotation(pdf_api *asposepdfcloud.PdfApiService, document_name string, page_num int32, output_document string, prefix string, remote_folder string) {
10+
// Append a new free text annotation to the PDF document.
11+
UploadFile(pdf_api, document_name)
12+
args := map[string]interface{}{
13+
"folder": remote_folder,
14+
}
15+
16+
text_style := asposepdfcloud.TextStyle{
17+
FontSize: 20,
18+
Font: "Arial",
19+
ForegroundColor: &asposepdfcloud.Color{A: 0xFF, R: 0x00, G: 0xFF, B: 0x00},
20+
BackgroundColor: &asposepdfcloud.Color{A: 0xFF, R: 0xFF, G: 0x00, B: 0x00},
21+
}
22+
23+
new_annotation := asposepdfcloud.FreeTextAnnotation{
24+
Rect: &asposepdfcloud.Rectangle{LLX: 100, LLY: 350, URX: 450, URY: 400},
25+
TextStyle: &text_style,
26+
Name: "Free Text Annotation",
27+
Flags: []asposepdfcloud.AnnotationFlags{asposepdfcloud.AnnotationFlagsDefault},
28+
HorizontalAlignment: asposepdfcloud.HorizontalAlignmentCenter,
29+
Intent: asposepdfcloud.FreeTextIntentFreeTextTypeWriter,
30+
RichText: NEW_FT_ANNOTATION_TEXT,
31+
Subject: NEW_FT_ANNOTATION_SUBJECT,
32+
Contents: NEW_FT_ANNOTATION_CONTENTS,
33+
Title: NEW_FT_ANNOTATION_DESCRIPTION,
34+
ZIndex: 1,
35+
Justification: asposepdfcloud.JustificationCenter,
36+
}
37+
38+
_, httpResponse, err := pdf_api.PostPageFreeTextAnnotations(document_name, page_num, []asposepdfcloud.FreeTextAnnotation{new_annotation}, args)
39+
if err != nil {
40+
fmt.Println(err.Error())
41+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
42+
fmt.Println("AppendTextAnnotation(): Failed to append annotation to the document page.")
43+
} else {
44+
fmt.Println("AppendTextAnnotation(): annotation '" + NEW_FT_ANNOTATION_TEXT + "' added to the document '" + document_name + "'.")
45+
DownloadFile(pdf_api, document_name, output_document, prefix)
46+
}
47+
}

0 commit comments

Comments
 (0)