docx4j word转pdf 中文宋体(中文正文)类型转换乱码

使用docx4j生成pdf时,宋体(中文正文)类型无法转换成功。如何配置支持宋体(中文正文)类型字体?

clipboard.png
转换结果为:

clipboard.png

代码:

String tempPath = "G:\\TestDoc\\weituo2.docx";
 WordprocessingMLPackage wordMLPackage = DocxUtils.getWordMLPackage(tempPath);
 String outpath = "G:\\TestDoc\\业务委托单.pdf";
 DocxUtils.toPDF(wordMLPackage,outpath);
public static void toPDF(WordprocessingMLPackage wordMLPackage,String outPath) throws Exception{
//        Mapper fontMapper = new IdentityPlusMapper();

        Mapper fontMapper = new IdentityPlusMapper();
        wordMLPackage.setFontMapper(fontMapper);
//        PhysicalFont font  = PhysicalFonts.getPhysicalFonts().get("Arial Unicode MS");
//        if (font!=null) {
//            fontMapper.getFontMappings().put("Times New Roman", font);
//        }
        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("simsun-extB"));
        fontMapper.put("仿宋",PhysicalFonts.get("FangSong"));
        fontMapper.put("仿宋_GB2312",PhysicalFonts.get("FangSong_GB2312"));
        fontMapper.put("幼圆",PhysicalFonts.get("YouYuan"));
        fontMapper.put("华文宋体",PhysicalFonts.get("STSong"));
        fontMapper.put("华文中宋",PhysicalFonts.get("STZhongsong"));
        DocxUtils.toP(wordMLPackage,outPath);
    }
public static void toP(WordprocessingMLPackage wordMLPackage,String outPath) throws Exception{
        OutputStream os = new FileOutputStream(outPath);
        FOSettings foSettings = Docx4J.createFOSettings();
        foSettings.setWmlPackage(wordMLPackage);
        Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
    }
阅读 15.3k
2 个回答
新手上路,请多包涵

<dependency>

<groupId>org.docx4j</groupId>
<artifactId>docx4j-export-fo</artifactId>
<version>3.3.0</version>

</dependency>

这个版本

新手上路,请多包涵

楼主找到解决方法了吗?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题