Skip to content

PDFCLOUD-4966: added snippets for Annotations #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions uses_cases/annotations/annotations_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package main

import (
"fmt"
"os"
"path"
"path/filepath"

asposepdfcloud "github.com/aspose-pdf-cloud/aspose-pdf-cloud-go/v25"
)

const (
LOCAL_FOLDER = "C:\\Samples"
REMOTE_FOLDER = "Your_Temp_Pdf_Cloud"
PDF_DOCUMENT_NAME = "sample.pdf"
PDF_OUTPUT = "output_sample.pdf"
PAGE_NUMBER = 1

ANNOTATION_ID = "GE5TAOZTHA2CYMRZGUWDIMBZFQZTEMA"

NEW_HL_ANNOTATION_TEXT = "NEW HIGHLIGHT TEXT ANNOTATION"
NEW_HL_ANNOTATION_DESCRIPTION = "This is a sample highlight annotation"
NEW_HL_ANNOTATION_SUBJECT = "Highlight Text Box Subject"
NEW_HL_ANNOTATION_CONTENTS = "Highlight annotation sample contents"

NEW_SO_ANNOTATION_TEXT = "NEW STRIKEOUT TEXT ANNOTATION"
NEW_SO_ANNOTATION_DESCRIPTION = "This is a sample strikeout annotation"
NEW_SO_ANNOTATION_SUBJECT = "Strikeout Text Box Subject"
NEW_SO_ANNOTATION_CONTENTS = "Strikeout annotation sample contents"

NEW_UL_ANNOTATION_TEXT = "NEW UNDERLINE TEXT ANNOTATION"
NEW_UL_ANNOTATION_DESCRIPTION = "This is a sample underline annotation"
NEW_UL_ANNOTATION_SUBJECT = "Underline Text Box Subject"
NEW_UL_ANNOTATION_CONTENTS = "Underline annotation sample contents"

NEW_FT_ANNOTATION_TEXT = "NEW FREE TEXT ANNOTATION"
NEW_FT_ANNOTATION_DESCRIPTION = "This is a sample annotation"
NEW_FT_ANNOTATION_SUBJECT = "Free Text Box Subject"
NEW_FT_ANNOTATION_CONTENTS = "Free Text annotation sample contents"

REPLACED_CONTENT = "This is a replaced sample annotation"
)

func InitPdfApi() *asposepdfcloud.PdfApiService {
// Initialize Credentials and create Pdf.Cloud service object
AppSID := "*********" // Your Application SID
AppKey := "*********" // Your Application Key

pdfApi := asposepdfcloud.NewPdfApiService(AppSID, AppKey, "")
return pdfApi
}

func UploadFile(pdf_api *asposepdfcloud.PdfApiService, name string) {
// Upload local file to the Pdf.Cloud folder
args := map[string]interface{}{
"folder": REMOTE_FOLDER,
}
file, _ := os.Open(filepath.Join(LOCAL_FOLDER, name))
_, httpResponse, err := pdf_api.UploadFile(filepath.Join(REMOTE_FOLDER, name), file, args)
if err != nil {
fmt.Println(err.Error())
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
fmt.Println("Unexpected error!")
} else {
fmt.Println("Result file'" + name + "' successfully uploaded!")
}
}

func SaveByteArrayToFile(local_folder string, file_name string, data []byte) {
// Save byte array data to the local folder
fileName := path.Join(local_folder, file_name)
f, err := os.Create(fileName)
if err != nil {
fmt.Println(err)
} else {
size, err := f.Write(data)
if err != nil || size == 0 {
fmt.Println("Failures in downloading result!")
} else {
fmt.Println("Result file'" + fileName + "' successfully downloaded!")
}
}
}

func DownloadFile(pdf_api *asposepdfcloud.PdfApiService, name string, output_name string, prefix string) {
// Download modified Pdf document to local folder from the Pdf.Cloud folder
args := map[string]interface{}{
"folder": REMOTE_FOLDER,
}
result_data, _, _ := pdf_api.DownloadFile(path.Join(REMOTE_FOLDER, name), args)
SaveByteArrayToFile(LOCAL_FOLDER, prefix+output_name, result_data)
}

