How to underline text with a dotted line?

Tags: dotted lineseparatorunderlineiText 7

I need to merge 2 paragraphs, the first is a sequence of dots, and the second is the text that I want write on dots:

Paragraph pdots1 = new Paragraph(
    "..................................................", font10);
Paragraph  pnote= new Paragraph("Some text on the dots", font10);
I tried to play with: pnote.setExtraParagraphSpace(-15); But this messes up the next paragraphs.

Posted on StackOverflow on Mar 13, 2014 by Accollativo

It's not a good idea to use a String with dots when you need a dotted line. It's better to use a dotted line created using the DottedLine class. See for instance the UnderlineWithDottedLine example:

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
doc.add(new Paragraph("This line will be underlined with a dotted line.").setMarginBottom(0));
doc.add(new LineSeparator(new DottedLine(1, 2)).setMarginTop(-4));
doc.close();

In this example (see underline_dotted.pdf for the result), I add the line under the baseline of the paragraph (using the setMarginTop() method of LineSeparator with negative value) and I define a lineWidth of 1 point and gap of 2 points between the dots (using the constructor of DottedLine).

Click this link if you want to see how to answer this question in iText 5.