日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

office文件转换为pdf文件

發布時間:2024/1/8 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 office文件转换为pdf文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

office文件轉換為pdf文件

  • 首先安裝openoffice,自行百度
  • 導入jar包,自行百度
  • 引入坐標:
  • <!-- 轉pdf配置 --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependency><dependency><groupId>org.openoffice</groupId><artifactId>juh</artifactId><version>3.2.1</version></dependency><dependency><groupId>org.openoffice</groupId><artifactId>ridl</artifactId><version>3.2.1</version></dependency><dependency><groupId>org.openoffice</groupId><artifactId>unoil</artifactId><version>3.2.1</version></dependency><dependency><groupId>commons-cli</groupId><artifactId>commons-cli</artifactId><version>1.1</version><optional>true</optional></dependency><dependency><groupId>com.thoughtworks.xstream</groupId><artifactId>xstream</artifactId><version>1.3.1</version></dependency><dependency><groupId>com.artofsolving</groupId><artifactId>jodconverter</artifactId><version>2.2.2</version><scope>system</scope> <!--system,類似provided,需要顯式提供依賴的jar以后,Maven就不會在Repository中查找它--><systemPath>${basedir}/src/main/resources/lib/jodconverter-2.2.2.jar</systemPath> <!--項目根目錄下的lib文件夾下--></dependency>
  • 代碼實現
  • 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;import java.io.*; import java.io.FileNotFoundException; import java.io.IOException; import java.rmi.*;public class Office2PdfUtils {/*** 將Office文檔轉換為PDF. 運行該函數需要用到OpenOffice, OpenOffice下載地址為* http://www.openoffice.org/** <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, .txt 等. 示例: F:\\office\\source.doc* @param destFile* 目標文件. 絕對路徑. 示例: F:\\pdf\\dest.pdf* @return 操作成功與否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置錯誤; 如果返回 0,* 則表示操作成功; 返回1, 則表示轉換失敗*/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=\"socket,host=127.0.0.1,port=8100;urp;\"";Process pro = Runtime.getRuntime().exec(command);// connect to an OpenOffice.org instance running on port 8100OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);connection.connect();// convertDocumentConverter converter = new OpenOfficeDocumentConverter(connection);converter.convert(inputFile, outputFile);// close the connectionconnection.disconnect();// 關閉OpenOffice服務的進程pro.destroy();return 0;} catch (FileNotFoundException e) {e.printStackTrace();return -1;} catch (ConnectException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return 1;}public static void main(String[] args) {String sourcePath = "C:\\Users\\admin\\Desktop\\demo\\測試.pptx";String destFile = "C:\\Users\\admin\\Desktop\\demo\\測試.pdf";office2PDF(sourcePath, destFile);} }
  • 運行時遇到的問題:FilenameUtils問題
  • <dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependency>

    引入坐標即可正常運行。

  • 打包時遇到的問題:找不到jar包
  • <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><includeSystemScope>true</includeSystemScope></configuration></plugin></plugins></build>

    修改對應代碼塊,該配置的意思就是maven打包時會將外部引入的jar包打包到項目中。

    總結

    以上是生活随笔為你收集整理的office文件转换为pdf文件的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。