Add Text Stamps to PDF in Python

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.

  1. Open the PDF document.
  2. Create a TextStamp object.
  3. Set stamp background behavior.
  4. Position the stamp on the page.
  5. Rotate the stamp if needed.
  6. Set text properties.
  7. Add the stamp to a page.
  8. 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)