ooP - 云代码空间
——
package com.hzh.energymonitoring.util; import java.io.File; import org.artofsolving.jodconverter.OfficeDocumentConverter; import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration; import org.artofsolving.jodconverter.office.OfficeManager; import com.hzh.common.systemconfig.SystemConfigUtils; /** * 这是一个工具类,主要是为了使Office2003-2007全部格式的文档 * (.doc|.docx|.xls|.xlsx|.ppt|.pptx)转化为pdf文件 */ public class OfficeToPDF { public static final String OFFICE_DOC = "doc"; public static final String OFFICE_DOCX = "docx"; public static final String OFFICE_XLS = "xls"; public static final String OFFICE_XLSX = "xlsx"; public static final String OFFICE_PPT = "ppt"; public static final String OFFICE_PPTX = "pptx"; public static final String OFFICE_TO_PDF = "pdf"; /** * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件<br> * * @param inputFilePath * 源文件路径,如:"e:/test.docx" * @param outputFilePath * 目标文件路径,如:"e:/test_docx.pdf" * @return */ public static boolean openOfficeToPDF(String inputFilePath, String outputFilePath) { return office2pdf(inputFilePath, outputFilePath); } /** * 获取OpenOffice.org 3的安装目录 * * @return OpenOffice.org 3的安装目录 */ public static String getOfficeHome() { return SystemConfigUtils.getSystemConfigValue("OPENOFFICE_PATH"); } /** * 连接OpenOffice.org 并且启动OpenOffice.org * * @return */ public static OfficeManager getOfficeManager() { DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration(); // 获取OpenOffice.org 3的安装目录 String officeHome = getOfficeHome(); config.setOfficeHome(officeHome); // 启动OpenOffice的服务 OfficeManager officeManager = config.buildOfficeManager(); officeManager.start(); return officeManager; } /** * 转换文件 * * @param inputFile * @param outputFilePath_end * @param inputFilePath * @param outputFilePath * @param converter */ public static void converterFile(File inputFile, String outputFilePath_end, String inputFilePath, String outputFilePath, OfficeDocumentConverter converter) { File outputFile = new File(outputFilePath_end); // 假如目标路径不存在,则新建该路径 if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } converter.convert(inputFile, outputFile); } /** * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件 * * @param inputFilePath * 源文件路径,如:"e:/test.docx" * @param outputFilePath * 目标文件路径,如:"e:/test_docx.pdf" * @return */ public static boolean office2pdf(String inputFilePath, String outputFilePath) { boolean flag = false; OfficeManager officeManager = getOfficeManager(); // 连接OpenOffice OfficeDocumentConverter converter = new OfficeDocumentConverter( officeManager); if (null != inputFilePath) { File inputFile = new File(inputFilePath); // 判断目标文件路径是否为空 if (null == outputFilePath) { // 转换后的文件路径 String outputFilePath_end = getOutputFilePath(inputFilePath); if (inputFile.exists()) {// 找不到源文件, 则返回 converterFile(inputFile, outputFilePath_end, inputFilePath, outputFilePath, converter); flag = true; } } else { if (inputFile.exists()) {// 找不到源文件, 则返回 converterFile(inputFile, outputFilePath, inputFilePath, outputFilePath, converter); flag = true; } } officeManager.stop(); } else { flag = false; } return flag; } /** * 获取输出文件 * * @param inputFilePath * @return */ public static String getOutputFilePath(String inputFilePath) { String outputFilePath = inputFilePath.replaceAll("." + getPostfix(inputFilePath), ".pdf"); return outputFilePath; } /** * 获取inputFilePath的后缀名,如:"e:/test.pptx"的后缀名为:"pptx" * * @param inputFilePath * @return */ public static String getPostfix(String inputFilePath) { return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1); } }5、pdf转swf的代码
package com.hzh.energymonitoring.util; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import com.hzh.common.systemconfig.SystemConfigUtils; /** * PDF转SWF工具 */ public class PdfToSwf { /** * pdf文件后缀名 */ public static final String FILE_NAME_OF_PDF = "pdf"; /** * swf文件后缀名 */ public static final String FILE_NAME_OF_SWF = "swf"; /** * 获得文件的路径 * * @param file * 文件的路径 ,如:"c:/test/test.swf" * @return 文件的路径 */ public static String getFilePath(String file) { String result = file.substring(0, file.lastIndexOf("/")); if (file.substring(2, 3) == "/") { result = file.substring(0, file.lastIndexOf("/")); } else if (file.substring(2, 3) == "\\") { result = file.substring(0, file.lastIndexOf("\\")); } return result; } /** * 新建一个目录 * * @param folderPath * 新建目录的路径 如:"c:\\newFolder" */ public static void newFolder(String folderPath) { try { File myFolderPath = new File(folderPath.toString()); if (!myFolderPath.exists()) { myFolderPath.mkdir(); } } catch (Exception e) { e.printStackTrace(); } } /** * 转化pdf为swf文件 * * @param sourcePath * pdf文件路径 ,如:"c:/hello.pdf" * @param destPath * swf文件路径,如:"c:/test/test.swf" * @return 正常情况下返回:0,失败情况返回:1 * @throws IOException */ public static int convertPDF2SWF(String sourcePath, String destPath) throws IOException { // 如果目标文件的路径是新的,则新建路径 newFolder(getFilePath(destPath)); // 源文件不存在则返回 File source = new File(sourcePath); if (!source.exists()) { return 0; } String path = SystemConfigUtils.getSystemConfigValue("SWFTOOLS_PATH"); // 调用pdf2swf命令进行转换 String command = path + "/pdf2swf.exe -t \"" + sourcePath + "\" -o \"" + destPath + "\" -s flashversion=9 -s languagedir=D:\\xpdf\\xpdf-chinese-simplified "; // 调用外部程序 Process process = Runtime.getRuntime().exec(command); final InputStream is1 = process.getInputStream(); new Thread(new Runnable() { public void run() { BufferedReader br = new BufferedReader(new InputStreamReader( is1)); try { while (br.readLine() != null) ; } catch (IOException e) { e.printStackTrace(); } } }).start(); // 启动单独的线程来清空process.getInputStream()的缓冲区 InputStream is2 = process.getErrorStream(); BufferedReader br2 = new BufferedReader(new InputStreamReader(is2)); // 保存输出结果流 StringBuilder buf = new StringBuilder(); String line = null; while ((line = br2.readLine()) != null) // 循环等待ffmpeg进程结束 buf.append(line); while (br2.readLine() != null) ; try { process.waitFor(); } catch (InterruptedException e) { e.printStackTrace(); } return process.exitValue(); } /** * pdf文件转换为swf文件操作 * * @param sourcePath * pdf文件路径 ,如:"c:/hello.pdf" * @param destPath * swf文件路径,如:"c:/test/test.swf" * @return */ public static boolean pdf2swf(String sourcePath, String destPath) { boolean flag = false; try { PdfToSwf.convertPDF2SWF(sourcePath, destPath); flag = true; } catch (Exception ex) { flag = false; } return flag; } }6、JSP页面加载swf代码
<%@ page contentType="text/html; charset=utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>showWenKU</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="${pageContext.request.contextPath}/flexpaper/swfobject/swfobject.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/flexpaper/flexpaper.js"></script> <script type="text/javascript"> var swfVersionStr = "9.0.124"; <!-- To use express install, set to playerProductInstall.swf, otherwise the empty string. --> var xiSwfUrlStr = "${expressInstallSwf}"; var flashvars = { SwfFile : escape("${pageContext.request.contextPath}/flexpaper/swf/test_1392283106730_1392283562352.swf?v1.4.0final"), Scale : 0.8, //放大因子,是一个0以上的数(带小数 1 = 100%) 。 ZoomTransition : "easeOut",//光学变焦过渡,默认值是easeOut,可取值: easenone, easeout, linear, easeoutquad ZoomTime : 0.5, //时间过渡让变焦达到新的放大因子,值为0或更大的数。 ZoomInterval : 0.1,//区间的滑动缩放。放大因子缺省值是0.1。如同在工具栏上使用滑动条按钮的效果。 FitPageOnLoad : true, //(布尔) 适合初始页大小(依高度而定)的装载页。如同在工具栏上使用fit-page按钮的效果。 FitWidthOnLoad : true, // (布尔)适合初始页宽度大小的装载页。如同在工具栏上使用fit-width按钮的效果。 PrintEnabled : true, //是否支持打印 FullScreenAsMaxWindow :false, //是否支持全屏 ProgressiveLoading : false, //是否支持延迟加载 SearchMatchAll : true,//设置为true时,单击搜索所有符合条件的地方高亮显示 PrintToolsVisible : false, ViewModeToolsVisible : true,//(布尔)显示或隐藏视图模式与工具栏 ZoomToolsVisible : true,//(布尔) 从工具栏显示或隐藏变焦工具 FullScreenVisible : true,//(布尔)以最大化方式打开一个新浏览器窗口。 NavToolsVisible : true,//(布尔)显示或隐藏导航工具 CursorToolsVisible : false,//(布尔) 显示或隐藏光标工具 SearchToolsVisible : true, localeChain: "en_US" //语言 }; var params = { }; params.quality = "high"; params.bgcolor = "#ffffff"; params.allowscriptaccess = "sameDomain"; params.allowfullscreen = "true"; var attributes = {}; attributes.id = "FlexPaperViewer"; attributes.name = "FlexPaperViewer"; swfobject.embedSWF( "${pageContext.request.contextPath}/flexpaper/swf/FlexPaperViewer.swf", "flashContent", "800", "532", swfVersionStr, xiSwfUrlStr, flashvars, params, attributes); swfobject.createCSS("#flashContent", "display:block;text-align:left;"); </script> </head> <body> <div id="flashContent"></div> </body> </html>页面展示效果如图: