admin管理员组

文章数量:1531701

2024年6月6日发(作者:)

关于开似百度文库和豆丁的在线文档(txt,doc,JPG等转为SWF或pdf)转Flash播放器的实现

需要安装flash 10 及以上版本才可以浏览。

2010-09-28

Java编程把Word转Pdf

文章分类:Java编程

Java代码

我的word转pdf用到了一个虚拟的打印机,安装一个Adobe Acrobat 7.0 Professional就

可以了

2。配置虚拟打印机,开始--打印机和传真--添加打印机---一步一步的配置就

行了。

3。点击配置的打印机右键首选项----设置----把不要发送字体到打印机取消勾

4。下载包。把所对应的放在windows/sys32下或者

jre/bin下

5。代码部分

代码

通过代码将PDF转换成SWF来说,现在比较常用的一种方式就是利

用SWFTools工具中的pdf2swf(/)。这个

工具还是比较好用的。转换成的SWF文件质量也不错。

默认分类 2010-12-04 01:37:12 阅读276 评论0 字号:大中小 订阅

Java代码

* PDF转SWF工具

* @author tangs

*

*/

public class Converter {

public static int convertPDF2SWF(String sourcePath, String destPath, String fileName) throws IOExc

eption {

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

File dest = new File(destPath);

if (!()) ();

//源文件不存在则返回

File source = new File(sourcePath);

if (!()) return 0;

//调用pdf2swf命令进行转换

String command = "D:Program " + " -o "" + destPath + "" + fileNa

me + "" -s languagedir=D:xpdfxpdf-chinese-simplified -s f

lashversion=9 "" + sourcePath + """;

Process pro = time().exec(command);

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(utStrea

m()));

while (ne() != null);

try {

r();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

tackTrace();

}

return lue();

}

public static void main(String []args) throws IOException {

String sourcePath = "c:";

String destPath = "c:";

String fileName = "";

tPDF2SWF(sourcePath, destPath, fileName);

}

}

* PDF转SWF工具

* @author tangs

*

*/

public class Converter {

public static int convertPDF2SWF(String sourcePath, String destPath, String fileName) throws

IOException {

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

File dest = new File(destPath);

if (!()) ();

//源文件不存在则返回

File source = new File(sourcePath);

if (!()) return 0;

//调用pdf2swf命令进行转换

String command = "D:Program " + " -o "" + destPath + "" +

fileName + "" -s languagedir=D:xpdfxpdf-chinese-simplified -s flashversion=9 "" + sourcePath + """;

Process pro = time().exec(command);

BufferedReader bufferedReader = new BufferedReader(new

InputStreamReader(utStream()));

while (ne() != null);

try {

r();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

tackTrace();

}

return lue();

}

public static void main(String []args) throws IOException {

String sourcePath = "c:";

String destPath = "c:";

String fileName = "";

tPDF2SWF(sourcePath, destPath, fileName);

}

}

就这么简单的几行代码就可以了。但是在程序中遇到中文就会出现意想不到的情况,这个也不

例外。在转换中,我发现有些中文PDF文件转换后会出现乱码的现象,因此这里还要处理一下乱码的问题。

看到上面代码中红色的一段了吗?这就是解决乱码的方法。这个方法是参考了

/xwx520/blog/item/这篇文章,感谢作者。

1.下载XPDF:ftp:///pub/xpdf/,并解压到

xpdf-chinese-simplified目录下。

2.下载字体:/wp-content/uploads/2009/02/,并解压到

xpdf-chinese-simplified/CMap目录下。

3.修改xpdf-chinese-simplified目录下的add-to-xpdfrc文件。将里面的路径设为自己的路径:

4.参照上面的代码,在调用pdf2swf命令中加入“ -s

languagedir=D:xpdfxpdf-chinese-simplified ”参数。

这样乱码的问题就解决了。

乱码的问题

通过XPDF抽取PDF中的中文文本

1、下载XPDF,参考下载地址:

/wp-content/uploads/2009/02/

2、下载字体和,参考下载地址:

/wp-content/uploads/2009/02/

3、解压XPDF和字体,将字体放到CMap目录下

Java代码

//调用pdf2swf命令进行转换

StringBuffer command =new StringBuffer();

(lsPath);

(" -o ").append(destPath +fileName);//文件输出目录指令

(" -z -B ").append(wer);//文件压缩指令,文件导航指

(" -t ").append(sourcePath);//载入源文件指令

(" -s languagedir=").append(guagedir);//文件语言指令

Process pro = time().exec(ng());

n(ng());

//调用pdf2swf命令进行转换

StringBuffer command =new StringBuffer();

(lsPath);

(" -o ").append(destPath +fileName);//文件输出目录指令

(" -z -B ").append(wer);//文件压缩指令,文件导航指

(" -t ").append(sourcePath);//载入源文件指令

(" -s languagedir=").append(guagedir);//文件语言指令

Process pro = time().exec(ng());

n(ng());

上面是我的java代码,生成的

ng()=“D: -o D: -z -B D: -t

D: -s languagedir=D:xpdf-chinese-simplified ”的字符串我直接在命令行执行的话,是能生成导

航的,但是在java中执行却生成不了导航,请大家指教

4、修改add-to-xpdfrc文件中的地址 ,将路径该为本机安装路径

5、修改xpdfrc文件 ,把地址修改为本机地址

6、编写简单的程序

7、运行

调用JODConverter将DOC文件转换为PDF文件

通过调用JODConverter将格式为WORD的DOC文件转换成PDF格式文件很容易,核心代码只有

几句:

