使用FlexPaperView在线阅读PDF
2014-06-04 00:00
162 查看
摘要: 介绍在java项目中使用FlexPaperView在线阅读PDF
需要
FlexPaperViewer.swf
pdf2swf.exe
jsp部分
显示PDF的JSP代码
java后台
FlexPaperView阅读时是调取.swf文件,所以在上传pdf时最好转换一份成swf文件
上传并转换的代码
Pdf2SwfUtil工具类
需要
FlexPaperViewer.swf
pdf2swf.exe
jsp部分
art.dialog.open(getRootPath() + '/contract/contract!lookContractPDF.xhtml?conid='+conid,{title: '合同PDF阅读窗', width: 750, height: "100%"});
显示PDF的JSP代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="com.hwxx.utils.CConst"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> </head> <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/style/flexPaper/flexpaper.css" /> <script type="text/javascript" src="${pageContext.request.contextPath}/script/common/utils.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/script/flexPaper/jquery.min.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/script/flexPaper/flexpaper.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/script/flexPaper/flexpaper_handlers.js"></script> <body> <div id="documentViewer" class="flexpaper_viewer" style="width:100%;height:842px"></div> <script type="text/javascript"> $('#documentViewer').FlexPaperViewer( { config : { //下面为PDF文件的.swf文件的地址 SWFFile :'${pageContext.request.contextPath}'+'/images/swf/'+'${pdfname}.swf', Scale :1, ZoomTransition : 'easeOut', ZoomTime : 0.5, ZoomInterval : 0.2, FitPageOnLoad : true, FitWidthOnLoad : false, FullScreenAsMaxWindow : false, ProgressiveLoading : false, MinZoomSize : 0.2, MaxZoomSize : 5, SearchMatchAll : false, InitViewMode : 'Portrait', RenderingOrder : 'flash', StartAtPage : '', ViewModeToolsVisible : true, ZoomToolsVisible : true, NavToolsVisible : true, CursorToolsVisible : true, SearchToolsVisible : true, WMode : 'window', localeChain: 'en_US' }} ); </script> </body> </html>
java后台
FlexPaperView阅读时是调取.swf文件,所以在上传pdf时最好转换一份成swf文件
上传并转换的代码
/** * 上传合同PDF文件 * @return * @throws IOException */ public void uploadPDF() throws IOException{ String contractid = reqParam("contractid"); String fileInputFileName = reqParam("fileInputFileName"); ActionContext ctx = ActionContext.getContext(); HttpServletResponse sd = ServletActionContext.getResponse(); sd.setContentType("UTF-8"); String path= request.getSession().getServletContext().getRealPath("images/uploadPDF"); File fd=new File(path); if(!fd.exists()){ fd.mkdir(); } try { DateFormat formater = new SimpleDateFormat("yyyyMMddHHmmss"); String picname = formater.format(new Date()); String savefile = null; Contract contract = contractService.findbyid(getInt(contractid)); //保存图片信息 if(file!=null){ savefile = picname + fileInputFileName.substring(fileInputFileName.lastIndexOf(".")); FileUtils.copyFile(file, new File(fd ,savefile)); //把pdf文件转为swf文件 Pdf2SwfUtil.pdf2swf(request, savefile); contract.setPdfname(picname); } //根据conid找出对应合同,写入合同信息 contract.setModifier(getEmpNum()); contract.setModifiertime(DateUtil.getDateNow("yyyy-MM-dd")); recordLog("上传合同PDF文件-[合同编号:"+contract.getContractlabel()+"]"); contractService.update(contract); JsonObject jo = new JsonObject(); jo.addProperty("success",true); jo.addProperty("msg","上传成功"); Gson gson = new Gson(); System.out.println(gson.toJson(jo)); response.setContentType("text/html;charset=utf-8"); response.getWriter().write(gson.toJson(jo)); } catch (IOException e) { JsonObject jo = new JsonObject(); jo.addProperty("success",true); jo.addProperty("msg","上传成功"); Gson gson = new Gson(); System.out.println(gson.toJson(jo)); response.setContentType("text/html;charset=utf-8"); response.getWriter().write(gson.toJson(jo)); e.printStackTrace(); } }
Pdf2SwfUtil工具类
public class Pdf2SwfUtil { /** * 将pdf文件转换为swf文件 * @param name * @return * @throws IOException */ public static boolean pdf2swf(HttpServletRequest request, String name) { try { //获取 pdf文件夹的路径 String pdfPath = getDirectory(request, "images\\uploadPDF"); //获取pdf文件夹中的某个文件路径 String pdfFile = pdfPath.concat("\\" + name); //获取swf文件夹的路径 String swfPath = getDirectory(request, "images\\swf"); //获取swf文件夹中的某个文件路径 String swfFile = swfPath.concat("\\" + name); //调用tools文件夹中的pdf2swf工具 String toolsPath = getDirectory(request, "SWFTools"); Runtime run = Runtime.getRuntime(); Process process = run.exec(toolsPath+ "\\pdf2swf.exe -t " + pdfFile + " -o " + swfFile.replace(".pdf",".swf")+" -T 9 ".replace("Program Files","'Program Files'")); process.waitFor(); return true; } catch (Exception e) { return false; } } /** * 获取某个文件夹的路径 * @param request * @param directoryName * @return */ public static String getDirectory(HttpServletRequest request, String directoryName) { String directory = request.getSession().getServletContext() .getRealPath(directoryName); return directory; } }
相关文章推荐
- iStylePDF在线阅读编辑控件在多浏览器(IE,firefox,chrome,opera等)中的使用
- PDF在线阅读控件多浏览器(IE,firefox,chrome,opera等)中的使用
- 实用的pdf在线阅读和处理工具
- PDF在线阅读任务——任务是动力!
- 推荐:每个人都需要成长,在线阅读《敏捷个人-认识自我,管理自我 V0.5.pdf》
- 实现WORD及PDF文档在线阅读 ASPNET FLEX PAPER
- pdf在线阅读网站生成解决方案
- PB中如何使用PDF阅读控件?
- FlexPaper 使用入门——在线PDF显示
- 黑马程序员--pdf文档在线阅读
- pdf2swf+flexpaper解决pdf在线阅读(类百度文库)
- Sharepoint 使用 Office Web Apps 在线浏览器查看 PDF文件
- PDF在线阅读任务(完成总结篇)
- PDF 在线阅读
- PDF 转 SWF 在线阅读
- 【转载】FlexPaper_1.2.1.swc——Flex在线显示PDF文档(使用FlexPaper)感悟
- 在线PDF阅读&编辑网站一览
- asp.net在线阅读pdf
- PDF文档防扩散以及PDF在线阅读控件的介绍
- 推荐:每个人都需要成长,在线阅读《敏捷个人-认识自我,管理自我 V0.5.pdf》