注意: 该项目需在windows下进行, 如果需要商用需准备Windows服务器
这里我们用到的工具是jacob 需要创建一个maven项目添加以下依赖
<dependency>
<groupId>net.sf.jacob-project</groupId>
<artifactId>jacob</artifactId>
<version>1.14.3</version>
<scope>compile</scope>
</dependency>
下一步就是配置我们的jacob
1.首先找到我们的jre安装目录, 我这里的目录是 C:\Program Files\Java\jdk1.8.0_241\jre\bin

然后将这两个文件复制到目录中
- jacob-1.14.3-x64.dll
- jacob-1.14.3-x86.dll
jacob配置文件下载地址
链接:https://pan.baidu.com/s/1PLvmjzRu8nbQAOohqcd0uw
提取码:pd1o
完成以上的这些操作就可以直接开始编写代码进行页数获取
package com.yysd.pdfutil.util;
import cn.hutool.core.io.FileUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpUtil;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import org.apache.pdfbox.pdmodel.PDDocument;
import java.io.*;
/**
* @author dell
*/
public class Word2PdfUtil {
// 不保存待定的更改。
static final int wdDoNotSaveChanges = 0;
// word转PDF 格式
static final int wdFormatPDF = 17;
/**
* 测试main方法
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// 需要传入的 word 路径可以是本机 也可以是网络路径
Word2PdfUtil.word2pdf("http://8e2a-60-216-158-158.ngrok.io/aaaa.docx","C:\\Users\\dell\\Desktop\\wordpage\\web.pdf");
// Word2PdfUtil.word2pdf("C:\\Users\\dell\\Desktop\\wordpage\\web.docx","C:\\Users\\dell\\Desktop\\wordpage\\web.pdf");
String fileUrl = "http://6820-58-56-64-2.ngrok.io/aaaa.docx";
//截取发送过来的文件名
System.out.println(fileUrl.substring(fileUrl.lastIndexOf("/") + 1, fileUrl.length() - 1));
//将文件下载后保存在D盘,返回结果为下载文件大小
long size = HttpUtil.downloadFile(fileUrl, FileUtil.file("D:\\file"));
System.out.println("Download size: " + size);
//进行Md5加密
System.out.println("加密后: "+DigestUtil.md5Hex(new FileInputStream("D:\\file\\aaaa.docx")));
// 创建需要解析的PDF文件
File file = new File("C:\\Users\\dell\\Desktop\\wordpage\\web.pdf");
// 加载文件
PDDocument doc = PDDocument.load(file);
// 通过工具类获取页数,打印
System.out.println("页数为 : "+doc.getNumberOfPages());
}
/**
* 工具类
* inputStream 转 File
*/
public static File inputStreamToFile(InputStream ins, String name) throws Exception{
File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);
if (file.exists()) {
return file;
}
OutputStream os = new FileOutputStream(file);
int bytesRead;
int len = 1024;
byte[] buffer = new byte[len];
while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
return file;
}
public static boolean word2pdf(String source, String target) {
System.out.println("Word转PDF开始启动...");
long start = System.currentTimeMillis();
ActiveXComponent app = null;
try {
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", false);
Dispatch docs = app.getProperty("Documents").toDispatch();
System.out.println("打开文档:" + source);
Dispatch doc = Dispatch.call(docs, "Open", source, false, true).toDispatch();
System.out.println("转换文档到PDF:" + target);
File tofile = new File(target);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(doc, "SaveAs", target, wdFormatPDF);
Dispatch.call(doc, "Close", false);
long end = System.currentTimeMillis();
System.out.println("转换完成,用时:" + (end - start)/1000 + "秒");
return true;
} catch (Exception e) {
System.out.println("Word转PDF出错:" + e.getMessage());
return false;
} finally {
if (app != null) {
app.invoke("Quit", wdDoNotSaveChanges);
}
}
}
}
demo项目上传到gitee 地址:
https://gitee.com/programmer-k/pdfutil.git