How to underline a portion of text in a text field?

I have an application that uses iText to fill PDF form fields. One of these fields has some text with tags. For example:


U>This text/U> should be underlined. 
I need the text enclosed in -tag to be underlined. How could I do that?

Posted on StackOverflow on Feb 18, 2015 by Galma88

Ordinary text fields do not support rich text. If you want the fields to remain interactive, you will need RichText fields. These are fields that are flagged in a way that they accept an RV value.

If it is OK for you to flatten the form (i.e. remove all interactivity), please take a look at the FillWithUnderline example:

public void manipulatePdf(String src, String dest)
    throws DocumentException, IOException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    AcroFields form = stamper.getAcroFields();
    FieldPosition pos = form.getFieldPositions("Name").get(0);
    ColumnText ct = new ColumnText(stamper.getOverContent(;
    ElementList elements = XMLWorkerHelper.parseToElementList(
Bruno Lowagie

", null); for (Element element : elements) { ct.addElement(element); } ct.go(); stamper.close(); }

In this example, we don't fill out the field, but we get the fields position (a page number and a rectangle). We then use ColumnText to add content at this position. As we are inputting HTML, we use XML Worker to parse the HTML into iText objects that we can add to the ColumnText object.

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now