openoffcie+swftools+flexpaper实现类似百度文库的阅读效果

这是我的处女作,有什么写的不好的还请大家见谅,好了下面开始正题openoffcie+swftools+flexpaper实现类似百度文库的阅读效果:

所用到的包的下载:

OpenOffice 下载地址http://www.openoffice.org/

JodConverter 下载地址http://sourceforge.net/projects/jodconverter/files/JODConverter/,也可以直接从附件里面下载

Swftools下载http://www.swftools.org/download.html

 源代码下载:http://download.csdn.net/detail/zhangwenlong136/4228945

下面是代码的实现部分:

第一步,将office文档转化为pdf文档,代码如下

1,.先安装好openoffce软件,引入JodConverter里面的包

2执行如下代码

import java.io.File;

importjava.io.IOException;

importjava.net.ConnectException;

importjava.util.Date;

importcom.artofsolving.jodconverter.DocumentConverter;

importcom.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;

importcom.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;

importcom.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

/**

 *

 * @author lvzf

 *

 */

public classJOD4DocToPDF extends Thread{  

  //private static final Log log =LogFactory.getLog(JOD4DocToPDF.class);

  private File inputFile;// 需要转换的文件  

    private File outputFile;// 输出的文件  

    private Integer id;

    public JOD4DocToPDF(String sourceFile,String destFile,Integer id) {

           File inputFile = newFile(sourceFile);

           if (!inputFile.exists()){

                   return;//找不到源文件, 则返回-1 

           } 

           // 如果目标路径不存在,则新建该路径 

           File outputFile = newFile(destFile); 

           if(!outputFile.getParentFile().exists()) { 

                   outputFile.getParentFile().mkdirs(); 

           }

        this.inputFile = inputFile;  

        this.outputFile = outputFile; 

        this.id=id;

    }

    public void docToPdf(){

        Date start = new Date();

        String OpenOffice_HOME = "C:\\Program Files\\OpenOffice.org3";//OpenOffice.org 3的存放路径

        if(OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '\\'){ 

                        OpenOffice_HOME +="\\";

        }

        //要执行的cmd代码

        String command = OpenOffice_HOME +"program\\soffice.exe -headless -accept=\"socket," +

                       "host=127.0.0.1,port=8100;urp;\""+ "-nofirststartwizard";

        //连接自己的服务器与端口

        OpenOfficeConnection connection = newSocketOpenOfficeConnection("127.0.0.1",8100);

        try {

               //创建进程调用cmd执行命令

Processpro = Runtime.getRuntime().exec(command);

        connection.connect();

        DocumentConverter converter = newOpenOfficeDocumentConverter(connection);

        converter.convert(inputFile,outputFile);    

        //关闭进程

        pro.destroy();

        } catch (ConnectException e) {

               e.printStackTrace(); 

        } catch (IOException e) {

               e.printStackTrace(); 

        }finally{

               if(connection != null) {

           connection.disconnect();  

           connection = null;  

    }

        }

        long l = (start.getTime() - newDate().getTime());  

        long day = l / (24 * 60 * 60 *1000);  

        long hour = (l / (60 * 60 * 1000) - day* 24);  

        long min = ((l / (60 * 1000)) - day *24 * 60 - hour * 60);  

        long s = (l / 1000 - day * 24 * 60 * 60- hour * 60 * 60 - min * 60);  

        System.out.println("生成" +outputFile.getName() + "耗费:" + min + "分" + s  

                + "秒");  

        //结束转换

       //DocService.getInstance().endConverPdfOrder(id);

    }    

    /** 

     * 由于服务是线程不安全的,所以……需要启动线程 

     */ 

    public void run() {

        this.docToPdf();  

    }  

    public File getInputFile() {  

        return inputFile;  

    }    

    public void setInputFile(File inputFile){  

        this.inputFile = inputFile;  

    }  

    public File getOutputFile() {  

        return outputFile;  

    }  

    public void setOutputFile(File outputFile){  

        this.outputFile = outputFile;  

    }  

    /** 

     * @param args 

     */ 

    public static void main(String[] args){  

           boolean tend=false;

        JOD4DocToPDF tools = newJOD4DocToPDF("F:\\99.doc",

               "F:\\88.pdf",3);

        tools.start();

    }

}

 

