Add Text to PDF via Python

Add text to PDF document with Python for .NET. Use Aspose.PDF to modify PDF documents programmatically

How to Work with Text in PDF Using Python for .NET Library

To add Text into PDF File, we’ll use Aspose.PDF for Python API, which is a feature-rich, powerful, and easy-to-use document manipulation API for .NET. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Python Package Manager Console

pip install aspose-pdf

Add Text to PDF File via Python


To try the code in your environment, you need Aspose.PDF for Python.

  1. Load the PDF with an instance of Document.
  2. Create a TextParagraph and define its properties.
  3. Add the TextParagraph to Page using TextBuilder.
  4. Save the file again.

Add Text to PDF - Python

This sample code shows how to add text into PDF document - Python

import aspose.pdf as ap

# Open document
document = ap.Document(input_pdf)

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

# Create text fragment
text_fragment = ap.text.TextFragment("main text")
text_fragment.position = ap.text.Position(100, 600)

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

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

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

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