File inputFile = new File("C:Documents and Settingsccnustone桌面新建文件夹数据挖掘技术

简介.doc");

File outputFile = new File("C:Documents and Settingsccnustone桌面新建文件夹数据挖掘技术简

介.pdf");

// connect to an instance running on port 8100

OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);

t();

// convert

DocumentConverter converter = new OpenOfficeDocumentConverter(connection);

t(inputFile, outputFile);

// close the connection

nect();

先到/project/?group_id=91849下载,接

压缩后,找到LIB文件夹,将里面的包配置到Build Path中,启动OPEN OFFICE的服务就OK了。至于要

实现其他格式转换,可以参看里面的API。

于是你打开任务管理器看看是否有

和 这两个进程,如果没有那就是因为Openoffice的服务没有启动.

你需要cmd到Openoffice主程序的安装目录下

cd C:Program 3program

运行如下命令

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

如果没有保存哦.你再看任务管理器的进程是不是多出了 和

现在重新运行程序

I have installed openofficeorg2.2 on redhat-linux. Java 1.4_11.

A difference is that I do not start the service throught the java client as mentioned in this thread.

I start it from the console using

soffice -headless -accept="socket,host=localhost,port=8100;urp;"

adding eManager also doesn't make a difference.

Also tried adding - luck

Netstat doesn't

When I tried opening the application from the menu (GUI), there is no response.

Hence I guess, there is something wrong or incomplete in the installation.

I had installed using the folllowing commands:

rpm -Uvh --prefix /usr/openoffice/ *.rpm

cd desktop-integration/

rpm -Uvh

Since I'm not using the default path, but /usr/openoffice/, not sure if path has to be updated in some

调用swftools将中文PDF转化为SWF

1、下载swftools,参考下载地址:/,并安装SWFTools;

2、参照《通过XPDF抽取PDF中的中文文本》中,对XPDF做相应配置,避免转化过程中产生中

文乱码;

3、通过命令提示符,进入SWFTools安装目录,启动转换命令D:Program FilesSWFTools>pdf2swf

-o G: -t G:testreadtest.

pdf -s languagedir=G:testreadxpdfchinese-simplified

4、转换结果

文档转换过程讲解

年前因为公司技术文档需要保密,所以一直在研究在线文档这类知识,网上有很多关于这方

面的资料,我大概总结了一下:

主要有从canbeingblog上找到的,近两年出现了许多以“经验交易”为核心的文档平台,如

豆丁网、百度文库、星期八等网站。这些网站将文档(知识)存放在网站(平台)上,供用

户浏览、讨论、下载,配以良好的积分体系和金钱激励(运营),完美的协调好知识分享与

知识传播之间的微妙关系。这样即有Web2.0的用户互动,也有Web3.0的价值分配,对社

会也是非常好的资源重用,是非常棒的一种运营模式。

从技术层面讲,这类网站的创新主要有文档预览、知识商城和SNS三个块。今年初公

司也决定在公司内部引入这种模式,以实现公司内部文档特别是售前方案的分享,很荣幸,

我被安排为此项目的开发经理,也专门研究了文档在线预览的技术。如今项目已经上线将近

百天,也是时候整理一下思路,这篇博客主要就是分析一些文档在线预览的技术细节,其实

网上已经有很多类似的文章,但大多没有说得很清楚,也比较零乱,所以我想整理一个完整

的文章来。文章内容将会涉及:总体思路、预览实现细节、缩略图实现、百度阅读器使用等。

文档预览即文档以flash的形式在网页上展示,所以要解决两个问题:以什么格式展示

flash、文档如何转化为符合格式的flash。

展示flash有两种方式,一种是直接生成好一个完整的flash展示出来,一种是用一个播

放器来播放另外一个文档flash。第一种方式可以用SWFTools的工具直接将pdf转化成一个

整体的flash展示,但这种方式的flash较大,且不容易控制,故用得较少。第二种方式是重

点,首先需要了解的是一个开源的flexpaper播放器,它可以播放一帧一页的flash;在flexpaper

的基础上,如果遇到大文档则会出现速度和性能的问题,这个时候需要像百度文库、豆丁一

样修改flexpaper,让它支持一次仅读取指定页数的flash;如果文档安全级别较高,不允许

下载查看,则需要给flexpaper加上加密解密算法;如果需要登录用户才能查看,则要让

flexpaper有登录UI。

转化文档为flash的思路有一条,但有多种实现方式。一般文档需要先转化为pdf,再从

pdf转化为flash。转化为pdf的方法有很多,例如使用虚拟打印机、使用MicrosoftOffice的

API、使用OpenOffice转化等,不过转化的过程中,可能由于文档有误、损坏、加密等原因

造成转化失败。从pdf转化为flash就简单了许多,直接使用SWFTools的pdf2swf即可实现

多种方式的转化。

下图是文档转化到swf的一个流程,其中type是文档的一个状态标识(0表示未处理过

的文档,1表示已生成pdf,2表示已生成flash,-1表示不受支持的文档格式,-2表示转化

为pdf失败,-3表示转化为flash失败)

个人总结:

经过采用这种方法后,实现文档在线阅读,但是,其他文档转flash格式非常麻烦,正常

文件一般没什么大问题,但是有异常的pdf文件,就会出现很多问题,不能转换成功。

所以,个人估计,百度,豆丁采用的应该是用播放器方式,不是我们这种常规方式。

很多人用java进行文档操作时经常会遇到一个问题,就是如何获得word,excel,pdf等文档的内容?现

在总结抽取word,pdf的一种方法。

其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取

word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。

jacob jar与dll文件下载: /down_?id=13

下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,

下面是一个简单的例子:

import ;

import .*;

import X.*;

/**

* Title: pdf extraction

* @version 1.0,who use this example pls remain the declare

*/

public class FileExtracter{

public static void main(String[] args) {

ActiveXComponent component = new ActiveXComponent("ation");

String inFile = "c:";

String tpFile = "c:";

String tFile = "c:";

boolean flag = false;

try {

perty("Visible", new Variant(false));

Object wordacc = perty("document.").toDispatch();

Object wordfile = (wordacc,"Open", ,

new Object[]{inFile,new Variant(false), new Variant(true)},

new int[1] ).toDispatch();

(wordfile,"SaveAs", , new Object[]{tpFile,new Variant}, new int[1]);

Variant f = new Variant(false);

(wordfile, "Close", f);

flag = true;

} catch (Exception e) {

tackTrace();

} finally {

("Quit", new Variant[] {});

}

}

}

通过openoffice将各类文档转换为pdf格式

(2010-08-02 09:03:43)

转载

标签:

it

之前找了一种方式是通过jacob调用adobe PDF虚拟打印机生成PDF,但多人同时调用时有同步问题;

在同事的提醒下找了下面的实现方式:

1、安装OpenOffice 3:下载路径:/new/zh_cn/

2、用以下命令启动OpenOffice服务

cd C:Program 3program

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

3、附上我写的DEMO,第三方包:

JODConverter v2.2.1

(1.7 MB)

官方網站:

/opensource/jodconverter

下載地點:

/opensource/jodconverter

/new/zh_tw/

目前版本: JODConverter v2.2.1, OpenOffice v3.0.0

使用需求: JDK1.4以上, 安裝OpenOffice v2.0.3以上

基本簡介:

JODConverter主要的功能是用來做各種檔案的轉換. 目前測試過, Word,Excel,PowerPoint轉

PDF都是沒問題的.

因為JODConverter是透過OpenOffice來做轉換, 所以使用前需要先安裝OpenOffice, 並且將

OpenOffice的Service啟動, 才可以使用.

使用教學:

Step1: 安裝OpenOffice

Step2: 啟動OpenOffice Service

1 cd C:Program 3program

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

Step3:將JODConverter的Jar檔放進專案中的Library, 請檢查你的專案是否包含以下的Jar

檔:

Step4: 準備一個word檔放在c:/

Step5: 執行以下程式

import ;

import ntConverter;

import ficeConnection;

import OpenOfficeConnection;

import ficeDocumentConverter;

public class JodDemo {

public static void main(String[] args) throws Exception{

File inputFile = new File("c:/");

File outputFile = new File("c:/");

// connect to an instance running on port 8100

OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);

t();

// convert

DocumentConverter converter = new OpenOfficeDocumentConverter(connection);

t(inputFile, outputFile);

// close the connection

nect();

}

}

