赞
踩
中文字体
simsun.ttc(宋体和新宋体)
simkai.ttf(楷体)
simhei.ttf(黑体)
simfang.ttf(仿宋体)
TrueType 字体应用
TrueType字体集合的应用
- import java.io.FileOutputStream;
- import com.itextpdf.text.Document;
- import com.itextpdf.text.Font;
- import com.itextpdf.text.Paragraph;
- import com.itextpdf.text.pdf.BaseFont;
- import com.itextpdf.text.pdf.PdfWriter;
-
-
- public class HelloFont {
-
- /**
- * @param args
- */
- public static void main(String[] args) {
- // 1、创建Document对象.
- final Document document = new Document();
- try {
- // 2、创建Writer监听document,并将PDF流指向文件.
- PdfWriter.getInstance(document, new FileOutputStream("D:/data/iText/turorial/helloFont.pdf"));
-
- // 3、开启document
- document.open();
-
- // 4、添加一个段落到document.
- BaseFont baseFont = BaseFont.createFont("D:/data/iText/fonts/simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
- Font font = new Font(baseFont, 32);
- document.add(new Paragraph("你好,这是个新世界。", font));
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- // 5、关闭document
- document.close();
- System.out.println("HelloFont");
- }
-
- }
从classpath路径设置字体
- import java.io.FileOutputStream;
- import org.springframework.core.io.ClassPathResource;
- import com.itextpdf.text.Document;
- import com.itextpdf.text.Font;
- import com.itextpdf.text.Paragraph;
- import com.itextpdf.text.pdf.BaseFont;
- import com.itextpdf.text.pdf.PdfWriter;
-
-
- public class FontDemo {
-
- /**
- * @param args
- */
- public static void main(String[] args) {
- // 1、创建Document对象.
- final Document document = new Document();
- try {
- // 2、创建Writer监听document,并将PDF流指向文件.
- PdfWriter.getInstance(document, new FileOutputStream("D:/data/iText/turorial/fontDemo.pdf"));
-
- // 3、开启document
- document.open();
-
- // 4、添加一个段落到document.
- ClassPathResource classPathResource = new ClassPathResource("/font/simfang.ttf");
- BaseFont baseFont = BaseFont.createFont(classPathResource.getPath(), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
- Font font = new Font(baseFont, 32);
- document.add(new Paragraph("你好,这是个新世界。", font));
- System.out.println("字体路径:" + classPathResource.getFile().getAbsolutePath());
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- // 5、关闭document
- document.close();
- System.out.println("HelloFont");
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。