java实现word文档转为pdf文件
【代码】java实现word文档转为pdf文件。
·
/** * 将doc转换为pdf * * @param docAbsolutedPath 传入word绝对路径 * @param pdfAbsolutedPath 传入生成的pdf绝对路径 * @return */ public String convertWordToPdf(String docAbsolutedPath, String pdfAbsolutedPath) { boolean suc = true; Document document = null; try { document = new Document(docAbsolutedPath); // 增加,测试字体转换时报错的情况,20200406 FontSettings fontSettings = new FontSettings(); // fontSettings.setFontsFolder("C:\\Users\\a\\Desktop\\字体", true); IWarningCallback callback = info -> { // We are only interested in fonts being substituted. if (info.getWarningType() == WarningType.FONT_SUBSTITUTION) { System.out.println("Font substitution: " + info.getDescription()); } }; document.setWarningCallback(callback); // 必须要有这句,否则转pdf时中文会因为字体问题而显示为方块。 // 另外服务器需要上传中文字体到/usr/share/fonts目录(复制windows C:\Windows\Fonts目录下的字体文件即可) document.setFontSettings(fontSettings); document.save(pdfAbsolutedPath); } catch (Exception e) { suc = false; //e.printStackTrace(); } finally { if (document != null) document = null; } //System.out.println("topdf---------------------------------------"+pdfAbsolutedPath); return pdfAbsolutedPath; }
更多推荐
已为社区贡献1条内容
所有评论(0)