Jenkov.com
Tutorials About




Java IText: Underline + Strikethrough


Last updated: 2014-05-24

You can add underline and strikethrough text using the Chunk class, and its setUnderline() method. You use a negative underline value to get the line lower below the text, and a positive underline value to get the line to strike through the text.

Here is a simple code example:

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class UnderlineStrikethroughExample {
  public static void main(String[] args) {

    Document document = new Document();

    try {
      PdfWriter.getInstance(document,
            new FileOutputStream("UnderlineStrikethrough.pdf"));

      document.open();

      Chunk underline = new Chunk("Underline. ");
      underline.setUnderline(0.1f, -2f); //0.1 thick, -2 y-location
      document.add(underline);

      document.add(new Paragraph("   "));

      Chunk strikethrough = new Chunk("Strikethrough.");
      strikethrough.setUnderline(0.1f, 3f); //0.1 thick, 2 y-location
      document.add(strikethrough);

      document.close();

    } catch (DocumentException e) {
      e.printStackTrace();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

  }

}

Here is what the generated document looks like:

IText Chunk's with underline and strikethrough
IText Chunk's with underline and strikethrough


Connect with me: Newsletter - Get all my free tips!
     



Close TOC
This website uses cookies to improve the user experience and gather statistics. Our advertisers use cookies too (3rd party cookies), to provide more relevant ads. Continued use of this website implies that you accept the use of cookies on this website. We do not share our cookies with our advertisers, and our advertisers do not share cookies with us.
OK