Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Image tiles all over the page #24

Merged
merged 5 commits into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -87,7 +97,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 @@ -101,7 +111,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 @@ -10,8 +10,8 @@ import (
pdf "github.com/unidoc/unidoc/pdf/model"
)

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 @@ -36,6 +36,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 @@ -22,14 +22,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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for some reason, the tiles in paragraph isn't working fine. Generates error after couple of iterations:

[DEBUG] contentstream.go:94 ERROR: Name too long (Font100000000...
@ajaxray can you check if you can make it work? Can be targeted outside the scope of this PR.

//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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have written as an interface for it to work with both paragraph and image

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))
}
}
}
}