Prompt
How do I watermark PDFs programmatically?
Latest observation
Oct 12, 2026
OpenAI APIWeb search: off
There are a few common ways to watermark PDFs programmatically, depending on your language and whether you want to add text, an image, or both.
Common approaches
1) Use a PDF library
Best if you want full control and reliable results.
Popular libraries:
- Python:
pypdf,reportlab - JavaScript/Node.js:
pdf-lib - Java: Apache PDFBox
- C#: iText, PdfSharp, IronPDF
- Go:
gofpdf,unipdf
2) Stamp an existing watermark onto each page
Typical flow:
- Create a transparent watermark page/image.
- Overlay it onto every page of the target PDF.
- Save the result.
3) Generate the PDF with the watermark already included
If you’re creating PDFs yourself, draw the watermark as part of the page rendering.
Example in Python using pypdf + reportlab
This is a common pattern for adding a text watermark:
from pypdf import PdfReader, PdfWriter
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from io import BytesIO
def create_watermark(text, page_size):
packet = BytesIO()
can = canvas.Canvas(packet, pagesize=page_size)
can.setFillAlpha(0.2) # transparency
can.setFont("Helvetica-Bold", 40)
can.saveState()
can.translate(page_size[0] / 2, page_size[1] / 2)
can.rotate(45)
can.drawCentredString(0, 0, text)
can.restoreState()
can.save()
packet.seek(0)
return PdfReader(packet).pages[0]
input_pdf = PdfReader("input.pdf")
output_pdf = PdfWriter()
for page in input_pdf.pages:
watermark = create_watermark("CONFIDENTIAL", (float(page.mediabox.width), float(page.mediabox.height)))
page.merge_page(watermark)
output_pdf.add_page(page)
with open("watermarked.pdf", "wb") as f:
output_pdf.write(f)
Example in Node.js using pdf-lib
const fs = require('fs');
const { PDFDocument, rgb, degrees } = require('pdf-lib');
async function addWatermark() {
const pdfBytes = fs.readFileSync('input.pdf');
const pdfDoc = await PDFDocument.load(pdfBytes);
const pages = pdfDoc.getPages();
for (const page of pages) {
const { width, height } = page.getSize();
page.drawText('CONFIDENTIAL', {
x: width / 2 - 100,
y: height / 2,
size: 40,
rotate: degrees(45),
color: rgb(0.7, 0.7, 0.7),
opacity: 0.2,
});
}
const outBytes = await pdfDoc.save();
fs.writeFileSync('watermarked.pdf', outBytes);
}
addWatermark();
If you want an image watermark
Use the same process, but draw an image instead of text.
Python libraries:
reportlabfor creating the watermark overlaypypdffor merging
Node.js:
pdf-libcan embed and draw images directly.
Important considerations
- Transparency: Use alpha/opacity so the watermark doesn’t obscure content.
- Page size/orientation: Handle portrait and landscape pages correctly.
- Performance: For large PDFs, avoid excessive page rendering unless needed.
- Compliance/security: A watermark is not a security feature by itself; it’s mostly deterrence/identification.
If you need stronger protection
Consider:
- password protection / permissions
- digital signatures
- rights management / DRM
- server-side access control
If you tell me your preferred language (Python, Node, Java, C#, etc.), I can give you a ready-to-run example.