Jenkov.com
Tutorials Books About
Java Itext

1 Java PDF Generation with IText
2 Getting Started with IText PDF API for Java
3 Java IText: Document
4 Java IText: Chunk
5 Java IText: Phrase
6 Java IText: Paragraph
7 Java IText: Chapter + Section
8 Java IText: Anchor (link)
9 Java IText: List
10 Java IText: Table
11 Java IText: Image
12 Java IText: Superscript + Subscript
13 Java IText: Underline + Strikethrough
14 Java IText: Font
15 Java IText: Modifying Existing PDF Documents




Java IText: Underline + Strikethrough


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!