Skip to content

Commit b1a2de3

Browse files
authored
Merge pull request #91 from aspose-pdf-cloud/pdfcloud-4971-added-snippets-watermarks
PDFCLOUD-4971: added snippets for Watermarks
2 parents bc0f3c8 + 95b0ba4 commit b1a2de3

File tree

5 files changed

+207
-0
lines changed

5 files changed

+207
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
asposepdfcloud "github.com/aspose-pdf-cloud/aspose-pdf-cloud-go/v25"
7+
)
8+
9+
// Extract images watermarks from document and show info to console
10+
func GetWatermarks(pdf_api *asposepdfcloud.PdfApiService, document string, remoteFolder string) {
11+
uploadFile(pdf_api, document)
12+
13+
args := map[string]interface{}{
14+
"folder": remoteFolder,
15+
}
16+
17+
result, httpResponse, err := pdf_api.GetPages(document, args)
18+
if err != nil {
19+
fmt.Println(err.Error())
20+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
21+
fmt.Println("GetPages() - Unexpected error!")
22+
} else {
23+
for pageNum := range result.Pages.List {
24+
imageResult, httpResponse, err := pdf_api.GetImages(document, int32(pageNum+1), args)
25+
if err != nil {
26+
fmt.Println(err.Error())
27+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
28+
fmt.Println("Unexpected error!")
29+
} else {
30+
for _, image := range imageResult.Images.List {
31+
fmt.Println("Page", pageNum+1, "Image Id=", image.Id, "LinkId:", image.Links[0].Href, "Width:", (image.Rectangle.LLY - image.Rectangle.LLX), "Height:", (image.Rectangle.URY - image.Rectangle.URX))
32+
}
33+
}
34+
}
35+
}
36+
}
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_pages.pdf"
17+
18+
PDF_DOCUMENT_WITH_WATERMARKS = "sample_watermarks.pdf"
19+
20+
PAGE_NUMBER = 2
21+
22+
IMAGE_FILE = "sample.png"
23+
IMAGE_OPACITY = 0.4
24+
IMAGE_ROTATE = 45
25+
IMAGE_X = 200
26+
IMAGE_Y = 400
27+
IMAGE_WIDTH = 64
28+
IMAGE_HEIGHT = 64
29+
30+
APP_SID = "************" // Your Application SID
31+
APP_KEY = "************" // Your Application Key
32+
)
33+
34+
var (
35+
IMAGE_IDS_DEL = []string{
36+
"GE5TKMBQGE4TWMRQGAWDIMBQFQZDSMJMGQ4TC",
37+
"GI5TKMBQGE4TWMRQGAWDIMBQFQZDSMJMGQ4TC",
38+
"GM5TKMBQGE4TWMRQGAWDIMBQFQZDSMJMGQ4TC",
39+
"GQ5TKMBQGE4TWMRQGAWDIMBQFQZDSMJMGQ4TC",
40+
"GU5TKMBQGE4TWMRQGAWDIMBQFQZDSMJMGQ4TC",
41+
"GY5TKMBQGE4TWMRQGAWDIMBQFQZDSMJMGQ4TC",
42+
"G45TKMBQGE4TWMRQGAWDIMBQFQZDSMJMGQ4TC",
43+
"HA5TKMBQGE4TWMRQGAWDIMBQFQZDSMJMGQ4TC",
44+
}
45+
)
46+
47+
func initPdfApi() *asposepdfcloud.PdfApiService {
48+
pdfApi := asposepdfcloud.NewPdfApiService(APP_SID, APP_KEY, "")
49+
return pdfApi
50+
}
51+
52+
// Upload local file to the remote folder with check errors
53+
func uploadFile(pdf_api *asposepdfcloud.PdfApiService, name string) {
54+
args := map[string]interface{}{
55+
"folder": REMOTE_FOLDER,
56+
}
57+
file, err := os.Open(filepath.Join(LOCAL_FOLDER, name))
58+
if err != nil {
59+
fmt.Println(err.Error())
60+
} else {
61+
_, httpResponse, err := pdf_api.UploadFile(path.Join(REMOTE_FOLDER, name), file, 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+
fmt.Println("File '" + name + " ' succeddfully uplouaded.")
68+
}
69+
}
70+
}
71+
72+
// Download file from remote folder and save it locally with check errors
73+
func downloadFile(pdf_api *asposepdfcloud.PdfApiService, name string, output_name string, prefix string) {
74+
args := map[string]interface{}{
75+
"folder": REMOTE_FOLDER,
76+
}
77+
result_data, httpResponse, err := pdf_api.DownloadFile(path.Join(REMOTE_FOLDER, name), args)
78+
if err != nil {
79+
fmt.Println(err.Error())
80+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
81+
fmt.Println("Unexpected error!")
82+
} else {
83+
fileName := path.Join(LOCAL_FOLDER, prefix+output_name)
84+
f, _ := os.Create(fileName)
85+
_, _ = f.Write(result_data)
86+
fmt.Println("File '" + prefix + fileName + "' successfully downloaded.")
87+
}
88+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
// Append image watermark to a document
11+
func AppendNewWatermark(pdf_api *asposepdfcloud.PdfApiService, document string, imageFileName string, opacity float64, rotate float64, xPos float64, yPos float64, width float64, height float64, outputDocument string, remoteFolder string) {
12+
uploadFile(pdf_api, document)
13+
uploadFile(pdf_api, imageFileName)
14+
15+
args := map[string]interface{}{
16+
"folder": remoteFolder,
17+
}
18+
19+
image := asposepdfcloud.ImageStamp{
20+
Background: true,
21+
Opacity: opacity,
22+
Rotate: asposepdfcloud.RotationNone,
23+
RotateAngle: rotate,
24+
XIndent: xPos,
25+
YIndent: yPos,
26+
Width: width,
27+
Height: height,
28+
Zoom: 1,
29+
FileName: path.Join(remoteFolder, imageFileName),
30+
}
31+
32+
_, httpResponse, err := pdf_api.PostDocumentImageStamps(document, []asposepdfcloud.ImageStamp{image}, args)
33+
if err != nil {
34+
fmt.Println(err.Error())
35+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
36+
fmt.Println("Unexpected error!")
37+
} else {
38+
fmt.Println("Successfully added image watermark. ")
39+
downloadFile(pdf_api, document, outputDocument, "add_watermark_")
40+
}
41+
}
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+
6+
asposepdfcloud "github.com/aspose-pdf-cloud/aspose-pdf-cloud-go/v25"
7+
)
8+
9+
// Delete images watermarks from document
10+
func DeleteImageWatermarks(pdf_api *asposepdfcloud.PdfApiService, document string, watermarkIds []string, outputDocument string, remoteFolder string) {
11+
uploadFile(pdf_api, document)
12+
13+
args := map[string]interface{}{
14+
"folder": remoteFolder,
15+
}
16+
17+
for _, watermarkId := range watermarkIds {
18+
_, httpResponse, err := pdf_api.DeleteImage(document, watermarkId, args)
19+
20+
if err != nil {
21+
fmt.Println(err.Error())
22+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
23+
fmt.Println("DeleteImageWatermarks()): Failed to delete image from the document.")
24+
} else {
25+
fmt.Println("DeleteImageWatermarks(): image '" + watermarkId + "' successfully deleted from the document '" + document + "'.")
26+
}
27+
}
28+
29+
downloadFile(pdf_api, document, outputDocument, "del_image_stamp_")
30+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
func main() {
4+
pdfApi := initPdfApi()
5+
6+
AppendNewWatermark(pdfApi, PDF_DOCUMENT, IMAGE_FILE, IMAGE_OPACITY, IMAGE_ROTATE, IMAGE_X, IMAGE_Y, IMAGE_WIDTH, IMAGE_HEIGHT, PDF_OUTPUT, REMOTE_FOLDER)
7+
8+
GetWatermarks(pdfApi, PDF_DOCUMENT_WITH_WATERMARKS, REMOTE_FOLDER)
9+
10+
DeleteImageWatermarks(pdfApi, PDF_DOCUMENT_WITH_WATERMARKS, IMAGE_IDS_DEL, PDF_OUTPUT, REMOTE_FOLDER)
11+
12+
}

0 commit comments

Comments
 (0)