Skip to content

Commit

Permalink
Merge branch 'image_tiles' of github.com:AnkurGel/markpdf into AnkurG…
Browse files Browse the repository at this point in the history
…el-image_tiles
  • Loading branch information
ajaxray committed Dec 25, 2022
2 parents 336e2c5 + ae65f31 commit f79d0da
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 8 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,17 @@ markpdf "path/to/source.pdf" "img/logo.png" "path/to/output.pdf" -Wo 0.3
# stretch full with of page at page bottom
markpdf "path/to/source.pdf" "img/logo.png" "path/to/output.pdf" --scale-width --offset-y=-10
markpdf "path/to/source.pdf" "img/logo.png" "path/to/output.pdf" -wy -10
```

# Scale the image to desired percentage
markpdf "path/to/source.pdf" "img/logo.png" "path/to/output.pdf" --scale=30

# Add image as tiles all over the page
markpdf "path/to/source.pdf" "img/logo.png" "path/to/output.pdf" --tiles

# Add image as tiles with interleaved spacing
markpdf "path/to/source.pdf" "img/logo.png" "path/to/output.pdf" --tiles --spacing=20
``


### Text watermarking

Expand Down Expand Up @@ -100,7 +110,7 @@ Currently the following font names are supported:
- **Specifying Colors**: write them as 6 or 3 digit hexadecilal as used in CSS, without the #

- `--color`, `--font` and `--font-size` flag has no impact for Image watermarking
- `--scale-*` and `--opacity` flag has no impact for Text watermarking
- `--scale-*`, `--tiles` and `--opacity` flag has no impact for Text watermarking
- Negative offset will set content positioning from opposite side (right for offsetX and botom from offsetY)
- Text with opacity is not supported at this moment. Instead, you can [create a transperent background PNG image](http://www.picturetopeople.org/text_generator/others/transparent/transparent-text-generator.html) with your text and then use it for watermarking.

Expand All @@ -114,7 +124,7 @@ Currently the following font names are supported:
✅ Configure image rotation angle
✅ Options to Stretch watermark to page width or height, proportionately
✅ Options to Stretch watermark to page width or height at the middle of page
◻️ Tile Image all over the page
Tile Image all over the page
✅ Render text on every page
✅ Configure text color, style and font
◻️ Configure text opacity
Expand Down
12 changes: 10 additions & 2 deletions img_watermark.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import (
)

func drawImage(watermarkImg *creator.Image, c *creator.Creator) {
watermarkImg.SetPos(offsetX, offsetY)
watermarkImg.SetOpacity(opacity)
watermarkImg.SetAngle(angle)
if tiles {
repeatTiles(watermarkImg, c)
return
}
watermarkImg.SetPos(offsetX, offsetY)
_ = c.Draw(watermarkImg)
}

Expand All @@ -20,9 +24,13 @@ func adjustImagePosition(watermarkImg *creator.Image, c *creator.Creator) {


if scaleImage != 100 {
debugInfo(fmt.Sprintf("Scaling to %v", scaleImage))
debugInfo(fmt.Sprintf("Scaling to %v%%", scaleImage))
watermarkImg.ScaleToHeight(scaleImage * watermarkImg.Width() / 100)
}
if tiles {
offsetX, offsetY = 0, 0
return
}
if scaleWCenter {
watermarkImg.ScaleToWidth(c.Context().PageWidth)
offsetX = 0
Expand Down
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"text/template"
)

var offsetX, offsetY, scaleImage, fontSize float64
var scaleH, scaleW, scaleHCenter, scaleWCenter, center, verbose, version bool
var offsetX, offsetY, scaleImage, fontSize, spacing float64
var scaleH, scaleW, scaleHCenter, scaleWCenter, center, tiles, verbose, version bool
var opacity, angle float64
var font, color string

Expand All @@ -37,6 +37,9 @@ func init() {
flag.Float64VarP(&opacity, "opacity", "o", 0.5, "Opacity of watermark. float between 0 to 1.")
flag.Float64VarP(&angle, "angle", "a", 0, "Angle of rotation. between 0 to 360, counter clock-wise.")

flag.BoolVarP(&tiles, "tiles", "t", false, "Repeat watermark as tiles on page. All offsets will be ignored.")
flag.Float64VarP(&spacing, "spacing", "z", 0, "Repeat watermark as tiles on page. All offsets will be ignored.")

flag.BoolVarP(&verbose, "verbose", "v", false, "Display debug information.")
flag.BoolVarP(&version, "version", "V", false, "Display Version information.")

Expand Down
12 changes: 11 additions & 1 deletion text_watermark.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,24 @@ func drawText(p *creator.Paragraph, c *creator.Creator) {
p.SetColor(creator.ColorRGBFromHex("#" + color))
p.SetAngle(angle)

// Encountering problem with tiles and text watermark. Contributions welcome.
//if tiles {
// repeatTiles(p, c)
// return
//}

_ = c.Draw(p)
}

func adjustTextPosition(p *creator.Paragraph, c *creator.Creator) {
p.SetTextAlignment(creator.TextAlignmentLeft)
p.SetEnableWrap(false)

if center {
if tiles {
p.SetWidth(p.Width()) // Not working without setting it manually
p.SetTextAlignment(creator.TextAlignmentCenter)
offsetX, offsetY = 0, 0
} else if center {
p.SetWidth(p.Width()) // Not working without setting it manually
p.SetTextAlignment(creator.TextAlignmentCenter)

Expand Down
36 changes: 36 additions & 0 deletions tiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"
"github.com/unidoc/unidoc/pdf/creator"
"math"
)

// Watermarkable is a common interface for watermarkable image or paragraph
type Watermarkable interface {
creator.VectorDrawable
SetPos(x, y float64)
}

func repeatTiles(watermark Watermarkable, c *creator.Creator) {
w := watermark.Width()
h := watermark.Height()
pw := c.Context().PageWidth
ph := c.Context().PageHeight

nw := math.Ceil(pw / w)
nh := math.Ceil(ph / h)

debugInfo(fmt.Sprintf("Settings tiles of %v x %v", nw, nh))
for i := 0; i < int(nw); i++ {
x := w * float64(i)
for j := 0; j < int(nh); j++ {
y := h * float64(j)
watermark.SetPos(x + spacing * float64(i), y + spacing * float64(j))
err := c.Draw(watermark)
if err != nil {
fatalIfError(err, fmt.Sprintf("Error %s", err))
}
}
}
}

0 comments on commit f79d0da

Please sign in to comment.