java利用iTextWorker生成pdf

 


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

 public static class PdfFont extends XMLWorkerFontProvider{
        public PdfFont(){
            super(null,null);
        }
        @Override
        public Font getFont(final String fontname, String encoding, float size, final int style) {

            Font FontChinese = null;
            try {
                BaseFont bfChinese = BaseFont.createFont("STSong-Light",
                        "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
                FontChinese = new Font(bfChinese, 12, Font.NORMAL);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(FontChinese==null)
                FontChinese = super.getFont(fontname, encoding, size, style);
            return FontChinese;
        }}

 

posted @ 2017-10-17 23:56  CoderQiang  阅读(177)  评论(0编辑  收藏  举报