项目中遇到的word文档在线预览需求,查阅很多资料决定利用openoffice转换word文档为pdf进行预览实现。
1.下载openoffice4安装 www.openoffice.org;
2.导入相关jar包
3.openoffice服务启动
安装OpenOffice成功后,您可以进入
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
4.openoffice转换文档代码,这是一个工具类,将要预览的文档进行转换
package org.neris.oltp.file.bo.impl;
import java.io.File;
import java.util.regex.Pattern;
import org.springframework.web.bind.annotation.RequestParam;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
//项目部署linux下因为远程服务启动的时候,host=真实的对外IP,不能写127.0.0.1。改成本地机器的IP地址即可。
public class ExchangePdf{
public static String preview(@RequestParam String filePath, @RequestParam String fileName) {
if(!"".equals(filePath)) {
/*1)根据项目所在的服务器环境,确定路径中的 / 和 \ */
String osName = System.getProperty("os.name");
if (Pattern.matches("Linux.*", osName)) {
filePath = "/" + filePath.replace("\\","/");
} else if(Pattern.matches("Windows.*", osName)) {
filePath.replace("/","\\");
}
/*2)获得文件名后缀*/
String ext = "";
if(!"".equals(fileName) && fileName.contains(".")){
ext = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toUpperCase();
}
/*利用openOffice将office文件转换为pdf格式, 然后预览doc, docx, xls, xlsx, ppt, pptx */
if ("DOC".equals(ext) || "DOCX".equals(ext) || "XLS".equals(ext) || "XLSX".equals(ext) || "PPT".equals(ext) || "PPTX".equals(ext)) {
/*filePath在数据库中是不带文件后缀的, 由于jodConverter必须要识别后缀,所以将服务器中的文件重命名为带后缀的文件*/
File docFile = new File(filePath);
/*File docFileWithExt = new File(filePath + "." + ext.toLowerCase()); //带后缀的文件
docFile.renameTo(docFileWithExt);
*/
/*转换之后的文件名*/
File pdfFile;
if(filePath.contains(".")){
pdfFile = new File(filePath.substring(0, filePath.lastIndexOf(".")) + ".pdf");
}else{
pdfFile = new File(filePath + ".pdf");
}
/*判断即将要转换的文件是否真实存在*/
if (docFile.exists()) {
/*判断改文件是否已经被转换过,若已经转换则直接预览*/
if (!pdfFile.exists()) {
/*打开OpenOffice连接,*/
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
//DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
ConverterDocument converterDocument = new ConverterDocument(connection);
converterDocument.convert(docFile, pdfFile);
connection.disconnect();
filePath = pdfFile.getPath(); //文件转换之后的路径
} catch (java.net.ConnectException e) {
e.printStackTrace(); //openoffice 服务未启动
} catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
e.printStackTrace(); //读取转换文件失败
throw e;
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally { //发生exception时, connection不会自动切断, 程序会一直挂着
try{
if(connection != null){
connection.disconnect();
connection = null;
}
}catch(Exception e){}
}
} else {
filePath = pdfFile.getPath(); //文件已经转换过
//response.setContentType("application/pdf");
}
}
}
}
return filePath;
}
}
5:在转换excel时如果列数多的话会出现切行的情况:解决方案
package org.neris.oltp.file.bo.impl;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import com.sun.star.awt.Size;
import com.sun.star.beans.PropertyValue;
import com.sun.star.lang.XComponent;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.view.PaperFormat;
import com.sun.star.view.PaperOrientation;
import com.sun.star.view.XPrintable;
public class ConverterDocument extends StreamOpenOfficeDocumentConverter {
public ConverterDocument(OpenOfficeConnection connection) {
super(connection);
}
public final static Size A5, A4, A3;
public final static Size B4, B5, B6;
public final static Size KaoqinReport;
static {
A5 = new Size(14800, 21000);
A4 = new Size(21000, 29700);
A3 = new Size(29700, 42000);
B4 = new Size(25000, 35300);
B5 = new Size(17600, 25000);
B6 = new Size(12500, 17600);
KaoqinReport = new Size(29700, 27940); //最大限度 宽 1600000
}
@Override
protected void refreshDocument(XComponent document) {
super.refreshDocument(document);
// The default paper format and orientation is A4 and portrait. To
// change paper orientation
// re set page size
XPrintable xPrintable = (XPrintable) UnoRuntime.queryInterface(XPrintable.class, document);
PropertyValue[] printerDesc = new PropertyValue[2];
// Paper Orientation
// printerDesc[0] = new PropertyValue();
// printerDesc[0].Name = "PaperOrientation";
// printerDesc[0].Value = PaperOrientation.PORTRAIT;
// Paper Format
printerDesc[0] = new PropertyValue();
printerDesc[0].Name = "PaperFormat";
printerDesc[0].Value = PaperFormat.USER;
// Paper Size
printerDesc[1] = new PropertyValue();
printerDesc[1].Name = "PaperSize";
printerDesc[1].Value = KaoqinReport;
try {
xPrintable.setPrinter(printerDesc);
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果excel有多个sheet,只能对第一个sheet页面有效,其他sheet无效,如果哪位那位大神解决出来了,请告知。
小白第一次写文章,写的不好,如果有疑惑或建议请留言,谢谢。