func DeletePopupAnnotations(pdf_api *asposepdfcloud.PdfApiService, document_name string, parent_annotation string) {
// Delete popup annotations for typed parent annotation in the page in the PDF document.
args := map[string]interface{}{
"folder": REMOTE_FOLDER,
}
result, httpResponse, err := pdf_api.GetDocumentPopupAnnotations(document_name, args)
if err != nil {
fmt.Println(err.Error())
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
fmt.Println("Unexpected error!")
} else {
for _, annotation := range result.Annotations.List {
if annotation.Parent.Id == parent_annotation {
_, response, err2 := pdf_api.DeleteAnnotation(document_name, annotation.Id, args)
if err2 != nil {
fmt.Println(err2)
} else if response.StatusCode < 200 || response.StatusCode > 299 {
fmt.Println("delete_popup_annotations(): Failed to delete popup annotation in the document.")
} else {
fmt.Println("delete_popup_annotations(): popup annotation id = '" + annotation.Id + "' for '" + annotation.Contents + "' deleted in the document '" + PDF_DOCUMENT_NAME + "'.")
}
}
}
}
}
22 changes: 22 additions & 0 deletions uses_cases/annotations/annotations_launch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

func main() {
pdfApi := InitPdfApi()

AppendHighlightAnnotation(pdfApi, PDF_DOCUMENT_NAME, PAGE_NUMBER, PDF_OUTPUT, "add_highlight_annotation_", REMOTE_FOLDER)

AppendStrikeoutAnnotation(pdfApi, PDF_DOCUMENT_NAME, PAGE_NUMBER, PDF_OUTPUT, "add_strikeout_annotation_", REMOTE_FOLDER)

AppendTextAnnotation(pdfApi, PDF_DOCUMENT_NAME, PAGE_NUMBER, PDF_OUTPUT, "add_freetext_annotation_", REMOTE_FOLDER)

AppendUnderlineAnnotation(pdfApi, PDF_DOCUMENT_NAME, PAGE_NUMBER, PDF_OUTPUT, "add_underline_annotation_", REMOTE_FOLDER)

annotation_id := RequestAnnotations(pdfApi, PDF_DOCUMENT_NAME, PAGE_NUMBER, REMOTE_FOLDER)
RequestAnnotationById(pdfApi, PDF_DOCUMENT_NAME, annotation_id, REMOTE_FOLDER)

DeleteAnnotation(pdfApi, PDF_DOCUMENT_NAME, ANNOTATION_ID, PDF_OUTPUT)

DeletePageAnnotations(pdfApi, PDF_DOCUMENT_NAME, PAGE_NUMBER, PDF_OUTPUT)

ModifyAnnotation(pdfApi, PDF_DOCUMENT_NAME, PDF_OUTPUT, ANNOTATION_ID, REMOTE_FOLDER)
}
25 changes: 25 additions & 0 deletions uses_cases/annotations/delete_page_annotations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"

asposepdfcloud "github.com/aspose-pdf-cloud/aspose-pdf-cloud-go/v25"
)

func DeletePageAnnotations(pdf_api *asposepdfcloud.PdfApiService, document_name string, page_num int32, output_name string) {
// Delete annotation from the PDF document.
UploadFile(pdf_api, document_name)
args := map[string]interface{}{
"folder": REMOTE_FOLDER,
}

_, httpResponse, err := pdf_api.DeletePageAnnotations(document_name, page_num, args)
if err != nil {
fmt.Println(err.Error())
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
fmt.Println("DeletePageAnnotations(): Failed to delete annotation from the document.")
} else {
fmt.Println("DeletePageAnnotations(): annotations on page '", page_num, "' deleted from the document '"+document_name+"'.")
DownloadFile(pdf_api, document_name, output_name, "del_page_annotations_")
}
}
26 changes: 26 additions & 0 deletions uses_cases/annotations/delete_text_annotation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"fmt"

asposepdfcloud "github.com/aspose-pdf-cloud/aspose-pdf-cloud-go/v25"
)

func DeleteAnnotation(pdf_api *asposepdfcloud.PdfApiService, document_name string, annotation_id string, output_name string) {
// Delete annotation from the PDF document.
UploadFile(pdf_api, document_name)
args := map[string]interface{}{
"folder": REMOTE_FOLDER,
}

_, httpResponse, err := pdf_api.DeleteAnnotation(document_name, annotation_id, args)
if err != nil {
fmt.Println(err.Error())
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
fmt.Println("DeleteAnnotation(): Failed to delete annotation from the document page.")
} else {
DeletePopupAnnotations(pdf_api, document_name, annotation_id)
fmt.Println("DdeleteAnnotation(): annotation '" + annotation_id + "' deleted from the document '" + document_name + "'.")
DownloadFile(pdf_api, document_name, output_name, "del_annotation_")
}
}
23 changes: 23 additions & 0 deletions uses_cases/annotations/get_annotation_by_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"

