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); }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
//加载字体文件(解决linux环境下无中文字体问题)
PhysicalFonts.addPhysicalFonts("SimSun", new URL("simsun.ttc"));
//宋体&新宋体
PhysicalFont simsunFont = PhysicalFonts.get("SimSun");
fontMapper.put("SimSun", simsunFont);
//自己准备字体库