最近项目有个需求需要将word转为pdf。
网络上有好多种方案:spire,aspose,jacob等,由于jacob需要在jdk添加dll文件,有点束缚,所以在这里就不讲这个方案了
一.spire.doc
1.引入jar包
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
2.wordtopdfutil
public class WordToPdfUtil2 {
public static void word2pdf(String inPath, String outPath) throws Exception {
Document document = new Document();
document.loadFromFile(inPath);
document.saveToFile(outPath, FileFormat.PDF);
}
public static void word2pdf(String inPath, OutputStream outputStream){
Document document = new Document();
document.loadFromFile(inPath);
document.saveToStream(outputStream,FileFormat.PDF);
}
}
3.测试
public static void main(String[] args) {
//aspose
// String docPath = "C:\\Users\\linxishui\\Desktop\\test1.doc";
// String pdfPath = "C:\\Users\\linxishui\\Desktop\\test.pdf";
// WordToPdfUtil.doc2pdf(docPath,pdfPath);
//spire
String docPath2 = "C:\\Users\\linxishui\\Desktop\\test1.doc";
String pdfPath2 = "C:\\Users\\linxishui\\Desktop\\test2.pdf";
try {
WordToPdfUtil2.word2pdf(docPath2,pdfPath2);
} catch (Exception e) {
e.printStackTrace();
}
}
4.效果
word:
pdf:
5.限制
此种方法为免费的,但是限制每次只能打印3页,超过三页后就不给打了,
免费的就是这样的啦,不然人家花钱版的怎么卖的出去嘞,价格6000/年起步,
我这个穷人家是买不起的啦,所以有了下面的一种方法
二.aspose
1.引入jar包
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>18.6</version>
<classifier>jdk16</classifier>
</dependency>
</dependencies>
这里注意:这个jar包如果不是pj后的,而是直接从maven拉下来的话,
虽然没有了页数限制,但是页面顶端会有一行广告水印哦,我这里用的
是pj版本配合license验证使用的
2.wordtopdfutil
public class WordToPdfUtil {
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = Test.class.getClassLoader().getResourceAsStream("\\license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static boolean doc2pdf(String inPath, String outPath) {
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return false;
}
FileOutputStream os = null;
try {
long old = System.currentTimeMillis();
File file = new File(outPath); // 新建一个空白pdf文档
os = new FileOutputStream(file);
Document doc = new Document(inPath); // Address是将要被转化的word文档
doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
long now = System.currentTimeMillis();
System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
} catch (Exception e) {
e.printStackTrace();
return false;
}finally {
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
}
3.测试
public static void main(String[] args) {
public static void main(String[] args) {
//aspose
String docPath = "C:\\Users\\linxishui\\Desktop\\test1.doc";
String pdfPath = "C:\\Users\\linxishui\\Desktop\\test.pdf";
WordToPdfUtil.doc2pdf(docPath,pdfPath);
//spire
// String docPath2 = "C:\\Users\\linxishui\\Desktop\\test1.doc";
// String pdfPath2 = "C:\\Users\\linxishui\\Desktop\\test2.pdf";
// try {
// WordToPdfUtil2.word2pdf(docPath2,pdfPath2);
// } catch (Exception e) {
// e.printStackTrace();
// }
}
4.效果
word:
pdf:
5.限制
这种方法是我目前找到的最实惠好用的一种了,限制就是要找到pj包了,我找了好久,这里我就不传播了
总结:此两种方法都比较简单,好用,而且window,linux版本都适用,如果linux上转换有乱码的话,装一下windows字体就行了
这里推荐一篇装windows字体博客:https://blog.csdn.net/neulily2005/article/details/106003527