asposepdfcloud "github.com/aspose-pdf-cloud/aspose-pdf-cloud-go/v25"
)

func RequestAnnotationById(pdf_api *asposepdfcloud.PdfApiService, document_name string, annotation_id string, remote_folder string) {
// Get annotation by Id in the PDF document.
UploadFile(pdf_api, document_name)
args := map[string]interface{}{
"folder": remote_folder,
}
result, httpResponse, err := pdf_api.GetTextAnnotation(document_name, annotation_id, args)
if err != nil {
fmt.Println(err.Error())
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
fmt.Println("RequestAnnotationById(): Failed to request text annotation from the document page.")
} else {
fmt.Println("RequestAnnotationById(): annotation '" + annotation_id + "' successfully found '" + result.Annotation.Contents + "' in the document '" + document_name + "'.")
}
}
32 changes: 32 additions & 0 deletions uses_cases/annotations/get_annotations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"fmt"

asposepdfcloud "github.com/aspose-pdf-cloud/aspose-pdf-cloud-go/v25"
)

func RequestAnnotations(pdf_api *asposepdfcloud.PdfApiService, document_name string, page_num int32, remote_folder string) string {
// Get annotations from the page in the PDF document.
UploadFile(pdf_api, document_name)
args := map[string]interface{}{
"folder": remote_folder,
}
annotation_result := ""
result, httpResponse, err := pdf_api.GetPageAnnotations(document_name, page_num, args)
if err != nil {
fmt.Println(err.Error())
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
fmt.Println("RequestAnnotations(): Failed to delete annotation from the document page.")
} else {
for _, annotation := range result.Annotations.List {
if annotation.AnnotationType == asposepdfcloud.AnnotationTypeText {
fmt.Println("RequestAnnotations(): annotation id=", annotation.Id, " with '"+annotation.Contents+"' content get from the document '"+document_name+"' on ", annotation.PageIndex, " page.")
annotation_result := annotation.Id
return annotation_result
}
}
}
fmt.Println("RequestAnnotations(): Failed to get annotation in the document.")
return annotation_result
}
44 changes: 44 additions & 0 deletions uses_cases/annotations/new_highlight_annotation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"fmt"

asposepdfcloud "github.com/aspose-pdf-cloud/aspose-pdf-cloud-go/v25"
)

func AppendHighlightAnnotation(pdf_api *asposepdfcloud.PdfApiService, document_name string, page_num int32, output_document string, prefix string, remote_folder string) {
// Append a new highlight text annotation to the PDF document.
UploadFile(pdf_api, document_name)
args := map[string]interface{}{
"folder": remote_folder,
}
new_annotation := asposepdfcloud.HighlightAnnotation{
Name: "Highlight_Text_Annotation",
Rect: &asposepdfcloud.Rectangle{LLX: 100, LLY: 350, URX: 450, URY: 400},
Flags: []asposepdfcloud.AnnotationFlags{asposepdfcloud.AnnotationFlagsDefault},
HorizontalAlignment: asposepdfcloud.HorizontalAlignmentLeft,
VerticalAlignment: asposepdfcloud.VerticalAlignmentTop,
RichText: NEW_HL_ANNOTATION_TEXT,
Subject: NEW_HL_ANNOTATION_SUBJECT,
Contents: NEW_HL_ANNOTATION_CONTENTS,
Title: NEW_HL_ANNOTATION_DESCRIPTION,
ZIndex: 1,
Color: &asposepdfcloud.Color{A: 0xFF, R: 0x00, G: 0xFF, B: 0x00},
QuadPoints: []asposepdfcloud.Point{
{X: 10, Y: 10},
{X: 20, Y: 10},
{X: 10, Y: 20},
{X: 10, Y: 10},
},
Modified: "03/27/2025 00:00:00.000 AM",
}
_, httpResponse, err := pdf_api.PostPageHighlightAnnotations(document_name, page_num, []asposepdfcloud.HighlightAnnotation{new_annotation}, args)
if err != nil {
fmt.Println(err.Error())
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
fmt.Println("AppendHighlightAnnotation(): Failed to append annotation to the document page.")
} else {
fmt.Println("AppendHighlightAnnotation(): annotation '" + NEW_HL_ANNOTATION_TEXT + "' added to the document '" + document_name + "'.")
DownloadFile(pdf_api, document_name, output_document, prefix)
}
}
46 changes: 46 additions & 0 deletions uses_cases/annotations/new_strikeout_annotation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"fmt"

