欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

怎么在Java中利用openoffice將doc、docx轉(zhuǎn)為pdf

本文章向大家介紹怎么在Java中利用openoffice將doc、docx轉(zhuǎn)為pdf,主要包括怎么在Java中利用openoffice將doc、docx轉(zhuǎn)為pdf的使用實(shí)例、應(yīng)用技巧、基本知識點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值,需要的朋友可以參考一下。

成都創(chuàng)新互聯(lián)于2013年創(chuàng)立,先為玉門等服務(wù)建站,玉門等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為玉門企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

Java的特點(diǎn)有哪些

Java的特點(diǎn)有哪些 1.Java語言作為靜態(tài)面向?qū)ο缶幊陶Z言的代表,實(shí)現(xiàn)了面向?qū)ο罄碚摚试S程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程。 2.Java具有簡單性、面向?qū)ο蟆⒎植际健踩浴⑵脚_獨(dú)立與可移植性、動態(tài)性等特點(diǎn)。 3.使用Java可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等。

1. 需要用的軟件

OpenOffice , JodConverter

2.啟動OpenOffice的服務(wù)

我到網(wǎng)上查如何利用OpenOffice進(jìn)行轉(zhuǎn)碼的時(shí)候,都是需要先用cmd啟動一個(gè)soffice服務(wù),啟動的命令是:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"。

但是實(shí)際上,對于我的項(xiàng)目,進(jìn)行轉(zhuǎn)碼只是偶爾進(jìn)行,然而當(dāng)OpenOffice的轉(zhuǎn)碼服務(wù)啟動以后,該進(jìn)程(進(jìn)程名稱是soffice.exe)會一直存在,并且大約占100M的內(nèi)存,感覺非常浪費(fèi)。于是我就想了一個(gè)辦法,可以將執(zhí)行該服務(wù)的命令直接在Java代碼里面調(diào)用,然后當(dāng)轉(zhuǎn)碼完成的時(shí)候,直接干掉這個(gè)進(jìn)程。在后面的JAVA代碼里面會有解釋。

所以,實(shí)際上,這第2步可以直接跳過

3.將JodConverter相關(guān)的jar包添加到項(xiàng)目中

將JodConverter解壓縮以后,把lib下面的jar包全部添加到項(xiàng)目中

注意:安裝openoffice

4. 下面就是重點(diǎn)嘍,詳見Java代碼解析

package cn;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
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;
/** 
 * office轉(zhuǎn)化為pdf 
 * pdf轉(zhuǎn)化為swf文件 
 * @author Administrator 
 * 
 */
public class Converter {
	private static String openOfficePath = "E:\\安裝軟件\\openoffice\\date";
	//openoffice軟件的安裝路徑 
	/** 
   * 將Office文檔轉(zhuǎn)換為PDF. 運(yùn)行該函數(shù)需要用到OpenOffice和jodconverter-2.2.2 
   * <pre> 
   * 方法示例: 
   * String sourcePath = "F:\\office\\source.doc"; 
   * String destFile = "F:\\pdf\\dest.pdf"; 
   * Converter.office2PDF(sourcePath, destFile); 
   * </pre> 
   *  
   * @param sourceFile 
   *      源文件, 絕對路徑. 可以是Office2003-2007全部格式的文檔, Office2010的沒測試. 包括.doc, 
   *      .docx, .xls, .xlsx, .ppt, .pptx等. 示例: F:\\office\\source.doc 
   * @param destFile 
   *      目標(biāo)文件. 絕對路徑. 示例: F:\\pdf\\dest.pdf 
   * @return 操作成功與否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置錯(cuò)誤; 如果返回 0, 
   *     則表示操作成功; 返回1, 則表示轉(zhuǎn)換失敗 
   */
	public static int office2PDF(String sourceFile, String destFile) {
		try {
			File inputFile = new File(sourceFile);
			if (!inputFile.exists()) {
				return -1;
				// 找不到源文件, 則返回-1
			}
			// 如果目標(biāo)路徑不存在, 則新建該路徑  
			File outputFile = new File(destFile);
			if (!outputFile.getParentFile().exists()) {
				outputFile.getParentFile().mkdirs();
			}
			String OpenOffice_HOME = openOfficePath;
			//這里是OpenOffice的安裝目錄  
			// 如果從文件中讀取的URL地址最后一個(gè)字符不是 '\',則添加'\'  
			if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '\\') {
				OpenOffice_HOME += "\\";
			}
			// 啟動OpenOffice的服務(wù)  
			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 = new OpenOfficeDocumentConverter(  
			          connection);
			converter.convert(inputFile, outputFile);
			// close the connection  
			connection.disconnect();
			// 關(guān)閉OpenOffice服務(wù)的進(jìn)程  
			pro.destroy();
			return 0;
		}
		catch (FileNotFoundException e) {
			e.printStackTrace();
			return -1;
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		return 1;
	}
	public static void main(String []args) throws Exception {
		String sourcePath = "C:\\Users\\Administrator\\Desktop\\1\\分組情況一覽表.xls";
		String destFile = "C:\\Users\\Administrator\\Desktop\\1\\dest.pdf";
		int flag = Converter.office2PDF(sourcePath, destFile);
		if (flag == 1) {
			System.out.println("轉(zhuǎn)化失敗");
		} else if(flag == 0){
			System.out.println("轉(zhuǎn)化成功");
		} else {
			System.out.println("找不到源文件, 或url.properties配置錯(cuò)誤");
		}
	}
}

到此這篇關(guān)于怎么在Java中利用openoffice將doc、docx轉(zhuǎn)為pdf的文章就介紹到這了,更多相關(guān)的內(nèi)容請搜索創(chuàng)新互聯(lián)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持創(chuàng)新互聯(lián)!

當(dāng)前文章:怎么在Java中利用openoffice將doc、docx轉(zhuǎn)為pdf
網(wǎng)站路徑:http://chinadenli.net/article42/ppsoec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站網(wǎng)站設(shè)計(jì)公司搜索引擎優(yōu)化全網(wǎng)營銷推廣網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)