docx4j word转pdf解决中文乱码问题(包括宋体(正文))

docx4j 是个非常好用的docx操作包,我主要用来做docx转pdf,但是发现中文会有乱码,原因是因为word里面的中文字体名也是中文的,但是从本地字体库里面拿到的字体名是英文的,所以需要做个映射,这里注意如果是Linux,需要安装中文字体,把windows上的字体复制上去就行。

后来还发现宋体(正文)和宋体(标题)还是乱码,网上也没找到答案,有人问了,但是没人回答,所以只能自己看日志和调试,最后找到了解决办法,如下:

   public static void main(String[] args) throws Exception {
     
        try (FileOutputStream os = new FileOutputStream("D:\\testpdf\\wtp.pdf")) {
     
            WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(new FileInputStream("C:\\Users\\MST\\Downloads\\CgALelzP-j-APRw5AADW_NR3rYI76.docx"));
            Mapper fontMapper = new IdentityPlusMapper();
            fontMapper.put("隶书", PhysicalFonts.get("LiSu"));
            fontMapper.put("宋体", PhysicalFonts.get("SimSun"));
            fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei"));
            fontMapper.put("黑体", PhysicalFonts.get("SimHei"));
            fontMapper.put("楷体", PhysicalFonts.get("KaiTi"));
            fontMapper.put("新宋体", PhysicalFonts.get("NSimSun"));
            fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
            fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));
            fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
            fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));
            fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));
            fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));
            fontMapper.put("等线", PhysicalFonts.get("SimSun"));
            fontMapper.put("等线 Light", PhysicalFonts.get("SimSun"));
            fontMapper.put("华文琥珀", PhysicalFonts.get("STHupo"));
            fontMapper.put("华文隶书", PhysicalFonts.get("STLiti"));
            fontMapper.put("华文新魏", PhysicalFonts.get("STXinwei"));
            fontMapper.put("华文彩云", PhysicalFonts.get("STCaiyun"));
            fontMapper.put("方正姚体", PhysicalFonts.get("FZYaoti"));
            fontMapper.put("方正舒体", PhysicalFonts.get("FZShuTi"));
            fontMapper.put("华文细黑", PhysicalFonts.get("STXihei"));
            fontMapper.put("宋体扩展",PhysicalFonts.get("simsun-extB"));
            fontMapper.put("仿宋_GB2312",PhysicalFonts.get("FangSong_GB2312"));
            fontMapper.put("新細明體",PhysicalFonts.get("SimSun"));
            //解决宋体(正文)和宋体(标题)的乱码问题
            PhysicalFonts.put("PMingLiU", PhysicalFonts.get("SimSun"));
            PhysicalFonts.put("新細明體", PhysicalFonts.get("SimSun"));
            mlPackage.setFontMapper(fontMapper);

            FOSettings foSettings = Docx4J.createFOSettings();
            foSettings.setWmlPackage(mlPackage);
            Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
        }
    }

你可能感兴趣的:(java)