word转pdf
Word文档转换成PDF
工具类代码:
import com.aspose.words.SaveFormat; import java.io.File; import java.io.FileOutputStream; /** * word 转 pdf */ public class Word2Pdf { /** * word转pdf * inpath: 输入word的路径 * outpath: 输出pdf的路径 */ public static String wordTOPdf(String inPath, String outPath,String fileName) throws Exception { if(!new File(outPath).exists()){ new File(outPath).mkdirs(); } // pdf 路径 String pdfPath = outPath + "/" + fileName + ".pdf"; File file = new File(pdfPath); // 生成pdf os FileOutputStream os = new FileOutputStream(file); //解决乱码 //如果是windows执行,不需要加这个 //TODO 如果是linux执行,需要添加这个***** //FontSettings.setFontsFolder("/usr/share/fonts",true); com.aspose.words.Document doc = new com.aspose.words.Document(inPath); //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换 doc.save(os, SaveFormat.PDF); // long now = System.currentTimeMillis(); // System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); return pdfPath; } }
用到的Jar包:
<!-- word转pdf(依赖windows本地的wps) -->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>19.2</version>
</dependency>
[注]:需要手动安装依赖jar包
mvn install:install-file -Dfile=D:\apache-maven-3.3.9\lib\aspose-words-19.2-jdk16.jar -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=19.2 -Dpackaging=jar
-Dfile 后边是jar包的路径,就是刚刚第一步的路径
-DgroupId:这个就是maven引用中的groupId,如下边的那个maven引用
-DartifactId : 这个是artifactId
-Dversion:这个是版本号,如果没有可以自己定义一个,到时候引用的时候会用到
-Dpackaging:这个一般都是jar,因为你引入的就是jar包

浙公网安备 33010602011771号