Skip to content

Commit

Permalink
Fix #469
Browse files Browse the repository at this point in the history
  • Loading branch information
hhrutter committed Jul 7, 2022
1 parent b9818b8 commit d8d83be
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
26 changes: 26 additions & 0 deletions pkg/api/test/stamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,3 +548,29 @@ func TestStampingLifecyle(t *testing.T) {
t.Fatalf("Watermarks found: %s\n", outFile)
}
}

func TestRecycleWM(t *testing.T) {
msg := "TestRecycleWM"
inFile := filepath.Join(inDir, "Test.pdf")
outFile := filepath.Join("..", "..", "samples", "watermark", "text", "TextRecycled.pdf")
onTop := false // we are testing watermarks

desc := "pos:tl, points:22, rot:0, scale:1 abs, off:0 -5, opacity:0.3"
wm, err := api.TextWatermark("This is a watermark", desc, onTop, false, pdfcpu.POINTS)
if err != nil {
t.Fatalf("%s %s: %v\n", msg, outFile, err)
}

if err = api.AddWatermarksFile(inFile, outFile, nil, wm, nil); err != nil {
t.Fatalf("%s %s: %v\n", msg, outFile, err)
}

wm.Recycle()

// Shift down watermark.
wm.Dy = -55

if err = api.AddWatermarksFile(outFile, outFile, nil, wm, nil); err != nil {
t.Fatalf("%s %s: %v\n", msg, outFile, err)
}
}
16 changes: 5 additions & 11 deletions pkg/pdfcpu/stamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ func DefaultWatermarkConfig() *Watermark {
}
}

func (wm *Watermark) Recycle() {
wm.objs = IntSet{}
wm.fCache = formCache{}
}

func (wm Watermark) typ() string {
if wm.isImage() {
return "image"
Expand Down Expand Up @@ -324,17 +329,6 @@ func (wm Watermark) multiStamp() bool {
return wm.Page == 0
}

// func (wm Watermark) calcMaxTextWidth() float64 {
// var maxWidth float64
// for _, l := range wm.TextLines {
// w := font.TextWidth(l, wm.FontName, wm.ScaledFontSize)
// if w > maxWidth {
// maxWidth = w
// }
// }
// return maxWidth
// }

func ResolveWMTextString(text, timeStampFormat string, pageNr, pageCount int) (string, bool) {
// replace %p with pageNr
// %P with pageCount
Expand Down
Binary file added pkg/samples/watermark/text/TextRecycled.pdf
Binary file not shown.

0 comments on commit d8d83be

Please sign in to comment.