How to Underline Text in a PDF using C#

This article explains how to underline text in a PDF using C#. The details about the environment’s settings, a list of tasks to be performed and a runnable sample code demonstrating how to underline text in PDF using C# is part of it. You will also learn the options to perform other relevant operations on the text in a PDF.

Steps to Underline in PDF File using C#

  1. Set the environment to use Aspose.PDF for .NET to underline the text
  2. Create a new PDF file using the Document class and add a page to it
  3. Create a TextBuilder object and link it to a particular page where some text is to be underlined
  4. Create some sample text using the TextFragment and set its font, position, and the underline flag
  5. Append this text fragment to the PDF file using the TextBuilder object
  6. Save the output PDF file with underlined text

The description of how to underline text in PDF file using C# is provided in the above steps. Create a new PDF by adding some sample text into it and excess the target text using the TextFragment class. Finally, the Underline flag must be true before appending the TextFragment to the PDF file.

Code to Underline Text in PDF using C#

This code demonstrates how to underline in PDF using C#. In this code sample, a new PDF file is created with a sample text and then TextFragment.TextState property is used that contains the underline flag. If you want to underline text in an existing PDF file, you may use the TextFragmentAbsorber class to search the target text and then underline it.

In this tutorial, we have learned to underline a text, however, if you are inclined to learn the process to strike out text in a PDF, refer to the article on how to strike out text in PDF using C#.