Add Text Stamps to PDF in Python
Contents
[
Hide
]
Adding Text Stamp with Python
You can use TextStamp class to add a text stamp in a PDF file. TextStamp class provides properties necessary to create a text based stamp like font size, font style, and font color etc. In order to add text stamp, you need to create a Document object and a TextStamp object using required properties. After that, you can call add_stamp() method of the Page to add the stamp in the PDF. The following code snippet shows you how to add text stamp in the PDF file. This is useful for adding annotations, watermarks, or labels to PDF pages.
- Open the PDF document.
- Create a TextStamp object.
- Set stamp background behavior.
- Position the stamp on the page.
- Rotate the stamp if needed.
- Set text properties.
- Add the stamp to a page.
- Save the modified PDF document.
import sys
import aspose.pdf as ap
from os import path
def add_text_stamp(input_file_name, output_file_name):
document = ap.Document(input_file_name)
# Create text stamp
text_stamp = ap.TextStamp("Sample Stamp")
# Set whether stamp is background
text_stamp.background = True
# Set origin
text_stamp.x_indent = 100
text_stamp.y_indent = 100
# Rotate stamp
text_stamp.rotate = ap.Rotation.ON90
# Set text properties
text_stamp.text_state.font = ap.text.FontRepository.find_font("Arial")
text_stamp.text_state.font_size = 14.0
text_stamp.text_state.font_style = (
ap.text.FontStyles.BOLD | ap.text.FontStyles.ITALIC
)
text_stamp.text_state.foreground_color = ap.Color.dark_green
# Add stamp to particular page
document.pages[1].add_stamp(text_stamp)
document.save(output_file_name)