首页 > 其他 > 详细

生成PDF,中文失踪问题

时间:2015-02-15 15:02:54      阅读:1820      评论:0      收藏:0      [点我收藏+]

使用itext生成pdf,在linux环境下,中文全部失踪,因为itext要在linux下支持中文字体需要引入itext-asian,并添加一个字体类。

字体类

 1 import com.itextpdf.text.Font;
 2 import com.itextpdf.text.pdf.BaseFont;
 3 import com.itextpdf.tool.xml.XMLWorkerFontProvider;
 4 
 5 public class PdfFont extends XMLWorkerFontProvider{
 6     public PdfFont(){
 7         super(null,null);
 8     }
 9     @Override
10     public Font getFont(final String fontname, String encoding, float size, final int style) {
11                                                                                            
12         Font FontChinese = null;
13         try {
14             BaseFont bfChinese = BaseFont.createFont("STSong-Light",
15                     "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
16             FontChinese = new Font(bfChinese, 12, Font.NORMAL);
17         } catch (Exception e) {
18             e.printStackTrace();
19         }
20         if(FontChinese==null)
21             FontChinese = super.getFont(fontname, encoding, size, style);
22         return FontChinese;
23     }
24 }

生成pdf

1 Document document = new Document(PageSize.A4, 50, 50, 50, 50);
2             PdfWriter writer = PdfWriter.getInstance(document, bos);
3             document.open();
4 
5             String is = "中文test123";
7             InputStream streamTemp = new ByteArrayInputStream(is.getBytes());
8             XMLWorkerHelper.getInstance().parseXHtml(writer, document,
9                     streamTemp, null,Charset.forName("UTF-8"),new PdfFont());

 

生成PDF,中文失踪问题

原文:http://www.cnblogs.com/joann/p/4292862.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!