在pom.xml中引入依赖
<!-- 方案:使用spire.doc 免费版有限制3页 word转pdf -->
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>5.2.0</version>
</dependency>
<!-- 方案:使用spire.doc 免费版有限制3页 word转pdf -->
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
spire是收费版,免费版只能转换3页
代码:
import com.spire.doc.*;
public class Spire2PdfUtil {
public static void main(String[] args) {
String sourcePath = "/Users/Downloads/d12aa77e34df992598ee8b9e5d5278b6.docx";
String targetPath = "/Users/Downloads/123-5-spire.pdf";
//实例化Document类的对象
Document doc = new Document();
//加载Word
doc.loadFromFile(sourcePath);
// 总页数
int pageCount = doc.getPageCount();
System.out.println(pageCount);
//保存为PDF格式
doc.saveToFile(targetPath,FileFormat.PDF);
}
}
转换效果还是比较好的,也比较快,只是要收费