iText API- Underline and Strike-through Example in Java


In this tutorial, we are going to show how you will add an underline to a text and strike-through a text using iText in Java. Using the Chunk class, and its setUnderline() method, you can add the underline or strike-through to any text in the PDF document.

UnderlineAndStrikethroughExample.java
package org.websparrow.itext;

import java.io.FileOutputStream;

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

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

			Document document = new Document();
			PdfWriter.getInstance(document, new FileOutputStream("iText/underline-strikethrough.pdf"));
			document.open();

			// Underline to the text
			Paragraph paragraph = new Paragraph("Welcome to ");
			Chunk underline = new Chunk("WebSparrow.org");
			underline.setUnderline(0.1f, -2f); // 0.1 thick, -2 y-location
			paragraph.add(underline);
			document.add(paragraph);

			// Strike-through to the text
			Paragraph paragraph1 = new Paragraph("Welcome to ");
			Chunk strikethrough = new Chunk("WebSparrow.org");
			strikethrough.setUnderline(0.1f, 3f); // 0.1 thick, 2 y-location
			paragraph1.add(strikethrough);
			document.add(paragraph1);

			document.close();
			System.out.println("Successfull");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Output:

iText API- Underline and Strike-through Example in Java

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.