乔山办公网我们一直在努力
您的位置:乔山办公网 > office365 > jodconverter支持什么格式转换-office2pdf java,java实现office预览

jodconverter支持什么格式转换-office2pdf java,java实现office预览

作者:乔山办公网日期:

返回目录:office365


要生成的pdf中包含书签,首先需要你的doc文件里面本身包含标签。
重新编辑doc文件,加入标签,或者按照文档出版的标准,学会应用【格式和样式】
特别是标题文字,不能把字号加大、居中就算是标题,必须把标题文字的【格式和样式】设置为【标题】才行。还有那些校标题,按级别分别设置为【标题1、zhidao2、3】等等,这样转化为pdf,才会生成各章节的标签。

http://nopainnogain.iteye.com/blog/819432

[JODConverter]word转pdf心得分享(转)

文档视频转为flash格式在线播放
OfficeSocketLinuxOpenSourceExcel
官方网站: http:///new/zh_tw/downloads.html
  目前版本: JODConverter v2.2.1, OpenOffice v3.0.0
  使用需求: JDK1.4以上, 安装OpenOffice v2.0.3以上

  基本简介:
  JODConverter主要的功能是用来做各种档案的转换. 目前测试过, Word,Excel,PowerPoint转PDF都是没问题的.
  因为JODConverter是透过OpenOffice来做转换, 所以使用前需要先安装OpenOffice, 并且将OpenOffice的Service启动, 才可以使用.

OpenOffice.org具有一个鲜为人知的特性就是其能够作为一个服务来运行,而这种能力具有一定的妙用。举例来说,你可以把openoffice.og变成一个转换引擎,利用这种转换引擎你可以通过网络接口或命令行工具对文件的格式进行转换,JODConverter可以帮助你实现OpenOffice.org的这种文件转换功能。
为了将OpenOffice.org作为一个转换引擎,你必须以服务的方式将它启动,使它在某个特定的端口监听连接,在Linux平台你可以用如下的命令启动openoffice.org:
soffice -headless -accept=”socket,port=8100;urp;”(我在linux下使用soffice -headless -accept=”socket,host=127.0.0.1,port=8100;urp;”,open office server是开启来了,但是文件转换不成功,异常是连接失败,这个很可以是你用jodconverter来转换时使用的是localhost,而当你的机有host配置文件里没有将localhost与127.0.0.1对应起来时,就无法解析了,这里可以修改host文件或去掉host=127.0.0.1,这样我试过可以成功)
在Windows平台, 使用如下命令:
“C:\Program Files\OpenOffice.org 2.2\program\soffice” -accept=”socket,port=8100;urp;”
  使用教学:
  Step1: 安装OpenOffice
  Step2: 启动OpenOffice Service

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

  Step3:将JODConverter的Jar档放进专案中的Library, 请检查你的专案是否包含以下的Jar档:

  jodconverter-2.2.1.jar
  jurt-2.3.0.jar
  xstream-1.2.2.jar
  ridl-2.3.0.jar
  commons-io-1.3.1.jar
  juh-2.3.0.jar
  slf4j-api-1.4.3.jar
  unoil-2.3.0.jar
  slf4j-jdk14-1.4.3.jar

  Step4: 准备一个word档放在c:/document.doc
  Step5: 执行以下程式

Java代码
<span style="font-size: medium;">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 JodDemo {

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

  File inputFile = new File("c:/document.doc");

  File outputFile = new File("c:/document.pdf");

  // connect to an OpenOffice.org instance running on port 8100

  OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);

  connection.connect();

  // convert

  DocumentConverter converter = new OpenOfficeDocumentConverter(connection);

  converter.convert(inputFile, outputFile);

  // close the connection

  connection.disconnect();

  }

  } </span>
程式说明:

  程式的部份相当简洁, 特别要注意的地方是第12行连线的port必须与你启动OpenOffice的Port相同,
  另外JODConverter预设是用副档名作文件种类的判断, 所以副档名必须要e799bee5baa6e79fa5e98193e58685e5aeb9362正确才行.
  如果副档名比较特别的话, 就必须在convert()的时候强制指定Document Type.

