一、目前需求顯示附件,要求能將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有他自己的編碼格式
所以我們要先加以判斷,然後進行編碼轉換。