生活随笔
收集整理的這篇文章主要介紹了
office文件转换为pdf文件
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
office文件轉(zhuǎn)換為pdf文件
首先安裝openoffice,自行百度導(dǎo)入jar包,自行百度
引入坐標(biāo):
<!-- 轉(zhuǎn)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,類(lèi)似provided,需要顯式提供依賴(lài)的jar以后,
Maven就不會(huì)在
Repository中查找它
--><systemPath>$
{basedir
}/src
/main
/resources
/lib
/jodconverter
-2.2.2.jar
</systemPath
> <!--項(xiàng)目根目錄下的lib文件夾下
--></dependency
>
代碼實(shí)現(xiàn)
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 {public static int office2PDF(String sourceFile
, String destFile
) {try {File inputFile
= new File(sourceFile
);if (!inputFile
.exists()) {return -1;}File outputFile
= new File(destFile
);if (!outputFile
.getParentFile().exists()) {outputFile
.getParentFile().mkdirs();}String OpenOffice_HOME = "C:\\Program Files (x86)\\OpenOffice 4";if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '\\') {OpenOffice_HOME += "\\";}String command
= OpenOffice_HOME+ "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";Process pro
= Runtime.getRuntime().exec(command
);OpenOfficeConnection connection
= new SocketOpenOfficeConnection("127.0.0.1", 8100);connection
.connect();DocumentConverter converter
= new OpenOfficeDocumentConverter(connection
);converter
.convert(inputFile
, outputFile
);connection
.disconnect();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\\測(cè)試.pptx";String destFile
= "C:\\Users\\admin\\Desktop\\demo\\測(cè)試.pdf";office2PDF(sourcePath
, destFile
);}
}
運(yùn)行時(shí)遇到的問(wèn)題:FilenameUtils問(wèn)題
<dependency><groupId>commons
-io
</groupId
><artifactId>commons
-io
</artifactId
><version>2.4</version
></dependency
>
引入坐標(biāo)即可正常運(yùn)行。
打包時(shí)遇到的問(wèn)題:找不到j(luò)ar包
<build><plugins><plugin><groupId>org
.springframework
.boot
</groupId
><artifactId>spring
-boot
-maven
-plugin
</artifactId
><configuration><includeSystemScope>true</includeSystemScope
></configuration
></plugin
></plugins
></build
>
修改對(duì)應(yīng)代碼塊,該配置的意思就是maven打包時(shí)會(huì)將外部引入的jar包打包到項(xiàng)目中。
總結(jié)
以上是生活随笔為你收集整理的office文件转换为pdf文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。