doc、pdf转换为swf文件预览

一、安装openoffice.org

主要模块有writer(文本文档),impress(演示文稿),Calc(电子表格),Draw(绘图),Math(公式),base(数据库)

笔者下载的是openoffice.org 3.3.0。下载完直接安装即可。

 

     但是,我们还需要启动openoffice server。有两种做法:

    1.以命令行方式启动openoffice server,缺点是每次系统重启,都需要手动去把openoffice server启动。

    2.openoffice server作为操作系统的服务启动,既然成为了系统服务,就可以设定开机自动启动了。

    我们先来看第一种方式,

1.以命令行方式启动openoffice server

  在cmd命令下,cd opeonofiice的安装路径/program如:cd c:\program files\openoffice.org 3\program\soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

 

2.以系统服务的方式启动

    这里我们还需要Windows Resource Kit tools,将openoffice server设为系统服务。

Windows Resource Kit tools 是微软专为管理人员、开发人员和高级用户开发的,包括管理活动目录、组策略、TCP/IP网络、注册表、系统安全、监测等涉及Windows Server 2003 操作系统的其它很多方面的非常规安装的工具组件。Resource Kit Tools for XP的发布使得XP用户也能使用Resource Kit Tools对这些问题进行处理。

    下载windows resource kit tools,我们进行默认安装。

     1.打开Windows Resource Kit Tools

     Command Shell执行以下命令:

 "C:\Program Files\Windows Resource Kits\Tools\instsrv" OpenOfficeUnoServer "C:\Program Files\Windows Resource Kits\Tools\srvany.exe"

打开管理工具->服务可以找到以 OpenOfficeUnoServer命名的服务

    2.打开注册表寻找以下路径

    HKEY_LOCAL_MACHINE -> SYSTEM ->ControlSet001 ->Services ->OpenOfficeUnoServer

  新建项 Parameters,在该项下添加两个字符串值:

  key:Application

     value:C:\Program Files\OpenOffice.org 3\program\soffice.exe

 

     key:AppParameters

     value:-invisible -headless -accept=socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard

 

     3.在服务控制台,启动 openoffice服务

     4.CMD中用以下命令查看8100是否已被监听:netstat -anop tcp

这样OpenOffice3.0就以服务方式运行在Windows系统上了。(使用cmd命令:netstat -anp tcp查看8100端口是否工作)

然後可以通过socket方式连接openOffice,以使用openoffice提供的某些服务,如文件转换服务,ms officepdf等等。

开源项目 JODConverter就是结合openoffice来进行文档转换的java组件。

另外有一个命令行工具swftools,该工具可以将pdf转换为swf格式的文档,提供给ie客戶端流览。    

 

二、使用JODConverter将office文档转换为pdf

JODConverter是一个javaOpenDucument文件转换器,可以进行许多文件格式的转换,它利用

OpenOffice来进行转换工作,它能进行以下的转换工作:

     1.Microsoft Office格式转换为OpenDucument,以及OpenDucument转换为Microsoft Office

     2.OpenDucument转换为PDFWordExcelPowerPoint转换为PDFRTF转换为PDF等。

它是一个开源项目。

我的项目是在eclipse下开发的。

下载最新版的jodconverter-2.2.2,把lib文件夹的包导入到你的DocConverter项目的lib文件夹内。

package ...
import java.io.File;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

public class DOC2PDFUtil extends Thread{
	private File inputFile;
	private File outputFile;
	
	public DOC2PDFUtil(File inputFile,File outputFile){
		this.inputFile = inputFile;
		this.outputFile = outputFile;
	}
	
	public void docToPdf(){
		OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
		try{
			connection.connect();
			DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
			converter.convert(inputFile, outputFile);
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(connection != null){
				connection.disconnect();
				connection = null;
			}
		}
	}
	
	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;
	}
	
	public static void main(String[] args) {
		File inputFile = new File("E://attachment//干旱系统测试问题.docx");
		File outputFile = new File("E://attachment//干旱系统测试问题.pdf");
		DOC2PDFUtil dp = new DOC2PDFUtil(inputFile, outputFile);
		dp.start();
	}
	
}


三、使用swftools将pdf转成swf

安装swftools,注意:安装路径最好不要存在空格