第二部,将pdf文档转化为swf

1.先安装swftools

2.执行如下代码

package com.test;

importjava.io.BufferedReader;  

importjava.io.File;  

importjava.io.IOException;  

importjava.io.InputStreamReader;  

/**  

 *  

 * @version 1.0 

 * @author lvzf

 */ 

public class PdfToSwf{  

//finalStringsource=SystemConfig.getInstance().getProperty("PDFTOSWF_PATH");

publicvoid convertPDF2SWF(String fromPath ,String toPath) throws IOException {

Stringcommand = "D:/百度文库软件/swftools/pdf2swf.exe" + " -t " +fromPath + " -o " + toPath + " -s flashversion=9";

System.out.println(command);

Runtime.getRuntime().exec("cmd/c" + command);

}

    private int convertPDF2SWF(StringsourcePath, String destPath,  

            String fileName) throws IOException{  

        // 目标路径不存在则建立目标路径  

        File dest = new File(destPath);  

        if (!dest.exists()) {  

            dest.mkdirs();  

        }  

        // 源文件不存在则返回  

        File source = newFile(sourcePath);  

        if (!source.exists()) {  

            return 0;  

        }  

        // 调用pdf2swf命令进行转换  

        // C:\ProgramFiles\SWFTools>pdf2swf.exe -z -B rfxview.swf -s flashversion=9  

        // d:/人员管理系  

        // 统PersonalManagementSystem简介.pdf -od:/test.swf  

        // 要把D:\\tools\\SWFTools\\放在path里面……不然使用不了播放器  

        // 先生成flash  

        String[] envp = new String[1];  

        envp[0] = "PATH=C:\\Program Files\\SWFTools\\"; 

        //-z -s flashversion=9   -T 9

        String command = "pdf2swf\"" + sourcePath  

                + "\" -o\"" + destPath + fileName + "\"";  

        //Process pro =Runtime.getRuntime().exec(command, envp);  

        //Process pro =Runtime.getRuntime().exec("cmd /c d:\\score.txt"); 

        Process pro =Runtime.getRuntime().exec("/"+source+" d:\\displaytag.pdf -o d:\\displaytag.swf");

        // System.out.println(command);  

        BufferedReader bufferedReader = newBufferedReader(  

                newInputStreamReader(pro.getInputStream()));  

        while (bufferedReader.readLine() !=null) {  

            String text =bufferedReader.readLine();  

            System.out.println(text);  

        }  

        try {  

            pro.waitFor();   

        } catch (InterruptedException e) {  

            // TODO Auto-generated catchblock  

            e.printStackTrace();  

        }  

        // 然后在套播放器  

        /* 

         * swfcombine -z -X 720 -Y 540"D:\tools\SWFTools\swfs\rfxview.swf" 

         * viewport="d:/人 

         *员管理系统PersonalManagementSystem简介.swf" -o"d:/人员管理系统PersonalManagemen 

         * tSystem简介.swf" 

         */ 

//        command = "swfcombine -z -X 720 -Y540 \"C:/Program Files/SWFTools/swfs/rfxview.swf\"viewport=\"" 

//                + destPath + fileName +"\" -o \"" + destPath + fileName +"\"";  

//        pro =Runtime.getRuntime().exec(command, envp);  

//        System.out.println(command);  

//        bufferedReader = new BufferedReader(newInputStreamReader(pro  

//                .getInputStream()));  

//        while (bufferedReader.readLine() !=null) {  

//            String text =bufferedReader.readLine();  

//            System.out.println(text);  

//        }  

//        try {  

//            pro.waitFor();  

//        } catch (InterruptedException e) {  

//            // TODO Auto-generated catchblock  

//            e.printStackTrace();  

//        }  

        return pro.exitValue();  

    }  

