Añadir texto a un PDF a través de Python

Añada texto al documento PDF con Python for .NET. Utilice Aspose.PDF for modificar documentos PDF mediante programación

Cómo trabajar con texto en PDF mediante la biblioteca Python for .NET

Para añadir texto a un archivo PDF, usaremos Aspose.PDF for Python, una API potente y fácil de usar. Abra PyPI, busque aspose-pdf e instálelo. También puede ejecutar el comando:

Console

pip install aspose-pdf

Agregue texto a un archivo PDF a través de Python


Para probar el código de su entorno, necesita Aspose.PDF for Python.

  1. Cargue el PDF con una instancia de Document.
  2. Cree un TextParagraph y defina sus propiedades.
  3. Agregue el párrafo de texto a la página con TextBuilder.
  4. Vuelva a guardar el archivo.

Añadir texto al PDF - Python

Este código de ejemplo muestra cómo añadir texto a 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 )