仿豆丁百度文库网页版阅读器完整解决方案

 

 

 

Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用openoffice转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。

此过程涉及软件(实测)

openoffice3.2(window)

swftools-2011-01-23-1815.exe(window)

openoffice3.1.1(linux)

swftools-2011-01-23-1815.tar.gz(linux)

jar包jodconverter-2.2.2(里面lib目录下含多个要用到的jar文件)

flexpaper1.4.0 (直接复制过来,项目上运行成功,本地运行还没成功)

FlexPaper_1.4.5_flash(网上下载的最新版本没试过,二选一即可)

附上自己写的java转换Demo DocConverter.java

importjava.io.BufferedInputStream;

importjava.io.File;

importjava.io.IOException;

importjava.io.InputStream;

 

importcom.artofsolving.jodconverter.DocumentConverter;

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

import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;

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

 

 

/**

 * doc docx格式转换

 * @author Administrator

 *

 */

public classDocConverter {

    private static final int environment = 1;//环境 1:windows2:linux (只涉及pdf2swf路径问题)

    privateString fileString;

    privateString outputPath = "";//输入路径 ,如果不设置就输出在默认的位置

    privateString fileName;

    private FilepdfFile;

    private FileswfFile;

    private FiledocFile;

   

    publicDocConverter(String fileString) {

       ini(fileString);

    }

   

    /**

     * 重新设置file

     * @param fileString

     */

    public voidsetFile(String fileString){

       ini(fileString);

    }

   

    /**

     * 初始化

     * @param fileString

     */

    private voidini(String fileString) {

       this.fileString =fileString;

       fileName =fileString.substring(0, fileString.lastIndexOf("."));

       docFile = newFile(fileString);

       pdfFile = newFile(fileName + ".pdf");

       swfFile = newFile(fileName + ".swf");

    }

 

    /**

     * 转为PDF

     * @param file

     */

    private voiddoc2pdf() throws Exception{

       if(docFile.exists()){

           if(!pdfFile.exists()){

              OpenOfficeConnection connection = newSocketOpenOfficeConnection(8100);

              try {

                  connection.connect();

                  DocumentConverter converter = newOpenOfficeDocumentConverter(connection);  

                  converter.convert(docFile, pdfFile); 

                  // close theconnection  

                  connection.disconnect();

                  System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath()+ "****");

              } catch(java.net.ConnectException e) {

                  // TODOAuto-generated catch block

                  e.printStackTrace();

                  System.out.println("****swf转换器异常,openoffice服务未启动!****");

                  throw e;

              } catch(com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {

                  e.printStackTrace();

                  System.out.println("****swf转换器异常,读取转换文件失败****");

                  throw e;

              } catch(Exception e){

                  e.printStackTrace();

                  throw e;

              }

           } else {

              System.out.println("****已经转换为pdf,不需要再进行转化****");

           }

       } else {

           System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");

       }

    }

   

    /**

     * 转换成 swf

     */

    private voidpdf2swf() throws Exception{

       Runtime r = Runtime.getRuntime();

       if(!swfFile.exists()){

           if(pdfFile.exists()){

              if(environment ==1){//windows环境处理

                  try {

                     Process p = r.exec("D:/TOOLS/SWFTools/pdf2swf.exe" +

                            pdfFile.getPath()+ " -o " + swfFile.getPath()+ " -T 9");

                     System.out.print(loadStream(p.getInputStream()));

                     System.err.print(loadStream(p.getErrorStream()));

                     System.out.print(loadStream(p.getInputStream()));

                     System.err.println("****swf转换成功,文件输出:" + swfFile.getPath()+ "****");

                     if(pdfFile.exists()){

                         pdfFile.delete();

                     }

                    

                  } catch(IOException e) {

                     e.printStackTrace();

                     throw e;

                  }

              } else if(environment ==2){//linux环境处理

                  try {

                     Process p = r.exec("pdf2swf" +

                            pdfFile.getPath()+ " -o " + swfFile.getPath()+ " -T 9");

                     System.out.print(loadStream(p.getInputStream()));

                     System.err.print(loadStream(p.getErrorStream()));

                     System.err.println("****swf转换成功,文件输出:" + swfFile.getPath()+ "****");

                     if(pdfFile.exists()){

                         pdfFile.delete();

                     }

                  } catch(Exception e) {

                     e.printStackTrace();

                     throw e;

                  }

              }

           } else {

              System.out.println("****pdf不存在,无法转换****");

           }

       } else {

           System.out.println("****swf已经存在不需要转换****");

       }

    }

 

    staticString loadStream(InputStream in) throwsIOException {

 

       int ptr= 0;

       in = newBufferedInputStream(in);

       StringBuffer buffer = newStringBuffer();

 

       while((ptr = in.read()) != -1 ) {

       buffer.append((char)ptr);

       }

 

       returnbuffer.toString();

    }

   

   

    /**

     * 转换主方法

     */

    public booleanconver() {

      

       if(swfFile.exists()){

           System.out.println("****swf转换器开始工作,该文件已经转换为swf****");

           return true;

       }

      

       if(environment ==1){

           System.out.println("****swf转换器开始工作,当前设置运行环境windows****");

       } else {

           System.out.println("****swf转换器开始工作,当前设置运行环境linux****");

       }

       try {

           doc2pdf();

           pdf2swf();

       } catch(Exception e) {

           // TODOAuto-generated catch block

           e.printStackTrace();

           return false;

       }

      

       if (swfFile.exists()){

           return true;

       } else {

           return false;

       }

    }

   

    /**

     * 返回文件路径

     * @param s

     */

    publicString getswfPath(){

       if(swfFile.exists()){

           String tempString = swfFile.getPath();

           tempString = tempString.replaceAll("\\\\", "/");

           returntempString;

       } else {

           return "";

       }

      

    }

   

    /**

     * 设置输出路径

     */

    public voidsetOutputPath(String outputPath){

       this.outputPath =outputPath;

       if(!outputPath.equals("")){

           String realName = fileName.substring(fileName.lastIndexOf("/"), fileName.lastIndexOf("."));

           if(outputPath.charAt(outputPath.length())== '/'){

              swfFile = newFile(outputPath + realName + ".swf");

           } else {

              swfFile = newFile(outputPath + realName + ".swf");

           }

       }

    }

   

    public static voidmain(String s[]) {

       DocConverter d = newDocConverter("D:/data/FTP/midlongforecast/midweatherforecast/20010202.xml");

       d.conver();

    }  

}

 

-------------------------------------------------------------------------------------------------------------------------------------------------------
 

以上即是所有用到的转换需要用到的文件

 

windows安装过程省略,linux下安装swftools命令

解压到目录后

./configure

make

make install

 

无论在哪个平台安装完以上两个软件后

需要在将openoffice以服务方式启动

Window 启动方式

cd C:\ProgramFiles\OpenOffice.org 3\program
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"–nofirststartwizard

Linux启动

soffice-headless -accept="socket,host=127.0.0.1,port=8100;urp;"-nofirststartwizard &

 

 

以上是实现两个平台下的转换,但是会有openoffice解析不了的字体乱码问题,或者显示字体变形等

更高级的web文档应用可以参考QQ邮件,邮件实现了在线预览excel doc pdf等格式文件并可以用鼠标点击选取复制,期待高手解决.

你可能感兴趣的:(windows,linux,职场,休闲)