一、目前需求显示附件,要求能将ppt,word,excle,txt这些文件类型的文件显示出来,所以解决的思路:
a) 让客户端自行装换成pdf
b) 使用兼容以上类型的显示插件
c) 将客户端上传的文件在程序转换成pdf格式,然后使用目前比较流行的pdf.js插件来显示
Ps: 第一点,用户体验不佳。第二点,目前没有找到免费支持显示以上文件类型的插件,如果有这种插件的朋友麻烦推荐一下,所以我的解决方案是第三点。
二、需要准备的工具
a) 下载Jacob(可解决doc,word,ppt,excel)
b) 下载IText.jar,ITextAsian.jar
三、实现
a) 首先Jacob中的jacob-1.18-x64.dll放到jdk-bin中
b) 在项目lin中导入jacob.jar
c)PDFUtils.java
package com.mytest.style;
import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
public class PDFUnits {
private static final int wdFormatPDF = 17;
private static final int xlsFormatPDF = 0;
private static final int pptFormatPDF = 32;
private static final int msoTrue = -1;
private static final int msofalse = 0;
public static boolean convert2PDF(String inputFile, String pdfFile) {
String suffix = getFileSufix(inputFile);
File file = new File(inputFile);
if (!file.exists()) {
System.out.println("文件不存在!");
return false;
}
if (suffix.equals("pdf")) {
System.out.println("PDF not need to convert!");
return false;
}
if (suffix.equals("doc") || suffix.equals("docx")) {
return word2PDF(inputFile, pdfFile);
} else if (suffix.equals("ppt") || suffix.equals("pptx")) {
return ppt2PDF(inputFile, pdfFile);
} else if (suffix.equals("xls") || suffix.equals("xlsx")) {
return excel2PDF(inputFile, pdfFile);
} else {
System.out.println("文件格式不支持转换!");
return false;
}
}
private static String getFileSufix(String fileName) {
int splitIndex = fileName.lastIndexOf(".");
return fileName.substring(splitIndex + 1);
}
private static boolean word2PDF(String inputFile, String pdfFile) {
try {
ActiveXComponent app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", false);
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.call(docs, "Open", inputFile, false, true)
.toDispatch();
File tofile = new File(pdfFile);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF
);
Dispatch.call(doc, "Close", false);
app.invoke("Quit", 0);
return true;
} catch (Exception e) {
return false;
}
}
private 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();
File tofile = new File(pdfFile);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(excel, "ExportAsFixedFormat", xlsFormatPDF, pdfFile);
Dispatch.call(excel, "Close", false);
app.invoke("Quit");
return true;
} catch (Exception e) {
return false;
}
}
private static boolean ppt2PDF(String inputFile, String pdfFile) {
try {
ActiveXComponent app = new ActiveXComponent(
"PowerPoint.Application");
Dispatch ppts = app.getProperty("Presentations").toDispatch();
Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, true,// ReadOnly
true,// Untitled指定文件是否有标题
false// WithWindow指定文件是否可见
).toDispatch();
File tofile = new File(pdfFile);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(ppt, "SaveAs", pdfFile, pptFormatPDF);
Dispatch.call(ppt, "Close");
app.invoke("Quit");
return true;
} catch (Exception e) {
return false;
}
}
public static void main(String[] args) {
//OfficeToPdfTools.convert2PDF("c:\\ppt.pptx", "c:\\ppt.pdf");
PDFUnits.convert2PDF("e:\\内容管理构建系统.docx", "e:\\内容管理构建系统.pdf");
//第一个参数是要转化的文件路径,第二个是转化完输出的文件的路劲
//OfficeToPdfTools.convert2PDF("c:\\word.docx", "c:\\word.pdf");
}
}
txt转pdf实现
a) 下载IText.jar,ITextAsian.jar(ITextAsian是对字体显示支持的jar包,也可以引用系统的字体,或者自己下载字体实现)
b)将jar包导入lin
c)实现代码
/**
*
* 判断txt编码格式
*
*/
public static String checkEncoding(int p){
String code = null;
switch (p) {
case 0xefbb:
code = "UTF-8";
break;
case 0xfffe:
code = "Unicode";
break;
case 0xfeff:
code = "UTF-16BE";
break;
default:
code = "GBK";
}
return code;
}
/**
* TXT转PDF
* @throws IOException
* @throws DocumentException
* txtPath 待转文件路路径
* padPath 储存路径
*/
public static boolean txt2pdf(String txtPath, String pdfPath){
Document document = new Document(PageSize.A4);
try {
InputStream is = new FileInputStream(txtPath);
int p = (is.read() << 8) + is.read();
String code = checkEncoding(p);
// 读取文本内容,判断txt文本编码格式,避免乱码问题
InputStreamReader isr = new InputStreamReader(new FileInputStream(txtPath), code);
BufferedReader reader = new BufferedReader(isr);
PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
/**
* 新建一个字体,iText的方法 STSongStd-Light 是字体,在iTextAsian.jar 中以property为后缀
* UniGB-UCS2-H 是编码,在iTextAsian.jar 中以cmap为后缀 H 代表文字版式是 横版, 相应的 V 代表 竖版
*/
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.BLACK);
// 打开文档,将要写入内容
document.open();
String line = reader.readLine();
while (line != null) {
Paragraph pg = new Paragraph(line, fontChinese);
document.add(pg);
line = reader.readLine();
}
document.close();
reader.close();
is.close();
return true;
} catch (Exception e) {
System.out.println("========Error:文档转换失败:" + e.getMessage());
}
return false;
}
txt转pdf需要注意的是:可能会出现中文显示不了的问题,网上有很多解决办法,例如ITextAsian的版本问题,引用字体的问题,等等问题。
但是我都尝试去解决我中文显示不了的问题,以上都无法解决我的问题,最后通过打印输出的文字,发现是乱码。因为txt有他自己的编码格式
所以我们要先加以判断,然后进行编码转换。