function add_watermark
A wrapper function that adds a customizable text watermark to every page of a PDF document with configurable opacity and color.
/tf/active/vicechatdev/CDocs/utils/pdf_utils.py
2162 - 2186
simple
Purpose
This function provides a module-level interface for adding watermarks to PDF files. It delegates the actual watermarking operation to a PDFManipulator class instance, making it easier to add watermarks without directly instantiating the manipulator class. Common use cases include branding documents, marking confidential materials, adding copyright notices, or labeling draft versions of documents.
Source Code
def add_watermark(input_path: str, output_path: str, watermark_text: str,
opacity: float = 0.3, color: str = "gray") -> str:
"""
Add a watermark to every page of a PDF (module-level wrapper)
Parameters
----------
input_path : str
Path to the input PDF
output_path : str
Path where the watermarked PDF will be saved
watermark_text : str
Text to use as watermark
opacity : float, optional
Opacity of the watermark (0.0 to 1.0)
color : str, optional
Color of the watermark
Returns
-------
str
Path to the watermarked PDF
"""
manipulator = PDFManipulator()
return manipulator.add_watermark(input_path, output_path, watermark_text, opacity, color)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
input_path |
str | - | positional_or_keyword |
output_path |
str | - | positional_or_keyword |
watermark_text |
str | - | positional_or_keyword |
opacity |
float | 0.3 | positional_or_keyword |
color |
str | 'gray' | positional_or_keyword |
Parameter Details
input_path: String path to the source PDF file that needs watermarking. Must be a valid file path pointing to an existing PDF document.
output_path: String path where the watermarked PDF will be saved. Can be a new file or overwrite an existing one. The directory must exist or be writable.
watermark_text: The text string to be displayed as a watermark on each page of the PDF. Can be any string value such as 'CONFIDENTIAL', 'DRAFT', company name, or copyright notice.
opacity: Float value between 0.0 (completely transparent) and 1.0 (completely opaque) controlling the watermark's transparency. Default is 0.3 for subtle visibility. Lower values make the watermark less intrusive.
color: String specifying the color of the watermark text. Accepts color names (e.g., 'gray', 'red', 'blue') or potentially hex values depending on the underlying implementation. Default is 'gray' for a neutral appearance.
Return Value
Type: str
Returns a string containing the path to the newly created watermarked PDF file. This is typically the same as the output_path parameter, confirming successful creation of the watermarked document.
Dependencies
reportlabfitzpikepdfPILdocx2pdfpandas
Required Imports
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter, A4
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch, cm
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, Image, PageBreak, Flowable
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
import fitz
import pikepdf
from PIL import Image as PILImage
from docx2pdf import convert
import pandas as pd
Usage Example
# Add a watermark to a PDF document
input_pdf = 'document.pdf'
output_pdf = 'watermarked_document.pdf'
watermark = 'CONFIDENTIAL'
# Basic usage with defaults
result = add_watermark(input_pdf, output_pdf, watermark)
print(f'Watermarked PDF saved to: {result}')
# Custom opacity and color
result = add_watermark(
input_path='report.pdf',
output_path='branded_report.pdf',
watermark_text='© 2024 Company Name',
opacity=0.5,
color='blue'
)
# Subtle draft watermark
result = add_watermark(
'draft.pdf',
'draft_marked.pdf',
'DRAFT - DO NOT DISTRIBUTE',
opacity=0.2,
color='red'
)
Best Practices
- Ensure the input PDF file exists and is readable before calling this function
- Verify that the output directory exists and has write permissions
- Use opacity values between 0.1 and 0.5 for readable watermarks that don't obscure content
- Test watermark visibility on different PDF backgrounds (white, colored, images) before production use
- Consider the length of watermark_text - very long strings may not display properly
- Keep a backup of the original PDF if overwriting is not intended
- The function depends on PDFManipulator class being properly initialized and available
- Handle potential exceptions from file I/O operations and PDF processing
- For batch processing, consider wrapping this function in error handling to continue processing other files if one fails
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class Watermarker 74.3% similar
-
class PDFManipulator 60.1% similar
-
function convert_to_pdf 50.4% similar
-
function merge_pdfs 50.3% similar
-
function convert_document_to_pdf 49.1% similar