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);
                                //加载字体文件(解决linux环境下无中文字体问题)

        PhysicalFonts.addPhysicalFonts("SimSun", new URL("simsun.ttc"));
        //宋体&新宋体
        PhysicalFont simsunFont = PhysicalFonts.get("SimSun");

        fontMapper.put("SimSun", simsunFont);

//自己准备字体库

评论 (0)