How to Underline in PDF using Python

This short tutorial guides on how to underline in PDF using Python. It shares the details to configure the development environment, a list of steps to write the application, and a runnable sample code to underline in PDF file using Python. You will get an introduction to all the important classes, methods, and properties required to add and underline a text without installing any other third-party tool.

Steps to Underline Text in PDF using Python

  1. Set the environment to use Aspose.PDF for Python via .NET to add underlined text
  2. Create a new PDF file using the Document class and add a new page
  3. Instantiate a TextBuilder object linked with the first page
  4. Create a TextFragment object and set the text, font, underline flag, and position of the text
  5. Append the TextFragment to the page using the TextBuilder object created above
  6. Save the resultant PDF document having underlined text in it

These steps define the process of how to underline in PDF file using Python. The process is commenced by creating a PDF file and then initializing a TextBuilder object by linking it with the selected page where text is to be added and underlined. The TextFragment object is initiated that contains properties to add formatted text at a particular position along with the flag to set the underline.

Code to Underline in PDF using Python

This code demonstrates the process of how to underline a PDF document using Python. It uses the TextBuilder class object that is initiated with the target page and contains features to append text and other contents. The TextFragment object is initiated with the sample text and a text_state property is configured by setting the font name, font size, underline flag, and text position.

This article has guided us on how to underline text in a PDF using Python. If you want to learn the process to strike a text, refer to the article on how to strike out text in Adobe PDF using Python.

 English