-
大小: 1.9MB文件類型: .zip金幣: 2下載: 0 次發(fā)布日期: 2023-10-01
- 語言: Java
- 標簽: 轉(zhuǎn)pdf??
資源簡介
Java使用open office實現(xiàn)各種文檔轉(zhuǎn)pdf ,簡單明了sssss

代碼片段和文件信息
package?com.achk.all2Pdf;
import?java.io.File;
import?java.io.FileNotFoundException;
import?java.io.IOException;
import?java.net.ConnectException;
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?Word2PDF?{
/**?
?????*?將Office文檔轉(zhuǎn)換為PDF.?運行該函數(shù)需要用到OpenOffice?OpenOffice下載地址為?
?????*?http://www.openoffice.org/?
?????*??
?????*??
?????*?方法示例:?
?????*?String?sourcePath?=?“F:\\office\\source.doc“;?
?????*?String?destFile?=?“F:\\pdf\\dest.pdf“;?
?????*?Converter.office2PDF(sourcePath?destFile);?
?????*?
?
?????*??
?????*?@param?sourceFile?
?????*????????????源文件?絕對路徑.?可以是Office2003-2007全部格式的文檔?Office2010的沒測試.?包括.doc?
?????*????????????.docx?.xls?.xlsx?.ppt?.pptx?.txt?等.?示例:?F:\\office\\source.doc?
?????*?@param?destFile?
?????*????????????目標文件.?絕對路徑.?示例:?F:\\pdf\\dest.pdf?
?????*?@return?操作成功與否的提示信息.?如果返回?-1?表示找不到源文件?或url.properties配置錯誤;?如果返回?0?
?????*?????????則表示操作成功;?返回1?則表示轉(zhuǎn)換失敗?
?????*/??
????public?static?int?office2PDF(String?sourceFile?String?destFile)?{??
????????try?{??
????????????File?inputFile?=?new?File(sourceFile);??
????????????if?(!inputFile.exists())?{??
????????????????return?-1;//?找不到源文件?則返回-1??
????????????}??
??
????????????//?如果目標路徑不存在?則新建該路徑??
????????????File?outputFile?=?new?File(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=\“sockethost=127.0.0.1port=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();??
????????????//?關閉OpenOffice服務的進程??
????????????pro.destroy();??
??
????????????return?0;??
????????}?catch?(FileNotFoundException?e)?{??
????????????e.printStackTrace();?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2017-12-12?09:52??allToPDF\
?????文件?????????588??2017-12-12?09:52??allToPDF\.classpath
?????文件????????1041??2017-12-12?09:52??allToPDF\.project
?????目錄???????????0??2017-12-12?09:52??allToPDF\.settings\
?????文件?????????503??2017-12-12?09:52??allToPDF\.settings\.jsdtscope
?????文件?????????364??2017-12-12?09:52??allToPDF\.settings\org.eclipse.jdt.core.prefs
?????文件?????????476??2017-12-12?09:52??allToPDF\.settings\org.eclipse.wst.common.component
?????文件?????????305??2017-12-12?09:52??allToPDF\.settings\org.eclipse.wst.common.project.facet.core.xm
?????文件??????????49??2017-12-12?09:52??allToPDF\.settings\org.eclipse.wst.jsdt.ui.superType.container
?????文件???????????6??2017-12-12?09:52??allToPDF\.settings\org.eclipse.wst.jsdt.ui.superType.name
?????目錄???????????0??2017-12-12?09:52??allToPDF\build\
?????目錄???????????0??2017-12-13?10:05??allToPDF\build\classes\
?????目錄???????????0??2017-12-13?10:05??allToPDF\build\classes\com\
?????目錄???????????0??2017-12-13?10:31??allToPDF\build\classes\com\achk\
?????目錄???????????0??2017-12-13?10:31??allToPDF\build\classes\com\achk\all2Pdf\
?????文件????????3058??2017-12-13?10:31??allToPDF\build\classes\com\achk\all2Pdf\Word2PDF.class
?????目錄???????????0??2017-12-12?09:52??allToPDF\src\
?????目錄???????????0??2017-12-12?09:52??allToPDF\src\com\
?????目錄???????????0??2017-12-13?10:31??allToPDF\src\com\achk\
?????目錄???????????0??2017-12-13?09:25??allToPDF\src\com\achk\all2Pdf\
?????文件????????4052??2017-12-13?10:31??allToPDF\src\com\achk\all2Pdf\Word2PDF.java
?????目錄???????????0??2017-12-12?09:52??allToPDF\WebContent\
?????目錄???????????0??2017-12-12?09:52??allToPDF\WebContent\me
?????文件??????????39??2017-12-12?09:52??allToPDF\WebContent\me
?????目錄???????????0??2017-12-12?09:52??allToPDF\WebContent\WEB-INF\
?????目錄???????????0??2017-12-13?10:05??allToPDF\WebContent\WEB-INF\lib\
?????文件???????41123??2017-12-13?10:05??allToPDF\WebContent\WEB-INF\lib\commons-cli-1.2.jar
?????文件??????109043??2017-12-13?10:05??allToPDF\WebContent\WEB-INF\lib\commons-io-1.4.jar
?????文件???????32054??2017-12-13?10:05??allToPDF\WebContent\WEB-INF\lib\jodconverter-2.2.2.jar
?????文件????????5712??2017-12-13?10:05??allToPDF\WebContent\WEB-INF\lib\jodconverter-cli-2.2.2.jar
?????文件???????55622??2017-12-13?10:05??allToPDF\WebContent\WEB-INF\lib\juh-3.0.1.jar
............此處省略7個文件信息
評論
共有 條評論