Anyone know how to add watermark to PDF programmatically?

I need to stamp a draft watermark on a batch of PDF reports automatically instead of doing each file by hand. My script keeps placing the text behind the page images. Anyone got this working?

Yeah, that is annoying, and fwiw it is usually just draw order. Your watermark is getting added before the existing page content, so anything opaque on the page covers it. Look for an overlay or foreground option instead of background or underlay, or append your stamp after the page’s current content stream. I would test on one copy first and check it in a normal viewer.

Check if your script is treating the watermark as a background layer. That always sits behind images. I use pdftk stamp so the draft text goes on top, then loop the folder. fwiw that has worked on my lease batches.

Had the same headache stamping DRAFT on our grant reports, and the fix was drawing the watermark text last, after the page content renders, since whatever gets drawn later sits on top. What language or library is your script using?

On my old laptop I batch-stamped a draft watermark with a small Python script, since stamping by hand is slow. The fix is to call merge on the watermark, not the page, so the text draws last. If your library has an over or append option, use it. I would make the draft a one page PDF with a transparent background first, so the images still show.

I ran into this with scanned reports too; the watermark needs to be merged onto the existing page as an overlay, not used as the background layer. With Python, I usually create a transparent watermark page in ReportLab, then merge it with pypdf using page.merge_page(watermark_page) after loading the original page. I also rotate it slightly and lower the fill opacity, then save to a separate output folder so the originals stay untouched.

Check if your script has a foreground or layer setting so it stamps over the image. Allways keep a backup of the original files before you run a batch!

I have seen the same problem on our construction reports. The watermark only stays visable if it is placed after the page images rather than before them.

@catherine88 That matches what I see too, it really is just draw order. If your script has an overlay or append option, use that, and always test one file and keep the originals.

Your script needs to append the watermark as a foreground content stream after the existing page content, not prepend it. I use PDFcap’s Watermark tool for quick batches, set “DRAFT” with low opacity, then apply it to all pages. Tip: test one image-heavy report first, and avoid flattening the PDF before adding the stamp.

That lines up with what I’ve seen too, draw order is almost always the culprit. @killerSteel99 your pypdf merge_page approach is basically what I do now, build the watermark as its own transparent page in ReportLab then merge onto each original page instead of the other way around. Saved me a bunch of headaches on batch jobs once I stopped treating it as a background layer.

@bella_wellington Software loves burying layers where nobody can see them. Try setting the stamp function to inject directly into the foreground stream so full-page scans do not hide it. Just avoid running any batch job on your original files until you verify the output in a basic viewer.

sounds like you’re stamping it as an underlay. most libs have an overlay flag, flip that on so it draws on top of the page content. i use pymupdf for batch stuff, insert_text with overlay=true works fine even on scanned files.