尝试过网上能搜到的大部分的转换方式,最后发现只有这种转换方式的效果是我能找到的最好的,如果有更好的,欢迎推荐给我
下载地址:https://download.csdn.net/download/qq_38187437/12820420
注意:此方式只能在windows环境!!!而且必须按照office或者wps!!!服务器上也必须有!
前置:
* 效果最好的一种方法,但是需要 window 环境,而且速度是最慢的
* jacob.jar 放在 E:\jdk1.8.0_14\jre\lib\ext
* jacob.dll 放在 E:\jdk1.8.0_14\jre\bin
1.首先,我这边才用的是把jar包放到自己项目里,我在src平级的目录新建lib目录,放自己的jar包文件 --
jacob.jar
2.然后在pom文件中引入,需要主要jar放置的位置,如果找不到位置报错,请检查位置
<dependency>
<groupId>com.jacob</groupId>
<artifactId>jacob</artifactId>
<version>jacob-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${pom.basedir}/lib/jacob.jar</systemPath>
</dependency>
3.转换工具类
package top.glory.common.utils;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import java.io.File;
/**
* 效果最好的一种方法,但是需要 window 环境,而且速度是最慢的
* 下载地址:http://www.microsoft.com/zh-cn/download/details.aspx?id=7
* jacob 包下载地址:http://sourceforge.net/projects/jacob-project/
* jacob.jar 放在 E:\jdk1.8.0_14\jre\lib\ext
* jacob.dll 放在 E:\jdk1.8.0_14\jre\bin
*
* @author 春秋
*/
public class WordToPdf {
private static final int wdFormatPDF = 17; // PDF 格式
public void wordToPDF(String sfileName, String toFileName) {
System.out.println("启动 Word...");
long start = System.currentTimeMillis();
ActiveXComponent app = null;
Dispatch doc = null;
try {
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
doc = Dispatch.call(docs, "Open", sfileName).toDispatch();
System.out.println("打开文档..." + sfileName);
System.out.println("转换文档到 PDF..." + toFileName);
File tofile = new File(toFileName);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(doc, "SaveAs", toFileName, // FileName
wdFormatPDF);
long end = System.currentTimeMillis();
System.out.println("转换完成..用时:" + (end - start) + "ms.");
} catch (Exception e) {
System.out.println("========Error:文档转换失败:" + e.getMessage());
} finally {
Dispatch.call(doc, "Close", false);
System.out.println("关闭文档");
if (app != null)
app.invoke("Quit", new Variant[]{});
}
// 如果没有这句话,winword.exe进程将不会关闭
ComThread.Release();
}
/**
* @param @param inputFile
* @param @param pdfFile
* @param @return 设定文件
* @return boolean 返回类型
* @throws
* @Title: word2PDF
* @Description: 转换word文档为pdf
*/
public static boolean word2PDF(String inputFile, String pdfFile) {
try {
//打开word应用程序
ActiveXComponent app = new ActiveXComponent("Word.Application");
//设置word不可见
app.setProperty("Visible", false);
//获得word中所有打开的文档,返回Documents对象
Dispatch docs = app.getProperty("Documents").toDispatch();
//调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
Dispatch doc = Dispatch.call(docs,
"Open",
inputFile,
false,
true
).toDispatch();
//调用Document对象的SaveAs方法,将文档保存为pdf格式
/*
Dispatch.call(doc,
"SaveAs",
pdfFile,
wdFormatPDF //word保存为pdf格式宏,值为17
);
*/
Dispatch.call(doc,
"ExportAsFixedFormat",
pdfFile,
wdFormatPDF //word保存为pdf格式宏,值为17
);
//关闭文档
Dispatch.call(doc, "Close", false);
//关闭word应用程序
app.invoke("Quit", 0);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @param @param inputFile
* @param @param pdfFile
* @param @return 设定文件
* @return boolean 返回类型
* @throws
* @Title: excel2PDF
* @Description: 转换excel为PDF
*/
public static boolean excel2PDF(String inputFile, String pdfFile) {
try {
ActiveXComponent app = new ActiveXComponent("Excel.Application");
app.setProperty("Visible", false);
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.call(excels,
"Open",
inputFile,
false,
true
).toDispatch();
/*Dispatch.call(excel,
"ExportAsFixedFormat",
xlTypePDF,
pdfFile
);*/
Dispatch.call(excel, "Close", false);
app.invoke("Quit");
return true;
} catch (Exception e) {
return false;
}
}
/**
* @param @param inputFile
* @param @param pdfFile
* @param @return 设定文件
* @return boolean 返回类型
* @throws
* @Title: ppt2PDF
* @Description: 转换ppt为office
*/
public static boolean ppt2PDF(String inputFile, String pdfFile) {
try {
ActiveXComponent app = new ActiveXComponent("PowerPoint.Application");
//app.setProperty("Visible", msofalse);
Dispatch ppts = app.getProperty("Presentations").toDispatch();
Dispatch ppt = Dispatch.call(ppts,
"Open",
inputFile,
true,//ReadOnly
true,//Untitled指定文件是否有标题
false//WithWindow指定文件是否可见
).toDispatch();
/*Dispatch.call(ppt,
"SaveAs",
pdfFile,
ppSaveAsPDF
);*/
Dispatch.call(ppt, "Close");
app.invoke("Quit");
return true;
} catch (Exception e) {
return false;
}
}
public static void main(String[] args) {
d.wordToPDF("D:\\outFile.docx", "D:\\outFile22.pdf");
}
}