Java代码实现WORD转PDF
第一步:
安装OpenOffice 在此良心提供windows版本安装文件
链接:https://pan.baidu.com/s/17pPCkcS1C46VtLhevqSgPw 密码:vmlu
安装就一直点下一步即可。
安装完成后,进入OpenOffice安装目录
安装目录一般为C:ProgramFiles(x86)/OpenOffice 4/program/
执行下面命令:
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"
第二步:
JAVA代码实现
这里在SpringBoot基础上写了个接口,可以直接通过项目调用
package com.test.controller; 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.StreamOpenOfficeDocumentConverter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import java.io.File;
import java.net.ConnectException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date; /**
* Created by jlm on 2019-05-07 11:46
*/
@RestController
@RequestMapping("/test")
public class WordToPdfController { @RequestMapping(value = "toPdf.do", method = RequestMethod.POST)
public boolean pauseToDeal(String sourceFile, String destFile) {
try {
File inputFile = new File(sourceFile);
if (!inputFile.exists()) {
// 找不到源文件, 则返回false
return false;
}
// 如果目标路径不存在, 则新建该路径
File outputFile = new File(destFile);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
//如果目标文件存在,则删除
if (outputFile.exists()) {
outputFile.delete();
}
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
connection.connect();
//用于测试openOffice连接时间
System.out.println("连接时间:" + df.format(new Date()));
DocumentConverter converter = new StreamOpenOfficeDocumentConverter(
connection);
converter.convert(inputFile, outputFile);
//测试word转PDF的转换时间
System.out.println("转换时间:" + df.format(new Date()));
connection.disconnect();
return true;
} catch (ConnectException e) {
e.printStackTrace();
System.err.println("openOffice连接失败!请检查IP,端口");
} catch (Exception e) {
e.printStackTrace();
}
return false;
} }
Maven POM文件配置
<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>
此方法适用于windos服务器下的项目,如需linux,请安装liunx版本OpenOffice
Java代码实现WORD转PDF的更多相关文章
- 如何通过 Java 代码隐藏 Word 文档中的指定段落
在编辑Word文档时,我们有时需要将一些重要信息保密. 因此,可以隐藏它们以确保机密性. 在本文中,将向您介绍如何通过 Java 程序中的代码隐藏 Word 文档中的特定段落.下面是我整理的具体步骤, ...
- 使用aspose.word两句代码将word转换为pdf
//Load Document Document document = new Document(@"C:\Users\Administrator\Desktop\人事---新员工转正总结( ...
- java代码操作word模板并生成PDF
这个博客自己现在没时间写,等后面有时间了,自己再写. 这中需求是在实际的项目开发中是会经常遇到的. 下面我们先从简单入手一步一步开始. 1.首先,使用word创建一个6行两列的表格. 点击插入-6行2 ...
- Linux系统下Java 转换Word到PDF时,结果文档内容乱码的解决方法
本文分享在Linux系统下,通过Java 程序代码将Word转为PDF文档时,结果文档内容出现乱码该如何解决.具体可参考如下内容: 1.问题出现的背景 在Windows系统中,使用Spire.Doc ...
- openOffice word转pdf,pdf转图片优化版
之前写了一个版本的,不过代码繁琐而且不好用,效率有些问题.尤其pdf转图片速度太慢.下面是优化版本的代码. spriing_boot 版本信息:2.0.1.RELEASE 1.配置信息: packag ...
- Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享
Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享 在此,先分享下写此文前的经历与感受,我所有的感觉浓缩到一个字,那就是:"坑&qu ...
- [java,2019-01-15] word转pdf
word转pdf jar包 <dependency> <groupId>org.docx4j</groupId> <artifactId>docx4j& ...
- java实现word转pdf在线预览(前端使用PDF.js;后端使用openoffice、aspose)
背景 之前一直是用户点击下载word文件到本地,然后使用office或者wps打开.需求优化,要实现可以直接在线预览,无需下载到本地然后再打开. 随后开始上网找资料,网上资料一大堆,方案也各有不同,大 ...
- 利用aspose-words 实现 java中word转pdf文件
利用aspose-words 实现 java中word转pdf文件 首先下载aspose-words-15.8.0-jdk16.jar包 引入jar包,编写Java代码 package test; ...
随机推荐
- 软考之J2SE
特别感谢软考让我如今就接触了神奇的java.曾经尽管真不知道java是个神马,看完马士兵的视频发现里面的东西并不陌生.有vb,c++,c#做基础加上这次的J2SE发现原来编程语言有非常多同样的特性.也 ...
- 99_leetcode_Best Time to Buy and sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- Razor基础,视图里如何调用controller里的函数
1.单行代码书写 @代码 2.多行代码书写 @{ //@加个大括号就可以在里面写C#代码了. C#代码第一行 C#代码第二行 } 3.Razor模板引擎会自动判别代码块,但是当代码存在二义性的时候,可 ...
- [办公应用]如何打印较小边距的PPT讲义(或者每页打印16页)
关键词:打印 PPT 讲义 4张 边距 今天同事问我如何打印PowerPoint的讲义.她自己使用PowerPoint打印讲义,设置每页4张,但是页边距太大:觉得浪费很大. 经过网上查阅后,现将方 ...
- 蜘蛛页面 获取一个网站的全部url 乐观代码
蜘蛛页面 from selenium import webdriver import time import random from bs4 import * import pymysql h, pt ...
- YTU 2505: 啤酒和饮料
2505: 啤酒和饮料 时间限制: 1 Sec 内存限制: 128 MB 提交: 850 解决: 318 题目描述 啤酒每罐2.3元,饮料每罐1.9元.小明买了若干啤酒和饮料,一共花了s元. 我们 ...
- 并不对劲的[noi2006]网络收费
题目略长,就从大视野上复制了. 听上去好像费用流,然而…… ***************************表示略长的题目的分界线************************ 1495: [ ...
- 数据仓库 VS 数据库
数据仓库(Data Warehouse)是一个面向主题的(Subject Oriented).集成的(Integrate).相对稳定的(Non-Volatile).反映历史变化(Time Varian ...
- 24. [Ext JS 4] 实战之Load Mask(加载遮罩)的显示与隐藏
转自:https://blog.csdn.net/oscar999/article/details/27176791
- 搜索goog
http://www.tech365.net/share/24.html 64.233.160.132|64.233.168.17|64.233.168.167|64.233.160.98|64.23 ...