pom依赖:

只有jodconverter支持docx,所以得去官网下载该包并放到私库,其他包不需要特殊处理。

  <dependency><groupId>com.documents4j</groupId><artifactId>documents4j-local</artifactId><version>1.0.3</version></dependency><dependency><groupId>com.documents4j</groupId><artifactId>documents4j-transformer-msoffice-word</artifactId><version>1.0.3</version></dependency><dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.17</version></dependency><dependency><groupId>org.apache.pdfbox</groupId><artifactId>fontbox</artifactId><version>2.0.17</version></dependency>
<dependency><groupId>com.artofsolving</groupId><artifactId>jodconverter</artifactId><version>2.2.2</version>
</dependency>
<dependency><groupId>org.openoffice</groupId><artifactId>juh</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>unoil</artifactId><version>3.0.1</version>
</dependency>

代码:

package com.insigma.facade.workflow.util;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 org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.ConnectException;
import java.net.URL;
import java.net.URLConnection;/*** @author maoMingHui* @Title: FileTransType* @Description:* @date 2020/3/19 9:18*/
public class FileTransType {//pdf文件转图片public static InputStream pdf2Image(File file, int dpi) throws Exception {InputStream is = null;ByteArrayOutputStream bs = new ByteArrayOutputStream();ImageOutputStream imOut;PDDocument pdDocument = PDDocument.load(file);PDFRenderer renderer = new PDFRenderer(pdDocument);BufferedImage image = renderer.renderImageWithDPI(0, dpi);imOut = ImageIO.createImageOutputStream(bs);ImageIO.write(image, "png", imOut);image.flush();is = new ByteArrayInputStream(bs.toByteArray());is.close();return is;}// word文件转pdfpublic static void word2Pdf(File sourceFile, File pdfFile) throws Exception {OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);try {connection.connect();DocumentConverter converter = new OpenOfficeDocumentConverter(connection);converter.convert(sourceFile, pdfFile);connection.disconnect();System.out.println("---------------第二步:转换为PDF格式 ,路径:" + pdfFile.getPath());} catch (java.net.ConnectException e) {e.printStackTrace();System.out.println("-----------------OpenOffice服务未启动");try {throw e;} catch (ConnectException e1) {e1.printStackTrace();}} catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {e.printStackTrace();System.out.println("----------------读取文件失败");throw e;} catch (Exception e) {e.printStackTrace();try {throw e;} catch (Exception e1) {e1.printStackTrace();}}}public static InputStream download(String urlString) throws Exception {// 构造URLURL url = new URL(urlString);// 打开连接URLConnection con = url.openConnection();//设置请求超时为5scon.setConnectTimeout(5 * 1000);// 输入流InputStream is = con.getInputStream();return is;}public static void inputStreamToFile(InputStream ins, File file) throws Exception {OutputStream os = new FileOutputStream(file);int bytesRead = 0;byte[] buffer = new byte[8192];while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {os.write(buffer, 0, bytesRead);}os.close();ins.close();}public static void pdf2png(File pdfFile, File pdgFile) {Integer SEPARATE_DISTANCE = 10;try {PDDocument doc = PDDocument.load(pdfFile);PDFRenderer renderer = new PDFRenderer(doc);int pageCount = doc.getNumberOfPages();BufferedImage imageNew = null;for (int i = 0; i < pageCount; i++) {BufferedImage image = renderer.renderImageWithDPI(i, 284);int width = image.getWidth();int height = image.getHeight();if (imageNew == null) {imageNew = new BufferedImage(width, (height + SEPARATE_DISTANCE) * pageCount,BufferedImage.TYPE_INT_RGB);}int[] imageRgbArray = new int[width * height];imageRgbArray = image.getRGB(0, 0, width, height, imageRgbArray, 0, width);imageNew.setRGB(0, (height + SEPARATE_DISTANCE) * i, width, height, imageRgbArray, 0, width);// SEPARATE_DISTANCE表示两张图片的间隔距离}ImageIO.write(imageNew, "PNG", pdgFile);} catch (Exception e) {e.printStackTrace();}}
}

服务器需要:

1.安装Apache OpenOffice

Apache OpenOffice-CentOs下的安装方法​blog.csdn.net

2.安装中文字体

CSDN-专业IT技术社区-登录​blog.csdn.net

效果图:

