Underline and strike-through in a cell

This is a code example of iText PDF, discover more.

31st May 2016
admin-marketing

Switch code for this example

SimpleTable6.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/28617095/strikethrough-in-cell-using-itext-in-android-java
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.io.font.FontConstants;
  15. import com.itextpdf.io.font.FontProgramFactory;
  16. import com.itextpdf.kernel.font.PdfFont;
  17. import com.itextpdf.kernel.font.PdfFontFactory;
  18. import com.itextpdf.kernel.pdf.PdfDocument;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.layout.Document;
  21. import com.itextpdf.layout.element.Cell;
  22. import com.itextpdf.layout.element.Paragraph;
  23. import com.itextpdf.layout.element.Table;
  24. import com.itextpdf.layout.element.Text;
  25. import com.itextpdf.samples.GenericTest;
  26. import com.itextpdf.test.annotations.type.SampleTest;
  27.  
  28. import org.junit.experimental.categories.Category;
  29.  
  30. import java.io.File;
  31.  
  32. @Category(SampleTest.class)
  33. public class SimpleTable6 extends GenericTest {
  34.     public static final String DEST = "./target/test/resources/sandbox/tables/simple_table6.pdf";
  35.  
  36.     public static void main(String[] args) throws Exception {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new SimpleTable6().manipulatePdf(DEST);
  40.     }
  41.  
  42.     @Override
  43.     protected void manipulatePdf(String dest) throws Exception {
  44.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  45.         Document doc = new Document(pdfDoc);
  46.  
  47.         Table table = new Table(2);
  48.         table.addCell("0123456789");
  49.         PdfFont font = PdfFontFactory.createFont(FontProgramFactory.createFont(FontConstants.HELVETICA));
  50.         table.addCell(new Cell().add(new Paragraph("0123456789").setFont(font).setLineThrough()));
  51.         Text text1 = new Text("0123456789");
  52.         text1.setUnderline(1.5f, -1);
  53.         table.addCell(new Paragraph(text1));
  54.         Text text2 = new Text("0123456789");
  55.         text2.setUnderline(1.5f, 3.5f);
  56.         table.addCell(new Paragraph(text2));
  57.         doc.add(table);
  58.  
  59.         doc.close();
  60.     }
  61. }
Contact

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