Thursday 17 November 2016

itext: Underline and strike-through in a cell

By using following font instance, you can strike through the content.

private static Font font = new Font(FontFamily.HELVETICA, 12f, Font.STRIKETHRU);
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class TestTable {
 private static final String fileName = "/Users/harikrishna_gurram/Documents/itext/tableEx.pdf";
 private static Font font = new Font(FontFamily.HELVETICA, 12f, Font.STRIKETHRU);

 private static PdfPTable getSquareTable() {
  PdfPTable table = new PdfPTable(2);
  table.setHeaderRows(1);
  PdfPCell cell = new PdfPCell();

  cell.setPhrase(new Phrase("Number"));
  table.addCell(cell);

  cell.setPhrase(new Phrase("Square"));
  table.addCell(cell);

  for (int i = 1; i < 10; i++) {

   cell.setPhrase(new Phrase(Integer.toString(i)));
   table.addCell(cell);

   table.addCell(new Phrase(Integer.toString(i * i + 1), font));
  }
  return table;

 }

 public static void main(String args[]) throws DocumentException, MalformedURLException, IOException {

  Document document = new Document();
  PdfWriter.getInstance(document, new FileOutputStream(fileName));

  document.open();

  PdfPTable table = getSquareTable();
  document.add(table);

  document.close();
 }
}

\





Previous                                                 Next                                                 Home

No comments:

Post a Comment