Java PDF Generation with IText
Introduction
Getting Started
Document
Chunk
Phrase
Paragraph
Chapter + Section
Anchor
List
Tables
Images
Superscript + Subscript
Underline + Strikethrough
Fonts
 
Modifying Existing PDF Documents
 


Java IText: Underline + Strikethrough

Connect with me:  
  Jenkov.com - News

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

NextNext : Font

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