Last modified: Feb 14, 2026 By Alexander Williams

Add Text Overlay to PDF with Python

Working with PDFs is a common task. You might need to add watermarks, stamps, or annotations.

Python offers powerful libraries to automate this. This guide shows you how to add text overlays.

You will learn to place text on existing PDFs. This is useful for branding or adding notes.

Why Add Text to PDFs Programmatically?

Manual editing of PDFs is slow and error-prone. Doing it with code is fast and consistent.

Imagine processing hundreds of invoices. You need to stamp "APPROVED" on each one.

Python can do this in seconds. It saves time and reduces human error.

Other use cases include adding page numbers, dates, or confidential labels. Automation is key for efficiency.

Essential Python Libraries for PDF Manipulation

Two main libraries are excellent for this task: PyMuPDF (fitz) and ReportLab.

PyMuPDF is great for modifying existing PDFs. It allows you to open a PDF and draw on its pages.

ReportLab is better for creating new PDFs from scratch. It can also overlay text on existing pages with some work.

We will focus on PyMuPDF for its simplicity in editing. First, you need to install it.


pip install PyMuPDF
    

Adding a Simple Text Overlay with PyMuPDF

Let's start with a basic example. We will open a PDF and add a text string to the first page.

The fitz.open() function opens the PDF file. We then get the first page with load_page().

The insert_text() method is used to place the text at specific coordinates. PDF coordinates start from the bottom-left.


import fitz  # PyMuPDF

# Open the existing PDF document
doc = fitz.open("input_document.pdf")

# Select the first page (page number 0)
page = doc.load_page(0)

# Define the text and its position (x, y from bottom-left)
text = "CONFIDENTIAL"
x_coord = 50   # 50 points from the left
y_coord = 800  # 800 points from the bottom

# Insert the text onto the page
page.insert_text((x_coord, y_coord), text, fontsize=24, color=(1, 0, 0))  # Red color

# Save the modified document to a new file
doc.save("output_document_with_overlay.pdf")
doc.close()
print("Text overlay added successfully!")
    

Text overlay added successfully!
    

Controlling Text Appearance and Position

The example above used a simple red text. You have full control over the styling.

The insert_text() method accepts many parameters. You can set font size, color, and opacity.

Color is defined as an RGB tuple. (1, 0, 0) is red. (0, 0, 0) is black. (0, 0, 1) is blue.

Positioning is critical. You often need to calculate the center of the page. You can get page dimensions with page.rect.


import fitz

doc = fitz.open("input_document.pdf")
page = doc.load_page(0)

# Get page dimensions
page_rect = page.rect
page_width = page_rect.width
page_height = page_rect.height

# Calculate center position for the text
text = "SAMPLE WATERMARK"
fontsize = 60
# Approximate text width is fontsize * len(text) * 0.5 (rough estimate)
text_width_approx = fontsize * len(text) * 0.5
x_center = (page_width - text_width_approx) / 2
y_center = page_height / 2

# Insert text with light gray color and transparency
page.insert_text(
    (x_center, y_center),
    text,
    fontsize=fontsize,
    color=(0.5, 0.5, 0.5),  # Gray color
    rotate=45,              # Rotate text 45 degrees
    overlay=True            # Draw over existing content
)

doc.save("output_centered_watermark.pdf")
doc.close()
    

Adding Text to Multiple Pages

To add text to every page, loop through all pages in the document. Use doc.page_count to get the total.

This is perfect for adding page numbers or a uniform footer. The logic is the same for each page.


import fitz

doc = fitz.open("input_document.pdf")

for page_num in range(doc.page_count):
    page = doc.load_page(page_num)
    footer_text = f"Page {page_num + 1} of {doc.page_count}"
    # Position text at the bottom center
    page_width = page.rect.width
    page.insert_text((page_width / 2 - 30, 40), footer_text, fontsize=10)

doc.save("output_with_page_numbers.pdf")
doc.close()
    

Advanced Overlays with ReportLab

ReportLab is another powerful tool. It is often used for generating PDFs. You can also use it to create an overlay page.

This method involves creating a new PDF with your text. Then you merge it with the original as a watermark.

It is more complex but offers superior typographic control. For simpler edits, PyMuPDF is recommended.

If you need to extract text from images within a PDF before overlaying, our Python Text Extraction from Images Guide can help.

Handling Text and File Operations

When working with text in files, Python's I/O tools are essential. For advanced text stream handling, understanding TextIOWrapper is useful.

You can learn more about efficient file operations in our guide on Python TextIOWrapper: Handle Text File Operations Efficiently.

This knowledge helps when reading configuration files for your overlay scripts.

Common Challenges and Solutions

Text Not Appearing: Check your coordinates. They might be outside the page boundary. Print the page dimensions to debug.

Wrong Font: PyMuPDF uses a default font. You can load other fonts, but it requires extra steps.

File Permissions: Ensure your script has read/write access to the input and output files.

Always test on a copy of your original PDF. This prevents accidental data loss.

Conclusion

Adding text overlays to PDFs with Python is straightforward. The PyMuPDF library makes it easy.

You can add watermarks, footers, or annotations automatically. This skill is valuable for document automation.

Start with simple text and coordinates. Then experiment with styling and multi-page documents.

Remember to handle files carefully. Use the right tools for your specific task. Happy coding!