声明
郑重声明本文来自CMDN jim先生
原创网址:[https://blog.csdn.net/u011967234/article/details/72760008](https://blog.csdn.net/u011967234/article/details/72760008)
本人感觉 jim先生写的清楚明了,对自己有用以方便以后使用抄录过来。
环境
下载Windows版本OpenOffice;
官网地址:http://www.openoffice.org/download/index.html。
代码实现
//main方法测试
public static void main(String[] args) {
convertWord2Pdf("C:\\Users\\Desktop\\2020-xxxxx.docx");
}
/**
*
* @description:将Office格式的文档转换为PDF格式的文档
* @param inputFilePath
*
*/
public static void convertWord2Pdf(String inputFilePath) {
DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
// OpenOffice安装在本地环境的目录
String officeHome = "D:\\OpenOffice 4";
config.setOfficeHome(officeHome);
OfficeManager officeManager = config.buildOfficeManager();
officeManager.start();
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
String outputFilePath = getReplaceFileAbsolutePath(inputFilePath, ".pdf");
File inputFile = new File(inputFilePath);
if (inputFile.exists()) {
File outputFile = new File(outputFilePath);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
// 进行PDF格式的转换
converter.convert(inputFile, outputFile);
}
officeManager.stop();
}
/**
*
* @description:更改文档后缀为指定的后缀名
* @param inputFilePath 输入文档的绝对路径
* @param replaceEndWith 指定的后缀名
* @return 返回替换指定后缀名的文档的绝对路径
*
*/
private static String getReplaceFileAbsolutePath(String inputFilePath, String replaceEndWith) {
String replaceFilePath = null;
Pattern pattern = Pattern.compile("(\\.[a-zA-Z]+)");
Matcher matcher = pattern.matcher(inputFilePath);
String endWith = null;
if(matcher.find()) {
endWith = matcher.group(1);
}
if(StringUtil.isEmpty(endWith)) {
return null;
}
replaceFilePath = inputFilePath.replaceAll(endWith, replaceEndWith);
return replaceFilePath;
}
/**
*
* @description:预览PDF文件
* @param attathFile(文件流)
* @param response
*
*/
public static void previewPdf(File attathFile, HttpServletResponse response) {
response.setContentType("application/pdf");
try {
if(attathFile.exists()) {
DataOutputStream dataOutputStream = new DataOutputStream(response.getOutputStream());
DataInputStream dataInputStream = new DataInputStream(new FileInputStream(attathFile));
byte[] buffer = new byte[2048];
int len = buffer.length;
while((len = dataInputStream.read(buffer, 0, len)) != -1) {
dataOutputStream.write(buffer);
dataOutputStream.flush();
}
dataInputStream.close();
dataOutputStream.close();
}
} catch (Exception e) {
/*logger.error(e.getMessage(), e);*/
e.printStackTrace();
}
}
需要准备的maven依赖
<!-- 在线预览office文件 start-->
<!-- https://mvnrepository.com/artifact/commons-cli/commons-cli -->
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.artofsolving/jodconverter -->
<dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openoffice/juh -->
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>juh</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openoffice/jurt -->
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>jurt</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openoffice/ridl -->
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>ridl</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-jdk14 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openoffice/unoil -->
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>unoil</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>com.github.livesense</groupId>
<artifactId>jodconverter-core</artifactId>
<version>1.0.5</version>
</dependency>
<!-- 在线预览office文件 end-->