Aggiungi timbro di testo al PDF tramite Python

Crea un timbro di testo in modo programmatico utilizzando Aspose.PDF per la libreria Python for .NET

Come aggiungere timbri di testo al PDF utilizzando la libreria Python for .NET

Per utilizzare il timbro di testo nei file PDF, utilizza Aspose.PDF for Python via .NET, un’API potente e facile da usare. Apri PyPI, cerca aspose-pdf e installalo. In alternativa, esegui il comando:

Console

pip install aspose-pdf

Aggiungi timbro di testo al documento PDF Python


È necessario Aspose.PDF for Python via .NET per provare il codice nel proprio ambiente.

  1. Carica il PDF con un’istanza di Document.
  2. Aprire un documento PDF utilizzando l’oggetto Document.
  3. Creare un timbro di testo e definirne le proprietà.
  4. Aggiungere il timbro di testo alla pagina utilizzando il metodo AddStamp

Aggiungi timbro di testo al PDF con Python

import aspose.pdf as apdf

from os import path

path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, outfile)

document = apdf.Document(path_infile)
document.pages.add()
# Create text stamp
textStamp = apdf.TextStamp("Sample Stamp")
# Set whether stamp is background
textStamp.background = True
# Set origin
textStamp.x_indent = 100
textStamp.y_indent = 100
# Rotate stamp
textStamp.rotate = apdf.Rotation.ON90
# Set text properties
textStamp.text_state.font = apdf.text.FontRepository.find_font("Arial")
textStamp.text_state.font_size = 14
textStamp.text_state.font_style = apdf.text.FontStyles(
    apdf.text.FontStyles.BOLD | apdf.text.FontStyles.ITALIC )
textStamp.text_state.foreground_color = apdf.Color.aqua
# Add stamp to particular page
document.pages[1].add_stamp(textStamp)

# Save output document
document.save(path_outfile)