-
Notifications
You must be signed in to change notification settings - Fork 208
/
watermark.go
191 lines (180 loc) · 7.09 KB
/
watermark.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"fmt"
"os"
)
func (a *App) WatermarkPDFByText(inFile string, outFile string, markText string, fontFamily string, fontSize int, fontColor string, angle int, opacity float32, num_lines int, line_spacing float32, word_spacing float32, x_offset float32, y_offset float32, multiple_mode bool, layer string, pages string) error {
logger.Printf("inFile: %s, outFile: %s, markText: %s, fontFamily: %s, fontSize: %d, fontColor: %s, angle: %d, opacity: %f, num_lines: %d, line_spacing: %f, word_spacing: %f, x_offset: %f, y_offset: %f, multiple_mode: %v, layer: %s\n", inFile, outFile, markText, fontFamily, fontSize, fontColor, angle, opacity, num_lines, line_spacing, word_spacing, x_offset, y_offset, multiple_mode, layer)
if _, err := os.Stat(inFile); os.IsNotExist(err) {
logger.Errorln(err)
return err
}
args := []string{"watermark", "add"}
if markText != "" {
args = append(args, "--mark-text", markText)
}
if fontFamily != "" {
args = append(args, "--font-family", fontFamily)
}
if fontColor != "" {
args = append(args, "--color", fontColor)
}
args = append(args, "--font-size", fmt.Sprintf("%d", fontSize))
args = append(args, "--angle", fmt.Sprintf("%d", angle))
args = append(args, "--opacity", fmt.Sprintf("%f", opacity))
args = append(args, "--num-lines", fmt.Sprintf("%d", num_lines))
args = append(args, "--line-spacing", fmt.Sprintf("%f", line_spacing))
args = append(args, "--word-spacing", fmt.Sprintf("%f", word_spacing))
args = append(args, "--x-offset", fmt.Sprintf("%f", x_offset))
args = append(args, "--y-offset", fmt.Sprintf("%f", y_offset))
if multiple_mode {
args = append(args, "--multiple-mode")
}
if layer != "" {
args = append(args, "--layer", layer)
}
if pages != "" {
args = append(args, "--page_range", pages)
}
if outFile != "" {
args = append(args, "-o", outFile)
}
args = append(args, inFile)
logger.Println(args)
return a.cmdRunner(args, "pdf")
}
func (a *App) WatermarkPDFByImage(inFile string, outFile string, wmPath string, angle int, opacity float32, scale float32, num_lines int, line_spacing float32, word_spacing float32, x_offset float32, y_offset float32, multiple_mode bool, layer string, pages string) error {
logger.Printf("inFile: %s, outFile: %s, wmPath: %s, angle: %d, opacity: %f, scale: %f, num_lines: %d, line_spacing: %f, word_spacing: %f, x_offset: %f, y_offset: %f, multiple_mode: %v, layer: %s\n", inFile, outFile, wmPath, angle, opacity, scale, num_lines, line_spacing, word_spacing, x_offset, y_offset, multiple_mode, layer)
if _, err := os.Stat(inFile); os.IsNotExist(err) {
logger.Errorln(err)
return err
}
args := []string{"watermark", "add", "--type", "image"}
if wmPath != "" {
args = append(args, "--wm-path", wmPath)
}
args = append(args, "--angle", fmt.Sprintf("%d", angle))
args = append(args, "--opacity", fmt.Sprintf("%f", opacity))
args = append(args, "--scale", fmt.Sprintf("%f", scale))
args = append(args, "--num-lines", fmt.Sprintf("%d", num_lines))
args = append(args, "--line-spacing", fmt.Sprintf("%f", line_spacing))
args = append(args, "--word-spacing", fmt.Sprintf("%f", word_spacing))
args = append(args, "--x-offset", fmt.Sprintf("%f", x_offset))
args = append(args, "--y-offset", fmt.Sprintf("%f", y_offset))
if multiple_mode {
args = append(args, "--multiple-mode")
}
if layer != "" {
args = append(args, "--layer", layer)
}
if pages != "" {
args = append(args, "--page_range", pages)
}
if outFile != "" {
args = append(args, "-o", outFile)
}
args = append(args, inFile)
logger.Println(args)
return a.cmdRunner(args, "pdf")
}
func (a *App) WatermarkPDFByPDF(inFile string, outFile string, wmPath string, layer string, pages string) error {
logger.Printf("inFile: %s, outFile: %s, wmPath: %s, layer: %s\n", inFile, outFile, wmPath, layer)
if _, err := os.Stat(inFile); os.IsNotExist(err) {
logger.Errorln(err)
return err
}
args := []string{"watermark", "add", "--type", "pdf"}
if wmPath != "" {
args = append(args, "--wm-path", wmPath)
}
if layer != "" {
args = append(args, "--layer", layer)
}
if pages != "" {
args = append(args, "--page_range", pages)
}
if outFile != "" {
args = append(args, "-o", outFile)
}
args = append(args, inFile)
logger.Println(args)
return a.cmdRunner(args, "pdf")
}
func (a *App) RemoveWatermark(inFile string, outFile string, method string, wm_index []int, wm_text string, pages string) error {
logger.Printf("inFile: %s, outFile: %s, method: %s, wm_index: %d, wm_text: %s, pages: %s\n", inFile, outFile, method, wm_index, wm_text, pages)
args := []string{"watermark", "remove", "--method", method}
if len(wm_index) > 0 {
args = append(args, "--wm_index")
for _, v := range wm_index {
args = append(args, fmt.Sprintf("%d", v))
}
}
if wm_text != "" {
args = append(args, "--wm_text", wm_text)
}
if pages != "" {
args = append(args, "--page_range", pages)
}
if outFile != "" {
args = append(args, "-o", outFile)
}
args = append(args, inFile)
logger.Println(args)
return a.cmdRunner(args, "pdf")
}
func (a *App) DetectWatermarkByIndex(inFile string, outFile string, wmIndex int) error {
logger.Printf("inFile: %s, outFile: %s, wmIndex: %d\n", inFile, outFile, wmIndex)
args := []string{"watermark", "detect"}
args = append(args, inFile)
args = append(args, "--wm_index")
args = append(args, fmt.Sprintf("%d", wmIndex))
if outFile != "" {
args = append(args, "-o", outFile)
}
logger.Println(args)
return a.cmdRunner(args, "pdf")
}
func (a *App) MaskPDFByRect(inFile string, outFile string, rect []float32, unit string, color string, opacity float32, angle float32, pages string) error {
logger.Printf("inFile: %s, outFile: %s, rect: %v, unit: %s, color: %s, opacity: %f, angle: %f, pages: %s\n", inFile, outFile, rect, unit, color, opacity, angle, pages)
args := []string{"mask", "--type", "rect"}
args = append(args, "--bbox")
for _, v := range rect {
args = append(args, fmt.Sprintf("%f", v))
}
if unit != "" {
args = append(args, "--unit", unit)
}
if color != "" {
args = append(args, "--color", color)
}
args = append(args, "--opacity", fmt.Sprintf("%f", opacity))
args = append(args, "--angle", fmt.Sprintf("%f", angle))
if pages != "" {
args = append(args, "--page_range", pages)
}
if outFile != "" {
args = append(args, "-o", outFile)
}
args = append(args, inFile)
logger.Println(args)
return a.cmdRunner(args, "pdf")
}
func (a *App) MaskPDFByAnnot(inFile string, outFile string, annot_page int, color string, opacity float32, angle float32, pages string) error {
logger.Printf("inFile: %s, outFile: %s, annot_page: %d, color: %s, opacity: %f, angle: %f, pages: %s\n", inFile, outFile, annot_page, color, opacity, angle, pages)
args := []string{"mask", "--type", "annot"}
args = append(args, "--annot-page", fmt.Sprintf("%d", annot_page))
if color != "" {
args = append(args, "--color", color)
}
args = append(args, "--opacity", fmt.Sprintf("%f", opacity))
args = append(args, "--angle", fmt.Sprintf("%f", angle))
if pages != "" {
args = append(args, "--page_range", pages)
}
if outFile != "" {
args = append(args, "-o", outFile)
}
args = append(args, inFile)
logger.Println(args)
return a.cmdRunner(args, "pdf")
}