2014年10月25日 星期六

itext-underline

http://tutorials.jenkov.com/java-itext/underline-strikethrough.html


Java IText: Underline + Strikethrough


By Jakob Jenkov
 Connect with me:
Rate article:
Share article:

You can add underline and strikethrough text using the Chunk class, and its setUnderline()method. You use a negative underline value to get the line lower below the text, and a positive underline value to get the line to strike through the text.
Here is a simple code example:
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

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

    Document document = new Document();

    try {
      PdfWriter.getInstance(document,
            new FileOutputStream("UnderlineStrikethrough.pdf"));

      document.open();

      Chunk underline = new Chunk("Underline. ");
      underline.setUnderline(0.1f, -2f); //0.1 thick, -2 y-location
      document.add(underline);

      document.add(new Paragraph("   "));

      Chunk strikethrough = new Chunk("Strikethrough.");
      strikethrough.setUnderline(0.1f, 3f); //0.1 thick, 2 y-location
      document.add(strikethrough);

      document.close();

    } catch (DocumentException e) {
      e.printStackTrace();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

  }

}
Here is what the generated document looks like:
IText Chunk's with underline and strikethrough
IText Chunk's with underline and strikethrough

沒有留言:

張貼留言