java office pdf_java+OpenOffice 实现office转成pdf

importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.util.Scanner;importcom.artofsolving.jodconverter.DocumentConverter;importcom.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;importcom.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;importcom.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;public classDoc2PdfHtmlUtil {private static final String servicePath = "C:\\Program Files (x86)\\OpenOffice 4\\";public static void main(String[] args) throwsException {//startOpenOfficeService();

String file = "d:/33.pptx";

InputStream inputStream= new FileInputStream(newFile(file));

file2pdf(inputStream,"d:/", "ppt");//shutdownOpenOfficeService();

}/*** 转换文件成pdf

*

*@paramfromFileInputStream:

*@throwsIOException*/

public static String file2pdf(InputStream fromFileInputStream, String toFilePath, String type) throwsException {

String timesuffix=String.valueOf(System.currentTimeMillis());

String docFileName= null;

String htmFileName= null;if ("doc".equals(type)) {

docFileName= timesuffix.concat(".doc");

htmFileName= timesuffix.concat(".pdf");

}else if ("docx".equals(type)) {

docFileName= timesuffix.concat(".docx");

htmFileName= timesuffix.concat(".pdf");

}else if ("xls".equals(type)) {

docFileName= timesuffix.concat(".xls");

htmFileName= timesuffix.concat(".pdf");

}else if ("xlsx".equals(type)) {

docFileName= timesuffix.concat(".xlsx");

htmFileName= timesuffix.concat(".pdf");

}else if ("ppt".equals(type)) {

docFileName= timesuffix.concat(".ppt");

htmFileName= timesuffix.concat(".pdf");

}else if ("pptx".equals(type)) {

docFileName= timesuffix.concat(".pptx");

htmFileName= timesuffix.concat(".pdf");

}else if ("txt".equals(type)) {

docFileName= timesuffix.concat(".txt");

htmFileName= timesuffix.concat(".pdf");

}else{return null;

}

File htmlOutputFile= new File(toFilePath + File.separatorChar +htmFileName);

File docInputFile= new File(toFilePath + File.separatorChar +docFileName);if(htmlOutputFile.exists()) {

htmlOutputFile.delete();

}

htmlOutputFile.createNewFile();

docInputFile.createNewFile();/*** 由fromFileInputStream构建输入文件*/

int bytesRead = 0;byte[] buffer = new byte[1024];

OutputStream os= newFileOutputStream(docInputFile);while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {

os.write(buffer,0, bytesRead);

}

os.close();

fromFileInputStream.close();

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

connection.connect();//convert

DocumentConverter converter = newStreamOpenOfficeDocumentConverter(connection);

converter.convert(docInputFile, htmlOutputFile);

connection.disconnect();//转换完之后删除word文件

docInputFile.delete();

System.out.println("---------------执行完成---");returnhtmFileName;

}/*** 文件转换成Html

*

*@paramfromFileInputStream

*@paramtoFilePath

*@paramtype

*@return*@throwsException*/

public static String file2Html(InputStream fromFileInputStream, String toFilePath, String type) throwsException {

String timesuffix=String.valueOf(System.currentTimeMillis());

String docFileName= null;

String htmFileName= null;if ("doc".equals(type)) {

docFileName= timesuffix.concat(".doc");

htmFileName= timesuffix.concat(".html");

}else if ("docx".equals(type)) {

docFileName= timesuffix.concat(".docx");

htmFileName= timesuffix.concat(".html");

}else if ("xls".equals(type)) {

docFileName= timesuffix.concat(".xls");

htmFileName= timesuffix.concat(".html");

}else if ("xlsx".equals(type)) {

docFileName= timesuffix.concat(".xlsx");

htmFileName= timesuffix.concat(".html");

}else if ("ppt".equals(type)) {

docFileName= timesuffix.concat(".ppt");

htmFileName= timesuffix.concat(".html");

}else if ("pptx".equals(type)) {

docFileName= timesuffix.concat(".pptx");

htmFileName= timesuffix.concat(".html");

}else if ("txt".equals(type)) {

docFileName= timesuffix.concat(".txt");

htmFileName= timesuffix.concat(".html");

}else if ("pdf".equals(type)) {

docFileName= timesuffix.concat(".pdf");

htmFileName= timesuffix.concat(".html");

}else{return null;

}

File htmlOutputFile= new File(toFilePath + File.separatorChar +htmFileName);

File docInputFile= new File(toFilePath + File.separatorChar +docFileName);if(htmlOutputFile.exists()) {

htmlOutputFile.delete();

}

htmlOutputFile.createNewFile();

docInputFile.createNewFile();/*** 由fromFileInputStream构建输入文件*/

int bytesRead = 0;byte[] buffer = new byte[1024 * 8];

OutputStream os= newFileOutputStream(docInputFile);while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {

os.write(buffer,0, bytesRead);

}

os.close();

fromFileInputStream.close();

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

connection.connect();//convert

DocumentConverter converter = newStreamOpenOfficeDocumentConverter(connection);

converter.convert(docInputFile, htmlOutputFile);

connection.disconnect();//转换完之后删除word文件

docInputFile.delete();returnhtmFileName;

}public static voidstartOpenOfficeService() {

String command=servicePath+ "program\\soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard";try{

Process pro=Runtime.getRuntime().exec(command);

}catch(IOException e) {

System.out.println("OpenOffice服务启动失败");

}

}/***关闭代码

**/

public static voidshutdownOpenOfficeService() {

Scanner in= null;try{

Process pro= Runtime.getRuntime().exec("tasklist");

in= newScanner(pro.getInputStream());while(in.hasNext()) {

String proString=in.nextLine();if(proString.contains("soffice.exe")) {

String cmd= "taskkill /f /im soffice.exe";

pro=Runtime.getRuntime().exec(cmd);

System.out.println("soffice.exe关闭");

}if(proString.contains("soffice.bin")) {

String cmd= "taskkill /f /im soffice.bin";

pro=Runtime.getRuntime().exec(cmd);

System.out.println("soffice.bin关闭");

}

}

}catch(IOException e) {

e.printStackTrace();

}finally{if(in != null) {

in.close();

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值