How to strike through text using iText?

I have 2 numbers one above the other, but the first one must have an Strikethrough. I'm using a table and a cell to put both numbers in the table, is there a way to meet my requirement?

31st May 2016

Posted on StackOverflow on Feb 19, 2015 by Ischaves

Please take a look at the SimpleTable6 example:

Screen shot

Screen shot

In the first row, we strike through a number using a setLineThrough() method:

PdfFont font = PdfFontFactory.createFont(FontProgramFactory.createFont(FontConstants.HELVETICA));
table.addCell(new Cell().add(new Paragraph("0123456789").setFont(font).setLineThrough()));

In this case, iText has made a couple of decisions for you: where do I put the line? How thick is the line?

If you want to make these decisions yourself, you can use the setUnderline() method:

Text text1 = new Text("0123456789");
text1.setUnderline(1.5f, -1);
table.addCell(new Paragraph(text1));
Text text2 = new Text("0123456789");
text2.setUnderline(1.5f, 3.5f);
table.addCell(new Paragraph(text2));

If you pass a negative value for the y-offset parameter, the Text will be underlined (see first column). You can also use this method to strike through text by passing a positive y-offset.

As you can see, we also defined the thickness of the line (1.5f). There is another setUnderline() method that also allows you to pass the following parameters:

  • color - the color of the line or null to follow the text color

  • thickness - the absolute thickness of the line

  • thicknessMul - the thickness multiplication factor with the font size

  • yPosition - the absolute y position relative to the baseline

  • yPositionMul - the position multiplication factor with the font size

  • cap - the end line cap.

Click this link if you want to see how to answer this question in iText 5.


Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now