心得:
  JODConverter使用起来相当方便, 官网也提供War档让JODConverter变成Web Service提供给不同的语言来呼叫.
  特别要注意的是, OpenOffice Service并不是ThreadSafe的, 多个Web AP在使用的时候必须要注意.

那我也来补充一些好了
之前也在试这个档案转换的程式
程式最好加上 try-catch
因为之前发现有些档案 format 不能转,发生 Exception 后,connection 不会自动切断,程序会hand 住
所以改成如下方式:

Java代码
<span style="font-size: medium;">public void convert(String input, String output){
File inputFile = new File(input);
File outputFile = new File(output);
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
} catch(Exception e) {
e.printStackTrace();
} finally {
try{ if(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){}
}
} </span>

再来,明明就是 open office 的档案,却生不能转换的问题。例如:*.STW, *.SXD, *.ODF 等,後来才知道可以自行指定来源档和输出档的 mime-type,程式如下:

Java代码
<span style="font-size: medium;">public void convertSTW(String input, String output){
DocumentFormat stw = new DocumentFormat("OpenOffice.org 1.0 Template", DocumentFamily.TEXT, "application/vnd.sun.xml.writer", "stw");
DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
DocumentFormat pdf = formatReg.getFormatByFileExtension("pdf");
File inputFile = new File(input);
File outputFile = new File(output);
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, stw, outputFile, pdf);
} catch(Exception e) {
e.printStackTrace();
} finally {
try{ if(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){}
}
} </span>
上面的程式是转换 STW 到 PDF,如果是 SXD / ODF 则只需要变更 DocumentFormat 的内容即可。

Java代码
<span style="font-size: medium;">DocumentFormat sxd = new DocumentFormat("OpenOffice.org 1.0 Drawing", DocumentFamily.DRAWING, "application/vnd.sun.xml.sraw", "sxd");

DocumentFormat odf = new DocumentFormat("OpenDocument Math", DocumentFamily.TEXT, "application/vnd.oasis.opendocument.formula", "odf"); </span>
所有 default support 的 DocumentFormat 都在 com.artofsolving.jodconverter.DefaultDocumentFormatRegistry 里,但并非所有 open office 支援的 file format 都有,所以要像上面的方法自行去定义 DocumentFormat,至于它里面的参数可以从jodconverter-2.2.2.jar包的com.artofsolving.jodconverter包下的document-formats.xml文件里面得到,这样就可以完成多种格式的转换,如open office,ms office , wps office及所有的纯文本文件。

在此献给所有需要作 File Convert 的人试试。
免钱的,最好用。还有 source code 可以自己改。

另 将图片文件放入 word中可直接用word自带 pdf 转化工具进行转化!
先用openoffice直接转换为pdf(打开openoffice,在openoffice文件菜单中打开该文档,再在文件菜单中导出为pdf)
看看是不是openoffice本身对该文档的支持不完善。

1 有几种原因可以参考一下:
1)从数据库读取出来的内容 与 写死的文件名是否zhidao 物理一致?即是否是其他编码?
2)注意从数据库读取出来的字符串是否在前后有多余的空格?即验证其长度
3)查找物理位置上的文件是否真的存在?
4) 是否能够连接到soffice上,soffice是否启动了多次
5) 我曾经遇到的问题是:在应用程序中写死的和从数据库读取的一样,但在系统磁盘上所保存的就不是这个文件名,而是某种编码的字符,因此找不到,希望对您有所帮助?
如果解决还请您共享经验啊!!多谢

相关阅读

  • <em>java</em>使用openoffice.org将word转pdf出...-java

  • 乔山办公网office365
  • 两种方式:1、纯百Java,用POI来做度2、用JNA调用word接口,根据office api来做第一种呢对于java开发来说相对简单知,但是需要学道POI,而且估计有些格式控制不好。第回二种要学习JNA,而
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网