github razvandimescu/gopdf v0.9.0
v0.9.0 — image watermark overlay

latest releases: v0.11.2, v0.11.1, v0.11.0...
6 months ago

What's new

Image overlay / watermark

Editor.AddImage places PNG or JPEG images on any page as PDF Image XObjects — with a grayscale SMask for alpha, an ExtGState for opacity, and a rotation/scale cm matrix. The same image is written once and shared by every page that references it.

logo, _ := pdf.LoadImage("logo.png")
ed := pdf.NewEditor(data)
for i := 0; i < doc.NumPages(); i++ {
    mb := doc.Page(i).MediaBox()
    ed.AddImage(pdf.ImageOverlay{
        Page:     i,
        Image:    logo,
        CX:       (mb[0] + mb[2]) / 2,
        CY:       (mb[1] + mb[3]) / 2,
        Width:    400, Height: 140,
        Rotation: 45,
        Opacity:  0.15,
    })
}
result, _ := ed.Apply()

cmd/watermark CLI

One-shot diagonal watermarking for the common case:

go run ./cmd/watermark -i in.pdf -img logo.png -o out.pdf \
    -angle 45 -opacity 0.15 -scale 0.85

Sizes the image so its rotated bounding box fits within both page dimensions at any angle (corrected during review — earlier draft only constrained the horizontal projection).

Editor.Document() accessor

Returns the parsed PDF, opened lazily on first call and shared across Apply / RedactText / external callers — eliminates the 2–3× re-parsing that previously occurred when callers needed page info before applying edits.

New API

  • pdf.Image — decoded raster, holding RGB + optional grayscale alpha
  • pdf.LoadImage(path) / pdf.LoadImageBytes(data) — PNG / JPEG decoders
  • pdf.ImageOverlay — page, image, center, size, rotation, opacity
  • Editor.AddImage(overlay) — register an overlay
  • Editor.Document() — shared parsed document

Full Changelog: v0.8.5...v0.9.0

Don't miss a new gopdf release

NewReleases is sending notifications on new releases.