項目有個需求,需要在微信企業号中word文檔線上浏覽。找了好多資料,沒找到什麼好辦法。無奈隻能轉換成圖檔顯示。
準備
jacob.jar
office2010
這個隻能是word轉pdf (找過好多插件可以word直接轉圖檔,但是效果不好)
/**
* word轉換pdf
* @param sfileName 源檔案路徑
* @param toFileName 目标檔案路徑
* @param folderPath 目标檔案所在檔案夾路徑
*/
public static void wordToPDF(String sfileName, String toFileName, String folderPath) {
System.out.println("啟動Word...");
ActiveXComponent app = null;
Dispatch doc = null;
try {
app = new ActiveXComponent(ISystemConstant.DICTIONARY_FILE_CONVERT_WORD_START);
app.setProperty(ISystemConstant.DICTIONARY_FILE_CONVERT_WORD_PROPERTY_01, new Variant(false));
Dispatch docs = app.getProperty(ISystemConstant.DICTIONARY_FILE_CONVERT_WORD_PROPERTY_02).toDispatch();
// doc = Dispatch.call(docs, "Open" , sourceFile).toDispatch();
doc = Dispatch.invoke(
docs,
ISystemConstant.DICTIONARY_FILE_CONVERT_WORD_PROPERTY_03,
Dispatch.Method,
new Object[] { sfileName, new Variant(false),
new Variant(true) }, new int[]).toDispatch();
System.out.println("打開文檔..." + sfileName);
System.out.println("轉換文檔到PDF..." + toFileName);
File tofile = new File(toFileName);
if (tofile.exists()) {
tofile.delete();
}
FileOperateHelper.newFolder(folderPath);
// Dispatch.call(doc, "SaveAs", destFile, 17);
Dispatch.invoke(doc, ISystemConstant.DICTIONARY_FILE_CONVERT_WORD_PROPERTY_04, Dispatch.Method, new Object[] {
toFileName, new Variant(WDFORMATPDF) }, new int[]);
} catch (Exception e) {
RmProjectHelper.logError("========Error:文檔轉換失敗:", e);
} finally {
Dispatch.call(doc, ISystemConstant.DICTIONARY_FILE_CONVERT_WORD_PROPERTY_05, false);
System.out.println("關閉文檔");
if (app != null)
app.invoke(ISystemConstant.DICTIONARY_FILE_CONVERT_WORD_PROPERTY_06, new Variant[] {});
// 如果沒有這句話,winword.exe程序将不會關閉
ComThread.Release();
}
/**
* 啟動word程序
*/
public static final String DICTIONARY_FILE_CONVERT_WORD_START = "Word.Application";
/**
* 調用方法
*/
public static final String DICTIONARY_FILE_CONVERT_WORD_PROPERTY_01 = "Visible";
/**
* 調用方法
*/
public static final String DICTIONARY_FILE_CONVERT_WORD_PROPERTY_02 = "Documents";
/**
* 調用方法
*/
public static final String DICTIONARY_FILE_CONVERT_WORD_PROPERTY_03 = "Open";
/**
* 調用方法
*/
public static final String DICTIONARY_FILE_CONVERT_WORD_PROPERTY_04 = "SaveAs";
/**
* 調用方法 關閉
*/
public static final String DICTIONARY_FILE_CONVERT_WORD_PROPERTY_05 = "Close";
/**
* 調用方法 退出
*/
public static final String DICTIONARY_FILE_CONVERT_WORD_PROPERTY_06 = "Quit";
pdf有了 接下來轉換圖檔
public static List<String> pdfToImage(String pdfPath,String imgPath, String rootPath) throws IOException {
FileChannel channel = null;
List<String> list = new ArrayList<String>();
try {
File file = new File(
pdfPath);
RandomAccessFile raf = new RandomAccessFile(file, "r");
channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, , channel
.size());
PDFFile pdffile = new PDFFile(buf);
System.out.println("頁數: " + pdffile.getNumPages());
for (int i = ; i <= pdffile.getNumPages(); i++) {
// draw the first page to an image
PDFPage page = pdffile.getPage(i);
// get the width and height for the doc at the default zoom
Rectangle rect = new Rectangle(, , (int) page.getBBox()
.getWidth(), (int) page.getBBox().getHeight());
// generate the image
Image img = page.getImage(rect.width, rect.height, // width &
// height
rect, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);
BufferedImage tag = new BufferedImage(rect.width, rect.height,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(img, , , rect.width, rect.height,
null);
list.add(imgPath + i + ISystemConstant.DICTIONARY_IMAGE_SUFFIX);
FileOutputStream out = new FileOutputStream(
rootPath + imgPath
+ i + ISystemConstant.DICTIONARY_IMAGE_SUFFIX); // 輸出到檔案流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); // JPEG編碼
out.close();
}
return list;
}finally{
channel.close();
}