You can use the Chunk
‘s setUnderline(float thickness, float yPosition)
method to add underline or strike through to a chunk. A negative value to the yPosition
create an underline while a positive value will produce a strike through.
package org.kodejava.itextpdf;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class UnderlineStrikeThroughDemo {
public static void main(String[] args) {
Document doc = new Document();
try {
PdfWriter.getInstance(doc, new FileOutputStream("UnderStrike.pdf"));
doc.open();
// Creates a chunk with an underline with 0.1 thickness
Chunk underline = new Chunk("The quick brown fox ");
underline.setUnderline(0.1f, -1f);
doc.add(underline);
// Creates a strike through chunk with 1 thickness
Chunk strike = new Chunk("jumps over the lazy dog.");
strike.setUnderline(1f, 3f);
doc.add(strike);
} catch (DocumentException | FileNotFoundException e) {
e.printStackTrace();
} finally {
doc.close();
}
}
}
Maven Dependencies
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.3</version>
</dependency>
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024
But how to read and replace from existing file? Above code works only for new PDF.
I have one below case. Using Graphics2d object first draw the line by applying stroke. After that write the text with underline using chunk class by setting underline. But it’s not updating the under line for generated pdf. Could you please let us know what is the root cause.