程式說明:

程式的部份相當簡潔, 特別要注意的地方是第12行連線的port必須與你啟動OpenOffice的

Port相同,

另外JODConverter預設是用副檔名作文件種類的判斷, 所以副檔名必須要正確才行.

如果副檔名比較特別的話, 就必須在convert()的時候強制指定Document Type.

心得:

JODConverter使用起來相當方便, 官網也提供War檔讓JODConverter變成Web Service提供

給不同的語言來呼叫.

特別要注意的是, OpenOffice Service並不是ThreadSafe的, 多個Web AP在使用的時候必須

要注意.

參考資料:

/opensource/jodconverter

那我也來補充一些好了

之前也在試這個檔案轉換的程式

程式最好加上 try-catch

因為之前發現有些檔案 format 不能轉,發生 Exception 後,connection 不會自動切斷,程

式會 hand 住

所以改成如下方式:

11121314

public void convert(String input, String output){

File inputFile = new File(input);

File outputFile = new File(output);

OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);

try {

t();

DocumentConverter converter = new OpenOfficeDocumentConverter(connection);

t(inputFile, outputFile);

} catch(Exception e) {

tackTrace();

} finally {

try{ if(connection != null){nect(); connection = null;}}catch(Exception

e){}

}

}

再來,明明就是 open office 的檔案,卻生不能轉換的問題。例如:*.STW, *.SXD, *.ODF 等,

後來才知道可以自行指定來源檔和輸出檔的 mime-type,程式如下:

17

public void convertSTW(String input, String output){

DocumentFormat stw = new DocumentFormat(" 1.0 Template",

, "application/", "stw");

DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();

DocumentFormat pdf = matByFileExtension("pdf");

File inputFile = new File(input);

File outputFile = new File(output);

OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);

try {

t();

DocumentConverter converter = new OpenOfficeDocumentConverter(connection);

t(inputFile, stw, outputFile, pdf);

} catch(Exception e) {

tackTrace();

} finally {

try{ if(connection != null){nect(); connection = null;}}catch(Exception

e){}

}

}

上面的程式是轉換 STW 到 PDF,如果是 SXD / ODF 則只需要變更 DocumentFormat 的

內容即可。

123

DocumentFormat sxd = new DocumentFormat(" 1.0 Drawing",

G, "application/", "sxd");

DocumentFormat odf = new DocumentFormat("OpenDocument Math", ,

"application/a", "odf");

所有 default support 的 DocumentFormat 都在

tDocumentFormatRegistry 裡,但並非所有 open office 支

援的 file format 都有,所以要像上面的方法自行去定義 DocumentFormat。

在此獻給所有需要作 File Convert 的人試試。

免錢的,最好用。還有 source code 可以自己改。

本文标签: 文档文件转换实现转化