某日,直属上级说:你去了解一下OpenOffice周三搞明白,然后我就开始看这方面的文档。总结一下踩到的坑和一些经验吧。
去官网上可以看到最新的版本是4.7.1了,下载就好了,默认安装路径:C:\Program Files (x86)\OpenOffice 4\program 然后傻瓜安装就可以,这个路径代码中多次会用到,注意留意。
在javaWeb中使用的话需要打开相关服务端口,简单,win+R cmd 逐行输入就好了,下面是命令:
//进入目录
cd C:\Program Files (x86)\OpenOffice 4\program
//执行
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
//查看是否启动成功
netstat -ano|findstr "8100"
出现端口信息127.0.0.1 8100什么的即可
不多说,代码附下(某位大神代码):
import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.ExternalOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeException;
import org.artofsolving.jodconverter.office.OfficeManager;
import java.io.File;
import java.util.Scanner;
public class ChangePDF {
private static final Logger logger = Logger.getLogger(ChangePDF.class.getName());
//OpenOffice安装路径,默认是 C:/Program Files (x86)/OpenOffice 4/
private static String officeHome = "C:\\Program Files (x86)\\OpenOffice 4\\";
//windows系统默认开启端口号8100
@SuppressWarnings("static-access")
private static int port = 8100;
//连接服务器
private static OfficeManager officeManager;
private static boolean reconnect() {
try {
ExternalOfficeManagerConfiguration externalProcessOfficeManager = new ExternalOfficeManagerConfiguration();
externalProcessOfficeManager.setConnectOnStart(true);
externalProcessOfficeManager.setPortNumber(8100);
officeManager = externalProcessOfficeManager.buildOfficeManager();
officeManager.start();
return true;
} catch (OfficeException e) {
e.printStackTrace();
return false;
}
}
//开启新的OpenOffice进程
private static void start() {
// logger.debug("启动OpenOffice服务");
logger.debug("启动OpenOffice服务");
try {
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
configuration.setOfficeHome(officeHome);//安装地址
configuration.setPortNumbers(port);//端口
configuration.setTaskExecutionTimeout(1000 * 60 * 5);//设置任务执行超时5min
configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24);//设置任务队列超时24h
officeManager = configuration.buildOfficeManager();
officeManager.start();
} catch (NullPointerException e) {
e.printStackTrace();
logger.error("启动OpenOffice服务出错" + e);
} catch (IllegalArgumentException e) {
e.printStackTrace();
logger.error("启动OpenOffice服务出错" + e);
} catch (IllegalStateException e) {
e.printStackTrace();
logger.error("启动OpenOffice服务出错" + e);
} catch (OfficeException e) {
e.printStackTrace();
logger.error("启动OpenOffice服务出错" + e);
}
}
//关闭进程
private static void stop() {
logger.debug("关闭OpenOffice服务");
try {
if (officeManager != null) {
officeManager.stop();
}
} catch (OfficeException e) {
e.printStackTrace();
logger.error("关闭OpenOffice服务出错" + e);
}
}
//1.PPT/ --> PDF
public static File convertToPdf(String input){
File inputFile = null;
File outFile = null;
//服务异常就开启新服务
try {
if (!reconnect()){
start();
}
//FilenameUtils.separatorsToSystem:转换分隔符为当前系统分隔符
// FilenameUtils.getFullPath:获取文件的完整目录
// FilenameUtils.getBaseName:取出文件目录和后缀名的文件名
String output = FilenameUtils.separatorsToSystem(FilenameUtils.getFullPath(input) + FilenameUtils.getBaseName(input) + ".pdf");
inputFile = new File(input);
outFile = new File(output);
logger.info("开始转换文档:" + input + "==>" + output);
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
converter.convert(inputFile,outFile);
} catch (OfficeException e) {
e.printStackTrace();
logger.error("转换文档出错:" + e);
outFile = null;
}finally {
logger.info("结束转换文档。");
stop();
}
return outFile;
}
public static void main(String[] args) {
//1
Scanner sc = new Scanner(System.in);
System.out.println("请输入所需浏览的文件路径和文件名:");
String s = sc.nextLine();
ChangePDF.convertToPdf(s);
//2
// ChangePDF.convertToPdf("G:\\DataText\\OpenOff\\A.ppt");
}
}
有些你可能用不到,但是可以导自己库中以后也省事了
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Youyi</groupId>
<artifactId>OpenOff</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>OpenOff Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>jurt</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>ridl</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>juh</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>unoil</artifactId>
<version>3.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.artofsolving.jodconverter/jodconverter-core -->
<dependency>
<groupId>org.artofsolving.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>3.0-beta-4</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<finalName>OpenOff</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
要使用pdf.js技术,可以在我提供链接下载源码
1、将pdfjs文件夹放在项目webapp下
2、创建html文档,设置好自己的路径
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<input type="button" onclick="openPDF()" value="预览文件"/>
<script>
function openPDF(){
var url="/OpenOff/src/main/webapp/A.pdf";
window.open("pdfjs/web/viewer.html?file=" + url);
}
script>
body>
html>
代码在连接上,但是前后台的响应还没有实现,大家先看主体部分吧。
链接:https://pan.baidu.com/s/1HDTYSKLkHaFkv1RktZOxUg
提取码:gve2
这个我查了很多资料,好像并不能实现在线的编辑,有想法的小伙伴欢迎留言。