jacobjar包_java⽂档在线预览Windows版本(jacob)⽂档在线预览有两种实现⽅式:
1. windows server下⽤ jacob
2. linux server下⽤openoffice
1
2
话不多说,看吧。这⾥是使⽤jacob实现的
1. 准备⼀下jar包
下载jacob.zip ,地址:sourceforge/projects/jacob-project/
64位系统就⽤ x64的dll,32位系统就⽤x86的dll。将dll⽂件放⼊放⼊jdk/bin⽬录下,如下图所⽰:
2. 将jacob引⼊到pom中记得把jar包放进去项⽬⾥⾯,如下图所⽰:
<dependency>
<groupId&le.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<dependency>
<groupId>com.jacob</groupId>
<artifactId>1.0.0</artifactId>
<scope>system</scope>
<systemPath>C:/Users/user/.m2/repository/com/jacob/1.0.0/jacob-1.0.0.jar</systemPath>
<!--<systemPath>标签的值就是jacob.jar的具体路径,改成⾃⼰的-->
</dependency>
3. ⽂档转pdf接⼝层
/
/直接调⽤这个⽅法即可
@ApiOperation(value = "⽂档转pdf", notes = "⽂档转pdf")
@RequestMapping(value = "convert2PDF", method = RequestMethod.POST)
public Map convert2PDF(String inputFile, String pdfFile) {
return vert2PDF(inputFile,pdfFile);
}
4. ⽂档转pdf业务层
@Value("${DOCUMENT_URL}")
private String DOCUMENT_URL;
@Value("${UPLOAD_URL}")
private String UPLOAD_URL;
private static final int wdFormatPDF = 17;
private static final int xlTypePDF = 0;
private static final int ppSaveAsPDF = 32;
private static final int msoTrue = -1;
private static final int msofalse = 0;
public Map convert2PDF(String inputFile, String pdfFile) {
Map<String, Object> tResultMap = new HashMap<>();
// 1.得到没有后缀名的⽂字
String fileName = inputFile.substring(0, inputFile.indexOf("."));
//2.拿到要转换的⽂件的全路径()
inputFile = UPLOAD_URL + inputFile;
/
/3.判断传进来的⽂件是否存在,有则下⼀步,没有就返回信息:“⽂件不存在”
String suffix = getFileSufix(inputFile);
File file = new File(inputFile);
if (!ists()) {
System.out.println("⽂件不存在!");
tResultMap.put("status", false);
return tResultMap;
}
//4.拿到要转换⽂件对应的转换后的⽂件的全路径
if (pdfFile == null) {
pdfFile = DOCUMENT_URL + fileName + ".pdf";
} else {
// 设置⽂件存储路径
pdfFile = DOCUMENT_URL + pdfFile + "/" + fileName + ".pdf";
}
//5.将转换后的⽂件全路径去⽂件库查该⽂件是否已经转换过了,如果已经转换过了,直接返回全路径,否则去转换pdf pdfFile = placeAll("\\\\", "/");
File filePdf = new File(pdfFile);
if (ists()) {
String filePdfPath = String().replaceAll("\\\\", "/");
tResultMap.put("msg",filePdfPath); // 如果是pdf⽂件,不⽤转换,直接拿原路径去预览
//tResultMap.put("msg", "为了分清楚是转换的还是直接给出路径的,这⾥是直接给出路径的。");
return tResultMap;
}
//6.转换pdf的⽅法
if (suffix.equals("pdf")) {
System.out.println("PDF not need to convert!");
tResultMap.put("msg", inputFile);
return tResultMap;
return tResultMap;
}
if (suffix.equals("doc") || suffix.equals("docx") || suffix.equals("txt")) {
tResultMap.put("status", word2PDF(inputFile, pdfFile));
pdfFile = placeAll("\\\\", "/");
tResultMap.put("msg", pdfFile);
return tResultMap;
} else if (suffix.equals("ppt") || suffix.equals("pptx")) {
tResultMap.put("status", ppt2PDF(inputFile, pdfFile));
pdfFile = placeAll("\\\\", "/");
tResultMap.put("msg", pdfFile);
return tResultMap;
} else if (suffix.equals("xls") || suffix.equals("xlsx")) {
tResultMap.put("status", excel2PDF(inputFile, pdfFile));
pdfFile = placeAll("\\\\", "/");
tResultMap.put("msg", pdfFile);
return tResultMap;
} else {
System.out.println("⽂件格式不⽀持转换!");
tResultMap.put("status", false);
return tResultMap;
}
}
public static String getFileSufix(String fileName) {
int splitIndex = fileName.lastIndexOf(".");
return fileName.substring(splitIndex + 1);
}
public boolean word2PDF(String inputFile, String pdfFile) {
try {
//打开word应⽤程序
ActiveXComponent app = new ActiveXComponent("Word.Application"); //设置word不可见
app.setProperty("Visible", false);
//获得word中所有打开的⽂档,返回Documents对象
Dispatch docs = Property("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) {
return false;
}
}
}
public boolean excel2PDF(String inputFile, String pdfFile) {
try {
ActiveXComponent app = new ActiveXComponent("Excel.Application");
app.setProperty("Visible", false);
Dispatch excels = Property("Workbooks").toDispatch();
Dispatch excel = Dispatch.call(excels,
"Open",
inputFile,
false,
true
).toDispatch();
Dispatch.call(excel,
"ExportAsFixedFormat",
replaceall()xlTypePDF,
pdfFile
);
Dispatch.call(excel, "Close", false);
app.invoke("Quit");
return true;
} catch (Exception e) {
return false;
}
}
public boolean ppt2PDF(String inputFile, String pdfFile) {
try {
ActiveXComponent app = new ActiveXComponent("PowerPoint.Application");
//app.setProperty("Visible", msofalse);
Dispatch ppts = Property("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;
}
}
5.预览pdf代码(注意了:⽂档在线预览是分为两步的,第⼀步是先将各种⽂档转换成pdf,第⼆步是读pdf)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论