Skip to content

Commit d6d726f

Browse files
authored
[US-800] Watermark examples. (#264)
* Watermark examples. * Simplified pdf_watermark_image.go * Updated unipdf version. * updated README.md
1 parent cd8a28a commit d6d726f

File tree

5 files changed

+115
-23
lines changed

5 files changed

+115
-23
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/unidoc/globalsign-dss v0.0.0-20220330092912-b69d85b63736
1414
github.com/unidoc/pkcs7 v0.2.0
1515
github.com/unidoc/unichart v0.3.0
16-
github.com/unidoc/unipdf/v3 v3.62.0
16+
github.com/unidoc/unipdf/v3 v3.65.0
1717
golang.org/x/crypto v0.31.0
1818
golang.org/x/image v0.18.0
1919
golang.org/x/text v0.21.0
@@ -50,7 +50,7 @@ require (
5050
github.com/unidoc/timestamp v0.0.0-20200412005513-91597fd3793a // indirect
5151
github.com/unidoc/unitype v0.4.0 // indirect
5252
go.opencensus.io v0.24.0 // indirect
53-
golang.org/x/net v0.24.0 // indirect
53+
golang.org/x/net v0.33.0 // indirect
5454
golang.org/x/oauth2 v0.7.0 // indirect
5555
golang.org/x/sys v0.28.0 // indirect
5656
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ github.com/unidoc/timestamp v0.0.0-20200412005513-91597fd3793a h1:RLtvUhe4DsUDl6
128128
github.com/unidoc/timestamp v0.0.0-20200412005513-91597fd3793a/go.mod h1:j+qMWZVpZFTvDey3zxUkSgPJZEX33tDgU/QIA0IzCUw=
129129
github.com/unidoc/unichart v0.3.0 h1:VX1j5yzhjrR3f2flC03Yat6/WF3h7Z+DLEvJLoTGhoc=
130130
github.com/unidoc/unichart v0.3.0/go.mod h1:8JnLNKSOl8yQt1jXewNgYFHhFm5M6/ZiaydncFDpakA=
131-
github.com/unidoc/unipdf/v3 v3.62.0 h1:CVsxq6k1SSIrprotlFvq6iBhA+5745dWaApB0LKtGcc=
132-
github.com/unidoc/unipdf/v3 v3.62.0/go.mod h1:0OIzSHHno23Y8WzaK+852abK8d3AxUZ1GQkMqpyCzu8=
131+
github.com/unidoc/unipdf/v3 v3.65.0 h1:ye3PP9JuUJnQd4BqRR4wJO/EN9EpHRGusMOruDjCS74=
132+
github.com/unidoc/unipdf/v3 v3.65.0/go.mod h1:tTbloOTKtGGi6z5doJshesDpQYktePhR+7r+WGCkooU=
133133
github.com/unidoc/unitype v0.4.0 h1:/TMZ3wgwfWWX64mU5x2O9no9UmoBqYCB089LYYqHyQQ=
134134
github.com/unidoc/unitype v0.4.0/go.mod h1:HV5zuUeqMKA4QgYQq3KDlJY/P96XF90BQB+6czK6LVA=
135135
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
@@ -153,8 +153,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
153153
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
154154
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
155155
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
156-
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
157-
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
156+
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
157+
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
158158
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
159159
golang.org/x/oauth2 v0.7.0 h1:qe6s0zUXlPX80/dITx3440hWZ7GwMwgDDyrSGTPJG/g=
160160
golang.org/x/oauth2 v0.7.0/go.mod h1:hPLQkd9LyjfXTiRohC/41GhcFqxisoUQ99sCUOHO9x4=

image/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ Having the ability to play around with images allows for the creation of attract
99
- [pdf_images_to_pdf.go](pdf_images_to_pdf.go) explains how to add multiple images in a PDF document, one image per page.
1010
- [pdf_list_images.go](pdf_list_images.go) explains how to list images in a PDF file. Passes through each page, goes through the content stream and finds instances of both. XObject Images and inline images. Also handles images referred within XObject Form content streams.
1111
- [pdf_watermark_image.go](pdf_watermark_image.go) explains how to add watermark image to each page of a PDF file.
12+
- [pdf_remove_watermark_image.go](pdf_remove_watermark_image.go) explains how to remove watermark image from a PDF file.
1213
- Image extraction example can be found in [../extract](extract) example subfolder.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Remove watermark image to each page of a PDF file.
3+
*
4+
* Run as: go run pdf_remove_watermark_image.go input.pdf output.pdf
5+
*/
6+
7+
package main
8+
9+
import (
10+
"fmt"
11+
"os"
12+
13+
"github.com/unidoc/unipdf/v3/common/license"
14+
"github.com/unidoc/unipdf/v3/model"
15+
)
16+
17+
func init() {
18+
// Make sure to load your metered License API key prior to using the library.
19+
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
20+
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
21+
if err != nil {
22+
panic(err)
23+
}
24+
}
25+
26+
func main() {
27+
if len(os.Args) < 3 {
28+
fmt.Printf("go run pdf_remove_watermark_image.go input.pdf output.pdf\n")
29+
os.Exit(1)
30+
}
31+
32+
inputPath := os.Args[1]
33+
outputPath := os.Args[2]
34+
35+
pdfReader, f, err := model.NewPdfReaderFromFile(inputPath, nil)
36+
if err != nil {
37+
fmt.Printf("Error: %v\n", err)
38+
os.Exit(1)
39+
}
40+
defer f.Close()
41+
42+
numPages, err := pdfReader.GetNumPages()
43+
if err != nil {
44+
fmt.Printf("Error: %v\n", err)
45+
os.Exit(1)
46+
}
47+
48+
for i := 0; i < numPages; i++ {
49+
pageNum := i + 1
50+
51+
// Read the page.
52+
page, err := pdfReader.GetPage(pageNum)
53+
if err != nil {
54+
fmt.Printf("Error: %v\n", err)
55+
os.Exit(1)
56+
}
57+
58+
// For watermarks applied by Adobe, just pass nil as argument.
59+
// For custom watermarks use pdf_list_images.go to figure out the name of watermark object.
60+
err = page.RemoveWatermarkImage("Img1")
61+
if err != nil {
62+
fmt.Printf("Error: %v\n", err)
63+
os.Exit(1)
64+
}
65+
}
66+
67+
// Generate a PDFWriter from PDFReader.
68+
pdfWriter, err := pdfReader.ToWriter(nil)
69+
if err != nil {
70+
fmt.Printf("Error: %v\n", err)
71+
os.Exit(1)
72+
}
73+
74+
err = pdfWriter.WriteToFile(outputPath)
75+
if err != nil {
76+
fmt.Printf("Error: %v\n", err)
77+
os.Exit(1)
78+
}
79+
80+
fmt.Printf("Complete, see output file: %s\n", outputPath)
81+
}

image/pdf_watermark_image.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ package main
88

99
import (
1010
"fmt"
11+
"image"
1112
"os"
1213

1314
"github.com/unidoc/unipdf/v3/common"
1415
"github.com/unidoc/unipdf/v3/common/license"
15-
"github.com/unidoc/unipdf/v3/creator"
1616
"github.com/unidoc/unipdf/v3/model"
1717
)
1818

@@ -49,13 +49,25 @@ func addWatermarkImage(inputPath string, outputPath string, watermarkPath string
4949
common.Log.Debug("Input PDF: %v", inputPath)
5050
common.Log.Debug("Watermark image: %s", watermarkPath)
5151

52-
c := creator.New()
52+
// Create the watermark image from file.
53+
wImgFile, err := os.Open(watermarkPath)
54+
if err != nil {
55+
return err
56+
}
57+
defer wImgFile.Close()
58+
59+
watermarkImg, _, err := image.Decode(wImgFile)
60+
if err != nil {
61+
return err
62+
}
5363

54-
watermarkImg, err := c.NewImageFromFile(watermarkPath)
64+
image, err := model.DefaultImageHandler{}.NewImageFromGoImage(watermarkImg)
5565
if err != nil {
5666
return err
5767
}
5868

69+
xImage, err := model.NewXObjectImageFromImage(image, nil, nil)
70+
5971
// Read the input pdf file.
6072
f, err := os.Open(inputPath)
6173
if err != nil {
@@ -82,21 +94,19 @@ func addWatermarkImage(inputPath string, outputPath string, watermarkPath string
8294
return err
8395
}
8496

85-
// Add to creator.
86-
c.AddPage(page)
87-
88-
watermarkImg.ScaleToWidth(c.Context().PageWidth)
89-
watermarkImg.SetPos(0, (c.Context().PageHeight-watermarkImg.Height())/2)
90-
watermarkImg.SetOpacity(0.5)
91-
_ = c.Draw(watermarkImg)
97+
// Add watermark with options.
98+
page.AddWatermarkImage(xImage, model.WatermarkImageOptions{Alpha: 0.5, FitToWidth: true})
9299
}
93100

94-
// Add reader outline tree to the creator.
95-
c.SetOutlineTree(pdfReader.GetOutlineTree())
96-
97-
// Add reader AcroForm to the creator.
98-
c.SetForms(pdfReader.AcroForm)
101+
// Generate a PDFWriter from PDFReader.
102+
pdfWriter, err := pdfReader.ToWriter(nil)
103+
if err != nil {
104+
return err
105+
}
99106

100-
err = c.WriteToFile(outputPath)
101-
return err
107+
err = pdfWriter.WriteToFile(outputPath)
108+
if err != nil {
109+
return err
110+
}
111+
return nil
102112
}

0 commit comments

Comments
 (0)