Itext.jar 的版本是 5.0.5
往 c:/test.pdf 文件写入中文
- Document doc = new Document();
- FileOutputStream out = new FileOutputStream(new File("C:/test.pdf"));
- PdfWriter.getInstance(doc, out);
- //文字com/lowagie/text/pdf/fonts/
- // BaseFont chinese = BaseFont.createFont("STSong-Light",
- // "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
- // BaseFont chinese = BaseFont.createFont("C:/windows/fonts/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
- BaseFont chinese = BaseFont.createFont(PdfTest.class.getResource("/") + "/simsun.ttc,1",
- BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
- Font fontChinese = new Font(chinese, 12, Font.NORMAL);
- Paragraph text = new Paragraph("你好。。。。",fontChinese);
- doc.open(); // 打开文档
- doc.add(text);
- doc.add(text);
- doc.close();
BaseFont chinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
若 BaseFont 如此定义 则会报一个
com.itextpdf.text.DocumentException: Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized.
原因: 以及解决方案: (摘自网上未测试.....)
http://cst.is-programmer.com/posts/18844.html
Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized
还是不行,继续研究,在网上前辈们说如下原因:
iText5.x版本以上中的font和encoding文件都是从String RESOURCE_PATH = "com/itextpdf/text/pdf/fonts/"加载的,而老itextasian.jar的包名是com.lowagie.text.pdf.fonts, 包名不一致导致路径错误,。 |
具体解决方法就是修改包的路径了,详细方法如下:
1.解压iTextAsian.jar 得到如下目录: iTextAsian --com --lowagie --text --fonts --...(字体属性文件) 2.将解压后的com目录下的包名lowagie更改为itextpdf 3.在命令行转至iTextAsian目录,重新打包为iTextAsian.jar文件 4.打包命令如下: jar cvf iTextAsian.jar com/itextpdf/text/pdf/fonts/* 5.执行后,将新的iTextAsian.jar加入classpath路径 |
运行结果,OK,解决问题。
===========================
可以使用系统自带的字体
- BaseFont chinese = BaseFont.createFont("C:/windows/fonts/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
或者 将simsun.ttc复制到 src 目录下面;
- BaseFont chinese = BaseFont.createFont(PdfTest.class.getResource("/") + "/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Itext.jar 的版本是 5.0.5
往 c:/test.pdf 文件写入中文
- Document doc = new Document();
- FileOutputStream out = new FileOutputStream(new File("C:/test.pdf"));
- PdfWriter.getInstance(doc, out);
- //文字com/lowagie/text/pdf/fonts/
- // BaseFont chinese = BaseFont.createFont("STSong-Light",
- // "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
- // BaseFont chinese = BaseFont.createFont("C:/windows/fonts/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
- BaseFont chinese = BaseFont.createFont(PdfTest.class.getResource("/") + "/simsun.ttc,1",
- BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
- Font fontChinese = new Font(chinese, 12, Font.NORMAL);
- Paragraph text = new Paragraph("你好。。。。",fontChinese);
- doc.open(); // 打开文档
- doc.add(text);
- doc.add(text);
- doc.close();
BaseFont chinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
若 BaseFont 如此定义 则会报一个
com.itextpdf.text.DocumentException: Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized.
原因: 以及解决方案: (摘自网上未测试.....)
http://cst.is-programmer.com/posts/18844.html
Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized
还是不行,继续研究,在网上前辈们说如下原因:
iText5.x版本以上中的font和encoding文件都是从String RESOURCE_PATH = "com/itextpdf/text/pdf/fonts/"加载的,而老itextasian.jar的包名是com.lowagie.text.pdf.fonts, 包名不一致导致路径错误,。 |
具体解决方法就是修改包的路径了,详细方法如下:
1.解压iTextAsian.jar 得到如下目录: iTextAsian --com --lowagie --text --fonts --...(字体属性文件) 2.将解压后的com目录下的包名lowagie更改为itextpdf 3.在命令行转至iTextAsian目录,重新打包为iTextAsian.jar文件 4.打包命令如下: jar cvf iTextAsian.jar com/itextpdf/text/pdf/fonts/* 5.执行后,将新的iTextAsian.jar加入classpath路径 |
运行结果,OK,解决问题。
===========================
可以使用系统自带的字体
- BaseFont chinese = BaseFont.createFont("C:/windows/fonts/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
或者 将simsun.ttc复制到 src 目录下面;
- BaseFont chinese = BaseFont.createFont(PdfTest.class.getResource("/") + "/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);