java 批量转pdf_百度文库的实现——java利用openoffice,批量word转pdf

packagewordtopdf;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.IOException;importcom.artofsolving.jodconverter.DocumentConverter;importcom.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;importcom.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;importcom.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;public classtopdftest {private static int depth=1;public static void main(String[] args) throwsIOException {//office2PDF("D:\\资源管理\\11","D:\\资源管理\\11");

find("D:\\资源管理\\11",depth);

}/*** 将Office文档转换为PDF. 运行该函数需要用到OpenOffice, OpenOffice下载地址为

*http://www.openoffice.org/*

*

 
 

* 方法示例:

* String sourcePath = "F:\\office\\source.doc";

* String destFile = "F:\\pdf\\dest.pdf";

* Converter.office2PDF(sourcePath, destFile);

*

*

*@paramsourceFile

* 源文件, 绝对路径. 可以是Office2003-2007全部格式的文档, Office2010的没测试. 包括.doc,

* .docx, .xls, .xlsx, .ppt, .pptx等. 示例: F:\\office\\source.doc

*@paramdestFile

* 目标文件. 绝对路径. 示例: F:\\pdf\\dest.pdf

*@return操作成功与否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置错误; 如果返回 0,

* 则表示操作成功; 返回1, 则表示转换失败*/

public static intoffice2PDF(String sourceFile, String destFile) {try{

File inputFile= newFile(sourceFile);if (!inputFile.exists()) {return -1;//找不到源文件, 则返回-1

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

File outputFile = newFile(destFile);if (!outputFile.getParentFile().exists()) {

outputFile.getParentFile().mkdirs();

}

String OpenOffice_HOME= "C:\\Program Files (x86)\\OpenOffice 4";//这里是OpenOffice的安装目录, 在我的项目中,为了便于拓展接口,没有直接写成这个样子,但是这样是绝对没问题的//如果从文件中读取的URL地址最后一个字符不是 '\',则添加'\'

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

OpenOffice_HOME+= "\\";

}//启动OpenOffice的服务

String command =OpenOffice_HOME+ "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";

Process pro=Runtime.getRuntime().exec(command);//connect to an OpenOffice.org instance running on port 8100

OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);

connection.connect();//convert

DocumentConverter converter = newOpenOfficeDocumentConverter(connection);

converter.convert(inputFile, outputFile);//close the connection

connection.disconnect();//关闭OpenOffice服务的进程

pro.destroy();return 0;

}catch(FileNotFoundException e) {

e.printStackTrace();return -1;

}catch(IOException e) {

e.printStackTrace();

}return 1;

}public static void find(String pathName,int depth) throwsIOException{int filecount=0;//获取pathName的File对象

File dirFile = newFile(pathName);//判断该文件或目录是否存在,不存在时在控制台输出提醒

if (!dirFile.exists()) {

System.out.println("do not exit");return;

}//判断如果不是一个目录,就判断是不是一个文件,时文件则输出文件路径

if (!dirFile.isDirectory()) {if(dirFile.isFile()) {

System.out.println(dirFile.getCanonicalFile());int end = String.valueOf(dirFile.getCanonicalFile()).lastIndexOf(".");

office2PDF(String.valueOf(dirFile.getCanonicalFile()),String.valueOf(dirFile.getCanonicalFile()).substring(0,end)+".pdf");

}return;

}//获取此目录下的所有文件名与目录名

String[] fileList =dirFile.list();int currentDepth=depth+1;for (int i = 0; i < fileList.length; i++) {//遍历文件目录

String string =fileList[i];//File("documentName","fileName")是File的另一个构造器

File file = newFile(dirFile.getPath(),string);

String name=file.getName();//如果是一个目录,搜索深度depth++,输出目录名后,进行递归

if(file.isDirectory()) {//递归

find(file.getCanonicalPath(),currentDepth);

}else{int end = file.getPath().lastIndexOf(".");

office2PDF(file.getPath(),file.getPath().substring(0,end)+".pdf");

}

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值