|
|
@ -1,7 +1,9 @@ |
|
|
|
package com.yxt.common.base.utils; |
|
|
|
|
|
|
|
import com.jacob.activeX.ActiveXComponent; |
|
|
|
import com.jacob.com.ComThread; |
|
|
|
import com.jacob.com.Dispatch; |
|
|
|
import com.jacob.com.Variant; |
|
|
|
import freemarker.template.Configuration; |
|
|
|
import freemarker.template.Template; |
|
|
|
import freemarker.template.Version; |
|
|
@ -86,13 +88,15 @@ public class WordUtils { |
|
|
|
public static void doc2pdf(String wordFile, String pdfPath, String pdfName) { |
|
|
|
ActiveXComponent app = null; |
|
|
|
System.out.println("开始转换..."); |
|
|
|
long start = System.currentTimeMillis(); |
|
|
|
Dispatch document = null; |
|
|
|
try { |
|
|
|
// 打开word
|
|
|
|
app = new ActiveXComponent("Word.Application"); |
|
|
|
// 获得word中所有打开的文档
|
|
|
|
Dispatch documents = app.getProperty("Documents").toDispatch(); |
|
|
|
// 打开文档
|
|
|
|
Dispatch document = Dispatch.call(documents, "Open", wordFile, false, true).toDispatch(); |
|
|
|
document = Dispatch.call(documents, "Open", wordFile, false, true).toDispatch(); |
|
|
|
// 如果文件存在的话,不会覆盖,会直接报错,所以我们需要判断文件是否存在
|
|
|
|
File targetFile = new File(pdfPath); |
|
|
|
if (!targetFile.exists()) { |
|
|
@ -104,14 +108,21 @@ public class WordUtils { |
|
|
|
target.delete(); |
|
|
|
} |
|
|
|
Dispatch.call(document, "SaveAs", pdfFile, 17); |
|
|
|
// 关闭文档
|
|
|
|
Dispatch.call(document, "Close", false); |
|
|
|
long end = System.currentTimeMillis(); |
|
|
|
System.out.println("转换完成..用时:" + (end - start) + "ms."); |
|
|
|
} catch (Exception e) { |
|
|
|
System.out.println("转换失败" + e.getMessage()); |
|
|
|
} finally { |
|
|
|
// 关闭文档
|
|
|
|
Dispatch.call(document, "Close", false); |
|
|
|
// 关闭office
|
|
|
|
app.invoke("Quit", 0); |
|
|
|
// app.invoke("Quit", 0);
|
|
|
|
System.out.println("关闭文档"); |
|
|
|
if (app != null) |
|
|
|
app.invoke("Quit", new Variant[]{}); |
|
|
|
} |
|
|
|
// 如果没有这句话,winword.exe进程将不会关闭
|
|
|
|
ComThread.Release(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|