Overlay

Text Watermark

Open an existing PDF and add a translucent "DRAFT" watermark.

doc, _ := gpdf.Open(pdfBytes)

doc.Overlay(0, func(p *template.PageBuilder) {
    p.Absolute(document.Mm(40), document.Mm(120), func(c *template.ColBuilder) {
        c.Text("DRAFT",
            template.FontSize(72),
            template.TextColor(pdf.Color{R: 0.9, G: 0.9, B: 0.9, A: 1, Space: pdf.ColorSpaceRGB}),
        )
    })
})

result, _ := doc.Save()

Page Numbers

Add page numbers to every page using EachPage.

doc, _ := gpdf.Open(pdfBytes)
count, _ := doc.PageCount()

doc.EachPage(func(i int, p *template.PageBuilder) {
    p.Absolute(document.Mm(170), document.Mm(285), func(c *template.ColBuilder) {
        c.Text(fmt.Sprintf("%d / %d", i+1, count),
            template.FontSize(10),
            template.AlignRight(),
        )
    }, template.AbsoluteWidth(document.Mm(20)))
})

result, _ := doc.Save()

Approval Stamp

Add an "APPROVED" stamp with a date at the top-right corner.

doc, _ := gpdf.Open(pdfBytes)

doc.Overlay(0, func(p *template.PageBuilder) {
    p.Absolute(document.Mm(130), document.Mm(15), func(c *template.ColBuilder) {
        c.Text("APPROVED",
            template.FontSize(28),
            template.Bold(),
            template.TextColor(pdf.RGB(0.8, 0, 0)),
        )
    })

    p.Absolute(document.Mm(130), document.Mm(25), func(c *template.ColBuilder) {
        c.Text("2026-03-05",
            template.FontSize(10),
            template.TextColor(pdf.RGB(0.5, 0.5, 0.5)),
        )
    })
})

result, _ := doc.Save()

Confidential Header

Add a red "CONFIDENTIAL" banner on every page using absolute positioning with page origin.

doc, _ := gpdf.Open(pdfBytes)

doc.EachPage(func(_ int, p *template.PageBuilder) {
    // Red background bar at the top
    p.Absolute(document.Mm(0), document.Mm(0), func(c *template.ColBuilder) {
        c.Line(
            template.LineColor(pdf.RGB(0.8, 0, 0)),
            template.LineThickness(document.Mm(8)),
        )
    }, template.AbsoluteOriginPage(), template.AbsoluteWidth(document.Mm(210)))

    // White text on the red bar
    p.Absolute(document.Mm(70), document.Mm(1), func(c *template.ColBuilder) {
        c.Text("CONFIDENTIAL",
            template.FontSize(14),
            template.Bold(),
            template.TextColor(pdf.RGB(1, 1, 1)),
        )
    }, template.AbsoluteOriginPage())
})

result, _ := doc.Save()

Multiple Overlays

Add different content to different pages using the gpdf.Open facade.

doc, _ := gpdf.Open(pdfBytes)

// Watermark on page 1
doc.Overlay(0, func(p *template.PageBuilder) {
    p.Absolute(document.Mm(50), document.Mm(140), func(c *template.ColBuilder) {
        c.Text("SAMPLE",
            template.FontSize(60),
            template.TextColor(pdf.Color{R: 0.85, G: 0.85, B: 0.85, A: 1, Space: pdf.ColorSpaceRGB}),
        )
    })
})

// Footer on page 2
doc.Overlay(1, func(p *template.PageBuilder) {
    p.Absolute(document.Mm(20), document.Mm(280), func(c *template.ColBuilder) {
        c.Text("Generated by gpdf",
            template.FontSize(8),
            template.TextColor(pdf.Gray(0.5)),
        )
    })
})

result, _ := doc.Save()