Use java.awt.Font to draw strings : Graphics2D « PDF « Java Tutorial






import java.awt.Graphics2D;
import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.AsianFontMapper;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    String text = "\u5e73\u548C";
    PdfContentByte cb = writer.getDirectContent();
    PdfTemplate tp = cb.createTemplate(100, 50);
    AsianFontMapper mapper = new AsianFontMapper(AsianFontMapper.JapaneseFont_Min,
        AsianFontMapper.JapaneseEncoding_H);
    Graphics2D g2 = tp.createGraphics(100, 50, mapper);
    java.awt.Font font = new java.awt.Font("Arial Unicode MS", java.awt.Font.PLAIN, 12);
    g2.setFont(font);
    g2.drawString(text, 0, 40);
    g2.dispose();
    cb.addTemplate(tp, 36, 780);
    document.close();
  }
}








29.42.Graphics2D
29.42.1.Text with an absolute position using java.awt.Graphics2D object
29.42.2.Use java.awt.Font to draw strings
29.42.3.Create Graphics2D from PdfTemplate
29.42.4.Draw with Graphics2D