asposepdfcloud "github.com/aspose-pdf-cloud/aspose-pdf-cloud-go/v25"
)

func AppendStrikeoutAnnotation(pdf_api *asposepdfcloud.PdfApiService, document_name string, page_num int32, output_document string, prefix string, remote_folder string) {
// Append a new strikeout text annotation to the PDF document.
UploadFile(pdf_api, document_name)
args := map[string]interface{}{
"folder": remote_folder,
}

new_annotation := asposepdfcloud.StrikeOutAnnotation{
Name: "Highlight_Text_Annotation",
Rect: &asposepdfcloud.Rectangle{LLX: 100, LLY: 350, URX: 450, URY: 400},
Flags: []asposepdfcloud.AnnotationFlags{asposepdfcloud.AnnotationFlagsDefault},
HorizontalAlignment: asposepdfcloud.HorizontalAlignmentLeft,
VerticalAlignment: asposepdfcloud.VerticalAlignmentTop,
RichText: NEW_SO_ANNOTATION_TEXT,
Subject: NEW_SO_ANNOTATION_SUBJECT,
Contents: NEW_SO_ANNOTATION_CONTENTS,
Title: NEW_SO_ANNOTATION_DESCRIPTION,
ZIndex: 1,
Color: &asposepdfcloud.Color{A: 0xFF, R: 0x00, G: 0xFF, B: 0x00},
QuadPoints: []asposepdfcloud.Point{
{X: 10, Y: 10},
{X: 20, Y: 10},
{X: 10, Y: 20},
{X: 10, Y: 10},
},
Modified: "03/27/2025 00:00:00.000 AM",
}

_, httpResponse, err := pdf_api.PostPageStrikeOutAnnotations(document_name, page_num, []asposepdfcloud.StrikeOutAnnotation{new_annotation}, args)
if err != nil {
fmt.Println(err.Error())
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
fmt.Println("AppendStrikeoutAnnotation(): Failed to append annotation to the document page.")
} else {
fmt.Println("AppendStrikeoutAnnotation(): annotation '" + NEW_SO_ANNOTATION_TEXT + "' added to the document '" + document_name + "'.")
DownloadFile(pdf_api, document_name, output_document, prefix)
}
}
47 changes: 47 additions & 0 deletions uses_cases/annotations/new_text_annotation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"fmt"

asposepdfcloud "github.com/aspose-pdf-cloud/aspose-pdf-cloud-go/v25"
)

func AppendTextAnnotation(pdf_api *asposepdfcloud.PdfApiService, document_name string, page_num int32, output_document string, prefix string, remote_folder string) {
// Append a new free text annotation to the PDF document.
UploadFile(pdf_api, document_name)
args := map[string]interface{}{
"folder": remote_folder,
}

text_style := asposepdfcloud.TextStyle{
FontSize: 20,
Font: "Arial",
ForegroundColor: &asposepdfcloud.Color{A: 0xFF, R: 0x00, G: 0xFF, B: 0x00},
BackgroundColor: &asposepdfcloud.Color{A: 0xFF, R: 0xFF, G: 0x00, B: 0x00},
}

new_annotation := asposepdfcloud.FreeTextAnnotation{
Rect: &asposepdfcloud.Rectangle{LLX: 100, LLY: 350, URX: 450, URY: 400},
TextStyle: &text_style,
Name: "Free Text Annotation",
Flags: []asposepdfcloud.AnnotationFlags{asposepdfcloud.AnnotationFlagsDefault},
HorizontalAlignment: asposepdfcloud.HorizontalAlignmentCenter,
Intent: asposepdfcloud.FreeTextIntentFreeTextTypeWriter,
RichText: NEW_FT_ANNOTATION_TEXT,
Subject: NEW_FT_ANNOTATION_SUBJECT,
Contents: NEW_FT_ANNOTATION_CONTENTS,
Title: NEW_FT_ANNOTATION_DESCRIPTION,
ZIndex: 1,
Justification: asposepdfcloud.JustificationCenter,
}

_, httpResponse, err := pdf_api.PostPageFreeTextAnnotations(document_name, page_num, []asposepdfcloud.FreeTextAnnotation{new_annotation}, args)
if err != nil {
fmt.Println(err.Error())
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
fmt.Println("AppendTextAnnotation(): Failed to append annotation to the document page.")
} else {
fmt.Println("AppendTextAnnotation(): annotation '" + NEW_FT_ANNOTATION_TEXT + "' added to the document '" + document_name + "'.")
DownloadFile(pdf_api, document_name, output_document, prefix)
}
}
Loading