package ...
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class PDF2SWFUtil {
	public static synchronized void pdf2swf(String fileDir,String exePath) throws IOException{
		String filePath = fileDir.substring(0,fileDir.lastIndexOf("/"));
		String fileName = fileDir.substring(filePath.length() + 1,fileDir.lastIndexOf("."));
		Process pro = null;
		if(isWindowsSystem()){
			String cmd = exePath + " \"" + fileDir + " \" -o \"" + filePath + "/" + fileName + ".swf\"";
			System.out.println(cmd);
			pro = Runtime.getRuntime().exec(cmd);
		}else{
			String[] cmd = new String[3];
			cmd[0] = exePath;
			cmd[1] = fileDir;
			cmd[2] = filePath + "/" + fileName + ".swf";
			pro = Runtime.getRuntime().exec(cmd);
		}
		
		new DoOutPut(pro.getInputStream()).start();
		new DoOutPut(pro.getErrorStream()).start();
		
		try {
			pro.waitFor();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	private static boolean isWindowsSystem(){
		String p = System.getProperty("os.name");
		return p.toLowerCase().indexOf("windows")>=0?true:false;
	}
	
	private static class DoOutPut extends Thread{
		public InputStream is;
		
		public DoOutPut(InputStream is){
			this.is = is;
		}
		
		public void run(){
			BufferedReader br = new BufferedReader(new InputStreamReader(this.is));
			String str = null;
			try{
				while((str = br.readLine()) != null);
			}catch (Exception e) {
				e.printStackTrace();
			}finally{
				if(br != null){
					try{
						br.close();
					}catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	public static void main(String[] args){
		String exePath = "D:/SWFTools/pdf2swf.exe";
		try{
			pdf2swf("E:/attachment/干旱系统测试问题.docx", exePath);
		}catch(IOException e){
			e.printStackTrace();
		}
	}
}


四、office文档转换为pdf,同事转换为swf

package ...

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;


public class DocConverter {
	private int environment=1;
	private String fileName;
	private File pdfFile;
	private File swfFile;
	private File docFile;
	private String command;
	
	public DocConverter(String fileString,String command){
		this.command = command;
		ini(fileString);
	}
	
	private void ini(String fileString){
		if(isWindowsSystem()){
			environment = 1;
		}else{
			environment = 2;
		}
		fileName = fileString.substring(0,fileString.lastIndexOf("."));
		docFile = new File(fileString);
		pdfFile = new File(fileName+".pdf");
		swfFile = new File(fileName+".swf");
	}
	
	private boolean isWindowsSystem(){
		String p = System.getProperty("os.name");
		return p.toLowerCase().indexOf("windows")>=0?true:false;
	}
	
	private void doc2pdf() throws Exception{
		if(docFile.exists()){
			if(!pdfFile.exists()){
				OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
				try{
					connection.connect();
					DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
					converter.convert(docFile, pdfFile);
					connection.disconnect();
				}catch (java.net.ConnectException e) {
					e.printStackTrace();
					System.out.println("....swf转换异常,openoffice服务未启动!");
					throw e;
				}catch (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 void pdf2swf() throws Exception
    {
        Runtime r=Runtime.getRuntime();
        if(!swfFile.exists())
        {
            if(pdfFile.exists())
            {
                if(environment==1)//windows环境处理
                {
                    try {
                        Process p=r.exec( command + " " +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 (Exception 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已存在不需要转换****");
        }
    }
    
    static String loadStream(InputStream in) throws IOException
    {
        int ptr=0;
        in=new BufferedInputStream(in);
        StringBuffer buffer=new StringBuffer();
        
        while((ptr=in.read())!=-1)
        {
            buffer.append((char)ptr);
        }
        return buffer.toString();
    }
    
    /*
     * 转换主方法
     */
    public boolean conver()
    {
        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) {
            e.printStackTrace();
            return false;
        }
        
        if(swfFile.exists())
        {
            return true;
        }
        else {
            return false;
        }
    }
    
    /*
     * 返回文件路径
     * @param s
     */
    public String getswfPath()
    {
        if(swfFile.exists())
        {
            String tempString =swfFile.getPath();
            tempString=tempString.replaceAll("\\\\", "/");
            return tempString;
        }
        else{
            return "";
        }
    }
    
    /*
     * 设置输出路径
     */
    public void setOutputPath(String outputPath)
    {
        if(!outputPath.equals(""))
        {
            String realName=fileName.substring(fileName.lastIndexOf("/"),fileName.lastIndexOf("."));
            if(outputPath.charAt(outputPath.length())=='/')
            {
                swfFile=new File(outputPath+realName+".swf");
            }
            else
            {
                swfFile=new File(outputPath+realName+".swf");
            }
        }
    }
    
    public static void main(String s[])
    {
        DocConverter d=new DocConverter("E:/attachment/1405391789462/1405391789462.docx","D:/SWFTools/pdf2swf.exe");
        d.conver();
    }
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值