本文转载[url]http://blog.donews.com/ooFrank/archive/2006/04/30/851650.aspx[/url]
1、使用iTextAsian.jar中的字体
2、使用Windows系统字体(TrueType)
3、使用资源字体(ClassPath)
示例代码:
1、使用iTextAsian.jar中的字体
BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
2、使用Windows系统字体(TrueType)
BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
3、使用资源字体(ClassPath)
BaseFont.createFont("/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
示例代码:
ByteArrayOutputStream baos = new ByteArrayOutputStream(OUTPUT_BYTE_ARRAY_INITIAL_SIZE);
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, baos);
writer.setViewerPreferences(PdfWriter.AllowPrinting | PdfWriter.PageLayoutSinglePage);
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
Font font = new Font(bf, 12, Font.NORMAL);
document.open();
Paragraph p = new Paragraph("你好", font);
document.add(p);
document.add(new Paragraph("Test2"));
Table table = new Table(2, 3);
table.addCell(new Phrase("我好", font));
table.addCell("C2R1");
table.addCell("C1R2");
table.addCell("C2R2");
Cell c = (Cell) table.getElement(0, 0);
c.setVerticalAlignment("Middle");
c.setBackgroundColor(new Color(255, 0, 0));
c.setHorizontalAlignment("Center");
document.add(table);
document.close();
baos.writeTo(new FileOutputStream("F:\\test.pdf"));