2014年10月25日 星期六

itext-font

http://tutorials.jenkov.com/java-itext/font.html

Font f3 = FontFactory.getFont("MSung-Light","UniCNS-UCS2-H", BaseFont.NOT_EMBEDDED);//chinese
Font f2 = new Font(baseFont);
//BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",false);//chinese








Java IText: Font


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

You can specify fonts for most text objects (Chunk, Phrase, Paragraph etc.) in IText. Actually, you can do a lot with fonts in IText. Too much to cover here, so I'll just cover the basics. Get the book "IText in Action" to get the full story on fonts.
To use a font you must first create the font. Then you pass it to the text object in it's constructor. Here is a simple code example:
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;

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

public class FontExample {

  public static void main(String[] args) {

    Document document = new Document();

    try {
      PdfWriter.getInstance(document,
        new FileOutputStream("Font.pdf"));
      
          Font font1 = new Font(Font.FontFamily.HELVETICA  , 25, Font.BOLD);
          Font font2 = new Font(Font.FontFamily.COURIER    , 18,
          Font.ITALIC | Font.UNDERLINE);
          Font font3 = new Font(Font.FontFamily.TIMES_ROMAN, 27);
      
      document.open();

      document.add(new Chunk(    "This is sentence 1. ", font1));
      document.add(new Phrase(   "This is sentence 2. ", font2));
      document.add(new Paragraph("This is sentence 3. ", font3));

      document.close();

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

  }
}
Here is what the generated document looks like:
An IText Font example
An IText Font example

沒有留言:

張貼留言