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

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

转换结果:

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);
    }




举报
ryan_yuan
发帖于2年前 1回/1K+阅
顶部