2 str转byte失败_linux服务器,JAVA进行word转pdf相关推荐

  1. [JAVA使用技巧]Java抽取Word和PDF格式文件_网络大本营

    Java抽取Word和PDF格式文件的四种武器(1) 很多人用java进行文档操作时经常会遇到一个问题,就是如何获得word,excel,pdf等文档的内容?我研究了一下,在这里总结一下抽取word, ...

  2. java实现word、pdf文件下载功能

    在SpringMVC的开发过程中,有时需要实现文档的下载功能.文档的下载功能涉及到了java IO流操作的基础知识,下面本文详细介绍java如何实现后台文档下载功能. 首先根据文档在项目中的存储路径建 ...

  3. java实现word转pdf在线预览格式

    java实现word转pdf在线预览格式 前段时间的项目里涉及了此功能,调研过一些方案,踩过一些坑,一一总结在此. java转pdf的方案很多,但是很多都要收费,转pdf也有一些格式方面的问题. 方案 ...

  4. Linux系统下Java 转换Word到PDF时,结果文档内容乱码的解决方法

    本文分享在Linux系统下,通过Java 程序代码将Word转为PDF文档时,结果文档内容出现乱码该如何解决.具体可参考如下内容: 1.问题出现的背景 在Windows系统中,使用Spire.Doc ...

  5. Aspose.Java实现word转pdf,添加水印等操作

    Aspose.Java实现word转pdf,添加水印等操作 一. word转pdf 二. 文档插入水印 Aspose是一款商用版控件,支持各类文档操作,这里主要介绍如何在Springboot项目中使用 ...

  6. txt doc rtf html,JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例.docx

    JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例 JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例??2012-06-2 ...

  7. java 模板 word转pdf 可分页 带图片

    java 模板 word转pdf 可分页 带图片 之前写过一个简单的案例,但是在项目中完全不能满足客户的需求,所以重新用啦一种方式来写,采用了word转换pdf的方式,这种经过不断研究,满足了可分页, ...

  8. Java实现Word转PDF方案选择

    Java实现Word转PDF方案选择 很多应用场景中都会涉及到Word转PDF,但Word转PDF的方案在网上一搜一大把,让人眼花缭乱,笔者踩过无数的坑后,最终总结出以下三种方案 OpenOffice ...

  9. JAVA POI Word转PDF convert方法 NullPointException

    JAVA POI Word转PDF convert方法 NullPointException 如果操作过通过POI操作过Word,请保证创建run之后run的值不为null,为null将在转换时报错. ...

  10. linux java进程消失_Linux服务器Java进程消失问题解决

    Linux服务器Java进程消失问题解决 发布时间:2020-08-20 15:17:37 来源:脚本之家 阅读:65 作者:myseries 这篇文章主要介绍了Linux服务器Java进程消失问题解 ...

最新文章

  1. 我的Linux生涯之文件链接
  2. java web一: xml
  3. 基础篇:事件的发送和处理
  4. C:简单的学生信息处理程序实现
  5. 使用序列化实现对象的拷贝(转载)
  6. 使用std::cout不能输出显示
  7. 突发!央行确认支付宝3种违法违规行为,处罚18万元!
  8. mysql记录当前表数据的数据条数据类型_mysql的表的操作 数据类型
  9. SVN可视化管理工具——Subversion Edge使用
  10. 使用 CRF 做中文分词
  11. TensorFlow Keras 官方文档中文版文档学习
  12. The Hidden Agenda User Simulation Model翻译
  13. 一度智信:拼多多开店必备条件
  14. Open-Vocabulary Object Detection Using Captions(2021 CVPR)----论文解读
  15. “七剂中医”治疗原理
  16. Exchange 2010 修改附件大小限制
  17. 麦克风阵列之一阶差分麦克风阵列
  18. 如何做好企业网站优化与推广工作
  19. java学习笔记02
  20. mapreduce实现ItemCF——基于物品的协同过滤

热门文章

  1. 【DCVRP】基于matlab蚁群算法求解带容量+距离的车辆路径规划问题【含Matlab源码 1038期】
  2. 【数学建模】基于matlab计划生育政策调整对人口数量、结构及其影响的研究【含Matlab源码 749期】
  3. 【缺陷识别】基于matlab GUI SVM金属表面缺陷分类与测量【含Matlab源码 682期】
  4. 【PM2.5预测】基于matlab灰色模型PM2.5预测【含Matlab源码 499期】
  5. web与ai相结合成为趋势_将AI和行为科学相结合可以改变体验
  6. 部署容器jenkins_使用Jenkins部署用于进行头盔检测的烧瓶容器
  7. ef联表查询速度_这个列式数据库牛!20亿行的查询,1s完成
  8. Latex:使用latex双栏模板时,图片caption名称不显示
  9. 例4.6 素数判定 - 九度教程第50题(素数筛法)
  10. c++优化后的快速排序