java word文档转换PDF文件
使用jacob 插件
链接: https://pan.baidu.com/s/1un6Hb2P4y3OhI3iHOgldrg 密码: j5v8
这是jacob 插件下载地方 有需要的可以自行下载
使用:
首先 将文件中
jacob-1.19-x86.dll 、jacob-1.19-x64.dll 文件放到jdk bin目录下,用户调用时启动插件功能
Java代码中如下:
package com.csnt.hbzpt.Helpper;
import com.csnt.hbzpt.action.ComplaintHistory.ComplainFlowInfoSaveAction;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.smartdp.components.tracer.TracerFactory;
import com.smartdp.components.tracer.interfaces.Tracer;
import java.io.File;
//import org.xhtmlrenderer.pdf.ITextRenderer;/
/**
* Created by Administrator on 2018/9/12.
* word文档转换pdf
*/
public class PdfGenerateHelper {
static final int wdFormatPDF = 17;// PDF 格式
public String wordToPDF(String docFileName, boolean removeOrgFile){
String urlPdf="";
long start = System.currentTimeMillis();
ActiveXComponent app = null;
Dispatch doc = null;
try {
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", new Variant(false));
Dispatch docs = (Dispatch) app.getProperty("Documents").toDispatch();
String pdfName = docFileName.substring(docFileName.lastIndexOf("/")+1,docFileName.lastIndexOf(".doc"));
String path=docFileName.substring(0,docFileName.lastIndexOf("/")+1);
String toFileName = path+ pdfName + ".pdf";
urlPdf=toFileName;
doc = Dispatch.call(docs, "Open" , docFileName).toDispatch();
Dispatch.call(doc,
"SaveAs",
toFileName, // FileName
wdFormatPDF);
long end = System.currentTimeMillis();
System.out.println("转换完成..用时:" + (end - start) + "ms.");
tracer.logInfo("转换完成..用时:" + (end - start) + "ms.");
} catch (Exception e) {
tracer.logInfo("========Error:文档转换失败:" + e.getMessage());
System.out.println("========Error:文档转换失败:" + e.getMessage());
} finally {
Dispatch.call(doc,"Close",false);
System.out.println("关闭文档");
tracer.logInfo("关闭文档");
if (app != null) {
app.invoke("Quit", new Variant[] {});
}
File orgFile = new File(docFileName);
//转换完成之后是否要删除word 看自己需求
// if (orgFile.exists()) {
// orgFile.delete();
// }
}
//如果没有这句话,winword.exe进程将不会关闭
ComThread.Release();
return urlPdf;
}
public static void main(String[] args) {
//调用测试
PdfGenerateHelper pdf = new PdfGenerateHelper();
String name="C:\\Users/dell/Desktop/test/test.doc";
pdf.wordToPDF(name,false);
}
}

浙公网安备 33010602011771号