package test; import java.awt.image.BufferedImage; import java.awt.image.RenderedImage; import java.io.File; import java.io.IOException; import java.net.ConnectException; import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; import org.apache.commons.io.FilenameUtils; import org.icepdf.core.pobjects.Document; import org.icepdf.core.pobjects.Page; import org.icepdf.core.util.GraphicsRenderingHints; import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry; import com.artofsolving.jodconverter.DocumentConverter; import com.artofsolving.jodconverter.DocumentFormat; import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; public class FileToImgUtil { private static OpenOfficeConnection startOpenOffice(){ //OpenOffice的安装目录,linux环境下需要手动启动openoffice服务 String OpenOffice_HOME = "D:\\Apache-OpenOffice-3.0\\openOffice.org\\OpenOffice.org 3\\"; // 启动OpenOffice的服务 String command = OpenOffice_HOME+ "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\""; try { Process pro = Runtime.getRuntime().exec(command); } catch (IOException e) { e.printStackTrace(); } //创建连接 OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100); return connection; } private static void doc2Pdf(String docPath, String pdfPath) throws ConnectException { File inputFile = new File(docPath); File outputFile = new File(pdfPath); OpenOfficeConnection connection = startOpenOffice(); connection.connect(); DocumentConverter converter = new OpenOfficeDocumentConverter(connection); //converter.convert(inputFile, outputFile); DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry(); DocumentFormat txt = formatReg.getFormatByFileExtension("odt") ; DocumentFormat pdf = formatReg.getFormatByFileExtension("pdf") ; converter.convert(inputFile, txt, outputFile, pdf); connection.disconnect(); } /** * 把ppt word excel等文件生成图片文件 * @param docPath 文件路径 * @param imgDirPath 图片保存文件夹 * @param fileName 文件名称点的前部分 */ public static void doc2Imags(String docPath, String imgDirPath,String fileName){ String pdfPath =String.format("%s%s.pdf", FilenameUtils.getFullPath(docPath), FilenameUtils.getBaseName(docPath)); try { doc2Pdf(docPath, pdfPath); pdf2Imgs(pdfPath, imgDirPath,fileName); File pdf = new File(pdfPath); /*if(pdf.isFile()){ pdf.delete(); }*/ System.out.println(pdfPath); } catch (ConnectException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /** * 将pdf转换成图片 * * @param pdfPath * @param imagePath * @return 返回转换后图片的名字 * @throws Exception */ private static List<String> pdf2Imgs(String pdfPath, String imgDirPath,String fileName) throws Exception { Document document = new Document(); document.setFile(pdfPath); float scale = 2f;//放大倍数 float rotation = 0f;//旋转角度 List<String> imgNames = new ArrayList<String>(); int pageNum = document.getNumberOfPages(); File imgDir = new File(imgDirPath); if (!imgDir.exists()) { imgDir.mkdirs(); } for (int i = 0; i < pageNum; i++) { BufferedImage image = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX, rotation, scale); RenderedImage rendImage = image; try { String filePath = imgDirPath + File.separator +fileName+i + ".jpg"; File file = new File(filePath); ImageIO.write(rendImage, "jpg", file); imgNames.add(FilenameUtils.getName(filePath)); } catch (IOException e) { e.printStackTrace(); return null; } image.flush(); } document.dispose(); return imgNames; } public static void main(String[] args) { //String docPath = "d:/94_storage安装.doc"; String docPath = "C:\\Users\\John\\Desktop\\测试文件3.xlsx"; String pdfPath = "C:\\Users\\John\\Desktop\\"; doc2Imags(docPath, pdfPath,"公司通讯录"); } }
注:需要注意jar包的问题,会各种jar的兼容性问题,下面的linux环境中测试已给出作者亲自测试的正常jar包
linux下通过直接运行class文件
java -classpath .:./jodconverter-2.2.1.jar:./icepdf-core-6.1.3.jar:./juh-2.3.0.jar:./jurt-2.3.0.jar:./ridl-2.3.0.jar: ./slf4j-api-1.7.25.jar:./commons-io-2.6.jar:./commons-net-3.3.jar:./unoil-2.3.0.jar com/yuzi/utils/Doc2HtmlUtil_test /opt/doc/12344.xlsx /opt/doc/12345.pdf 123
openoffice的启动方式(linux中&表示后台启动)
1、安装OpenOffice4(该版本较稳定,亲自测试):下载路径:http://www.openoffice.org/download/index.html 2、用以下命令启动OpenOffice服务 windows下: cd D:\Program Files (x86)\OpenOffice 4\program soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard linux下: cd /opt/openoffice4/program ./soffice "-accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager" -nologo -headless -nofirststartwizard &
博客:www.sudo.ren