word和excel转pdf

1.下载jacob.jar包  网址:https://sourceforge.net/projects/jacob-project/files/jacob-project/

2.导入到本地仓库:mvn install:install-file -Dfile=[jar包名 ] -DgroupId=[目录a] -DartifactId=[目录b]-Dversion=[版本] -Dpackaging=jar

3.

将红色框文件拷贝到jdk目录下的jre/bin目录中去同时  C:\Windows\System32目录页也要拷贝一份

4.开始编码 工具类

package com.example.mybatis.util;


import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PDFConvert {

private static final Logger logger = LoggerFactory.getLogger(PDFConvert.class);

private static final Integer WORD_TO_PDF_OPERAND = 17;
private static final Integer EXCEL_TO_PDF_OPERAND = 0;

/**
* @Description excel转pdf
* @Param [excelFile, pdfPath]
* @return boolean
**/
public static void excel2PDF(String excelFile, String pdfPath) {
ActiveXComponent ax = null;
Dispatch excel = null;
try {
ComThread.InitSTA();
ax = new ActiveXComponent("Excel.Application");
ax.setProperty("Visible", new Variant(false));
ax.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
Dispatch excels = ax.getProperty("Workbooks").toDispatch();

Object[] obj = new Object[]{
excelFile,
new Variant(false),
new Variant(false)
};
excel = Dispatch.invoke(excels, "Open", Dispatch.Method, obj, new int[9]).toDispatch();

// 转换格式
Object[] obj2 = new Object[]{
new Variant(EXCEL_TO_PDF_OPERAND), // PDF格式=0
pdfPath,
new Variant(0) //0=标准 (生成的PDF图片不会变模糊) ; 1=最小文件
};
Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method,obj2, new int[1]);
} catch (Exception es) {
logger.error("excel转pdf异常:"+excelFile,es);
} finally {
if (excel != null) {
Dispatch.call(excel, "Close", new Variant(false));
}
if (ax != null) {
ax.invoke("Quit", new Variant[] {});
ax = null;
}
ComThread.Release();
}
}

/**
* word转pdf
*
* @param wordlFile
* @param pdfPath
* @return
*/
public static void word2pdf(String wordlFile, String pdfPath) {
ActiveXComponent app = null;
Dispatch doc = null;
try {
ComThread.InitSTA();
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", false);
Dispatch docs = app.getProperty("Documents").toDispatch();
Object[] obj = new Object[]{
wordlFile,
new Variant(false),
new Variant(false),//是否只读
new Variant(false),
new Variant("pwd")
};
doc = Dispatch.invoke(docs, "Open", Dispatch.Method, obj, new int[1]).toDispatch();
// Dispatch.put(doc, "Compatibility", false); //兼容性检查,为特定值false不正确
Dispatch.put(doc, "RemovePersonalInformation", false);
Dispatch.call(doc, "ExportAsFixedFormat", pdfPath, WORD_TO_PDF_OPERAND); // word保存为pdf格式宏,值为17
}catch (Exception es) {
logger.error("word转pdf异常:"+wordlFile,es);
} finally {
if (doc != null) {
Dispatch.call(doc, "Close", false);
}
if (app != null) {
app.invoke("Quit", 0);
}
ComThread.Release();
}
}


}


5.测试类
@Test
public void contextLoads() {

String wordFile = "C:\\Users\\intasect\\Desktop\\测试\\测试.xlsx";
String pdfFile = "C:\\Users\\intasect\\Desktop\\测试\\测试.pdf";
System.out.println("开始转换...");
// 开始时间
long start = System.currentTimeMillis();
PDFConvert.excel2PDF(wordFile,pdfFile );

long end = System.currentTimeMillis();
System.out.println("转换成功,用时:" + (end - start) + "ms");


}
6.结果

 

 
posted @ 2019-08-07 10:23  change丶fl  阅读(439)  评论(0编辑  收藏  举报