Skip to content

Instantly share code, notes, and snippets.

@aspose-com-kb
Last active February 25, 2023 05:04
How to Underline in PDF using Python. For more information: https://kb.aspose.com/pdf/python/how-to-underline-in-pdf-using-python/
import aspose.pdf as pdf
# Load License
license = pdf.License()
license.set_license("Aspose.Total.lic")
# Create documentation object
pdfDocument = pdf.Document()
# Add age page to PDF document
pdfDocument.pages.add()
# Create TextBuilder for first page
tb = pdf.text.TextBuilder(pdfDocument.pages[1])
# TextFragment with sample text
fragment = pdf.text.TextFragment("Test message")
# Set the font for TextFragment
fragment.text_state.font = pdf.text.FontRepository.find_font("Arial")
fragment.text_state.font_size = 10
# Set the Underline flag
fragment.text_state.underline = True
# Specify the text position
fragment.position = pdf.text.Position(10, 800)
# Append TextFragment to PDF file
tb.append_text(fragment)
# Save the resulting PDF document
pdfDocument.save("underlined.pdf")
print("Text underlined in PDF successfully")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment