Aggiungi testo al PDF tramite Python

Aggiungi testo al documento PDF con Python for .NET. Usa Aspose.PDF per modificare i documenti PDF a livello di codice

Come lavorare con il testo in un PDF utilizzando la libreria Python for .NET

Per aggiungere testo a un file PDF, utilizzeremo Aspose.PDF for Python, 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 testo al file PDF tramite Python


Per provare il codice nel tuo ambiente, hai bisogno di Aspose.PDF for Python.

  1. Carica il PDF con un’istanza di Document.
  2. Crea un TextParagraph e definisci le sue proprietà.
  3. Aggiungi il TextParagraph alla pagina usando TextBuilder.
  4. Salvate nuovamente il file.

Aggiungi testo al PDF - Python

Questo codice di esempio mostra come aggiungere testo in un documento PDF - 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)

# Get particular page
page = document.pages[1]

# Create text fragment
text_fragment = apdf.text.TextFragment("Hello, world!")
text_fragment.position = apdf.text.Position(100, 600)

# Set text properties
text_fragment.text_state.font_size = 12
text_fragment.text_state.font = apdf.text.FontRepository.find_font("TimesNewRoman")
text_fragment.text_state.background_color = apdf.Color.light_gray
text_fragment.text_state.foreground_color = apdf.Color.red

# Create TextBuilder object
builder = apdf.text.TextBuilder(page)

# Append the text fragment to the PDF page
builder.append_text(text_fragment)

# Save resulting PDF document.
document.save(path_outfile )