    public static void main(String[] args){

        try {  

//               int r=newtest().convertPDF2SWF(sourcePath,  

//                    destPath, fileName);

//            System.out.println(r);

               newPdfToSwf().convertPDF2SWF("F:\\在线文档库.pdf","F:\\0000.swf");

        } catch (IOException e) {  

            // TODO Auto-generated catchblock  

            e.printStackTrace();  

        } 

    }

第三步,在页面中导入flexpaper

具体代码如下

<%@ pagelanguage="java" pageEncoding="UTF-8"%>

<!DOCTYPE htmlPUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"xmlns:xn="http://www.renren.com/2009/xnml"xmlns:og="http://ogp.me/ns#">

<head>

<metahttp-equiv="Content-Type" content="text/html;charset=UTF-8" />

<title>在线文档库</title>

<scripttype="text/javascript"src="js/swfobject/swfobject.js"></script>

<scripttype="text/javascript">

if(window.addEventListener)

window.addEventListener('DOMMouseScroll',handleWheel, false);

window.onmousewheel= document.onmousewheel = handleWheel;

if(window.attachEvent)

window.attachEvent("onmousewheel",handleWheel);

 

functionhandleWheel(event){

try{

if(!window.document.FlexPaperViewer.hasFocus()){returntrue;}

window.document.FlexPaperViewer.setViewerFocus(true);

window.document.FlexPaperViewer            

 

 

        <scripttype="text/javascript">

            <!-- For version detection, setto min. required Flash Player version, or 0 (or 0.0.0), for no versiondetection. -->

            var swfVersionStr ="9.0.124";

            <!-- To use express install, setto playerProductInstall.swf, otherwise the empty string. -->

 

           var status="1";

           //alert(status);

            if(status=="1"){

            var xiSwfUrlStr ="playerProductInstall.swf";

            var flashvars = {

                  SwfFile :escape("0000.swf"),

  Scale : 0.6,

  ZoomTransition : "easeOut",

  ZoomTime : 0.5,

                                   ZoomInterval : 0.1,

                                   FitPageOnLoad : false,

                                   FitWidthOnLoad : true,

                                   PrintEnabled : true,

                                   FullScreenAsMaxWindow : true,

  ProgressiveLoading : 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(

               "FlexPaperViewer.swf", "flashContent",

                "600","500",

                swfVersionStr, xiSwfUrlStr,

                flashvars, params, attributes);

swfobject.createCSS("#flashContent","display:block;text-align:center;");

            }

        </script>

</head>

<body>

 

  <imgsrc="docin_93278439_90x80.jpg"/>

 

 <div class="grid clear">

 

      <div class="doc-player"id="flashContent">

               

<spanid="message">播放器加载中,请稍候...</span>

 </div>

 </div>

</body>

</html>

PS: 在进程中soffice.exe不能关闭 选用如下的方式进行关闭

1.cmd下运行 taskkill /?查看关闭.exe的方法

  /S   system           指定要连接的远程系统。

  /U   [domain\]user   指定应该在哪个用户上下文执行这个命令。

  /P   [password]      为提供的用户上下文指定密码。如果忽略,提示输入

  /FI  filter           应用筛选器以选择一组任务。

                         允许使用"*"。例如,映像名称 eq acme*

  /PID processid        指定要终止的进程的 PID。

                         使用 TaskList 取得 PID。

  /IM  imagename        指定要终止的进程的映像名称。通配符'*'可用来

                         指定所有任务或映像名称。

  /T                     终止指定的进程和由它启用的子进程。

  /F                     指定强制终止进程。

  /?                     显示帮助消息。

 

2.选择运行 taskkill /F /IMsoffice.exe /T 出现下述结果

成功: 已终止 PID 13500 (属于PID 12412 子进程)的进程。

成功: 已终止 PID 12412 (属于PID 4836 子进程)的进程。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值