Python实现PDF添加水印无效问题排查与解决请求
Fix: Watermark Not Showing Up in Your PDF
Let's break down why your watermark isn't appearing and fix it step by step:
Key Issues in Your Code
- You're adding the original page to the writer before applying the watermark: Once you call
pdf_writer.add_page(pdf_page), that unmodified page is locked into the output. Merging the watermark afterward doesn't update the page already in the writer. - Hardcoded page size (
letter) might not match your source PDF: If your input PDF uses a different page size (like A4), the watermark could be drawn outside the visible area. - Merge order might hide the watermark: By default, merging can place the watermark behind the original content, making it invisible if the original page has a solid background.
Corrected Code
import os import io from PyPDF2 import PdfReader, PdfWriter from reportlab.lib.pagesizes import letter, A4 from reportlab.pdfgen import canvas folder_path = 'D:/pdf/input/' output_folder = 'D:/pdf/output/' image_path = 'D:/pdf/watermark.jpg' x, y = 50, 50 page_number = 1 # 1-based index if not os.path.exists(output_folder): os.makedirs(output_folder) for pdf_file in os.listdir(folder_path): if pdf_file.endswith(".pdf"): pdf_reader = PdfReader(open(os.path.join(folder_path, pdf_file), "rb")) pdf_writer = PdfWriter() for page_num in range(len(pdf_reader.pages)): pdf_page = pdf_reader.pages[page_num] # Only apply watermark to the target page if page_num == page_number - 1: # convert to 0-based index # Get the actual page size of the source PDF page page_width = float(pdf_page.mediabox.width) page_height = float(pdf_page.mediabox.height) # Create a canvas with the same size as the source page packet = io.BytesIO() can = canvas.Canvas(packet, pagesize=(page_width, page_height)) # Optional: Make sure watermark is drawn on top (set transparency if needed) # can.setFillAlpha(0.3) # uncomment to make watermark semi-transparent can.drawImage(image_path, x, y) can.save() # Move to the start of the BytesIO buffer packet.seek(0) watermark_pdf = PdfReader(packet) # Merge the watermark ON TOP of the original page pdf_page.merge_page(watermark_pdf.pages[0], over=True) # Add the (possibly modified) page to the writer pdf_writer.add_page(pdf_page) with open(os.path.join(output_folder, pdf_file), "wb") as output_file: pdf_writer.write(output_file) print("Watermarks added successfully to the specified pages!")
What Changed?
- Reordered the page addition: Now we modify the page first (if it's the target page), then add it to the writer. This ensures the watermarked page is saved.
- Dynamic page size: We fetch the actual dimensions of the source PDF page using
mediabox, so the canvas matches exactly—no more off-screen watermarks. - Explicit top-layer merge: Added
over=Truetomerge_page()to ensure the watermark is drawn on top of the original content. - Optional transparency: Added a commented line to make the watermark semi-transparent (super useful for text/watermark images that shouldn't block content).
Additional Tips
- Test with a small, simple PDF first to verify the watermark position.
- If your watermark is still missing, check the
x/ycoordinates—remember PDF uses a coordinate system where the origin (0,0) is the bottom-left corner of the page. - Ensure your watermark image is not too large for the page dimensions.
内容的提问来源于stack exchange,提问作者Przemysław Mateusz Buzałek
相关产品推荐
相关产品推荐

