在网上也是找了好久才找到的一些比较好的资料,我自己总结梳理了一下,方便后面各位小伙伴使用。

1、效果图

所需的架包百度网盘:

百度链接https://pan.baidu.com/s/1oGsL7hSo71I76aQ3E7GJxA

提取码: 3axi

2、实现代码

2.1 word转pdf实现

package com.ruoyi.common.utils.file;import com.aspose.words.*;
import com.aspose.words.Shape;import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;/*** word转pdf*/
public class WordToPdfUtils {/***  word转pdf* @param inPath word文件路径* @param outPath 输出路径*/public static boolean doc2pdf(String inPath, String outPath) {FileOutputStream os =null;try {File file = new File(outPath); // 新建一个空白pdf文档os = new FileOutputStream(file);Document doc = new Document(inPath); // Address是将要被转化的word文档//添加水印//insertWatermarkText(doc,str);//保存pdf文件doc.save(os, SaveFormat.PDF);} catch (Exception e) {e.printStackTrace();return false;}finally{if(os!=null){try {os.close();} catch (IOException e) {e.printStackTrace();}}return true;}}/**** @Title: insertWatermarkText* @Description: PDF生成水印* @author mzl* @param doc* @param watermarkText* @throws Exception* @throws*/private static void insertWatermarkText(Document doc, String watermarkText) throws Exception{if (!watermarkText.equals("")&&null!=watermarkText){Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);//水印内容watermark.getTextPath().setText(watermarkText);//水印字体watermark.getTextPath().setFontFamily("宋体");//水印宽度watermark.setWidth(400);//水印高度watermark.setHeight(100);//旋转水印watermark.setRotation(-30);//水印颜色watermark.getFill().setColor(Color.lightGray);watermark.setStrokeColor(Color.lightGray);watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);watermark.setWrapType(WrapType.NONE);watermark.setVerticalAlignment(VerticalAlignment.CENTER);watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);Paragraph watermarkPara = new Paragraph(doc);watermarkPara.appendChild(watermark);for (Section sect : doc.getSections()){insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);}}}private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) throws Exception{HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);if (header == null){header = new HeaderFooter(sect.getDocument(), headerType);sect.getHeadersFooters().add(header);}header.appendChild(watermarkPara.deepClone(true));}}

2.2 excel转pdf实现

package com.ruoyi.common.utils.file;import com.aspose.cells.License;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;/*** excel转pdf帮助类*/
public class ExcelToPdfUtils {/*** excel转pdf方法* @param Address  原路径excel* @param putPath  转换pdf后的路径*/public static void excel2pdf(String Address,String putPath) {if (!getLicense()) {          // 验证License 若不验证则转化出的pdf文档会有水印产生return ;}try {File pdfFile = new File(putPath); // 输出路径Workbook wb = new Workbook(Address);// 原始excel路径FileOutputStream fileOS = new FileOutputStream(pdfFile);wb.save(fileOS, SaveFormat.PDF);fileOS.close();} catch (Exception e) {e.printStackTrace();}}public static boolean getLicense() {boolean result = false;try {InputStream is =ExcelToPdfUtils.class.getClassLoader().getResourceAsStream("license.xml"); //// license.xml这个文件你放在静态文件资源目录下就行了License aposeLic = new License();aposeLic.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();}return result;}
}

2.3 ppt转pdf实现

package com.ruoyi.common.utils.file;import com.aspose.slides.License;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;/*** ppt 转pdf  帮助类*/
public class PptToPdfUtils {private static InputStream license;/*** 获取license** @return*/public static boolean getLicense() {boolean result = false;try {license = PptToPdfUtils.class.getClassLoader().getResourceAsStream("license.xml");// license路径License aposeLic = new License();aposeLic.setLicense(license);result = true;} catch (Exception e) {e.printStackTrace();}return result;}/*** ppt 转pdf 方法* @param Address ppt原路径* @param outPath pdf转出路径*/public static void ppt2pdf(String Address,String outPath) {// 验证Licenseif (!getLicense()) {return ;}try {//   long old = System.currentTimeMillis();File file = new File(outPath);// 输出pdf路径Presentation pres = new Presentation(Address);//输入ppt路径FileOutputStream fileOS = new FileOutputStream(file);pres.save(fileOS, SaveFormat.Pdf);fileOS.close();} catch (Exception e) {e.printStackTrace();}}}

2.4 pdf转jpg、png图片帮助类

需要导入maven架包

<dependencys> <dependency><groupId>com.sleepycat</groupId><artifactId>je</artifactId><version>5.0.73</version></dependency><dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.8</version></dependency>
</dependencys> 
package com.ruoyi.common.utils.file;import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.rendering.PDFRenderer;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;/*** pdf 转图片 帮助类*/
public class PdftoImageUtils {/***  pdf 转图片方法* @param address pdf原文件地址* @param toImagepath  转换后图片存放地址* @return   图片地址集合* @throws Exception*/public static List<String> pdfToImageFile(String address, String toImagepath) throws Exception {PDDocument doc = null;ByteArrayOutputStream os = null;InputStream stream = null;OutputStream out = null;ArrayList<String> strings = new ArrayList<>();try {// pdf路径stream = new FileInputStream(address);// 加载解析PDF文件doc = PDDocument.load(stream);PDFRenderer pdfRenderer = new PDFRenderer(doc);PDPageTree pages = doc.getPages();int pageCount = pages.getCount();for (int i = 0; i < pageCount; i++) {BufferedImage bim = pdfRenderer.renderImageWithDPI(i, 200);os = new ByteArrayOutputStream();ImageIO.write(bim, "jpg", os);byte[] dataList = os.toByteArray();//获取当前时间  保存图片规则Date date = new Date();SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式String format = dateFormat.format(date).replace(":","/");int anInt = new Random().nextInt(1000);  //随机数String imageAddress=toImagepath+"/"+format+"/hello_" + anInt + ".jpg";strings.add(imageAddress);// jpg文件转出路径File file = new File(imageAddress);if (!file.getParentFile().exists()) {// 不存在则创建父目录及子文件file.getParentFile().mkdirs();file.createNewFile();}out = new FileOutputStream(file);out.write(dataList);}return strings;} catch (Exception e) {e.printStackTrace();throw e;} finally {if (doc != null) doc.close();if (os != null) os.close();if (stream != null) stream.close();if (out != null) out.close();}}
}

鄙人才疏学浅,希望对你们有帮助,谢谢!

Java实现word、excel、ppt转pdf文件,pdf转图片(无水印)相关推荐

  1. Word,Excel,PPT等Office文件Web浏览器在线预览

    博主联系方式   https://fizzz.blog.csdn.net/article/details/113049879 前两天接到一个需求:需要在线预览用户上传的Word,Excel,PPT文档 ...

  2. 分享一个java对xml,excel,jdbc.properties,读写文件,读写图片等实现(1)

    2019独角兽企业重金招聘Python工程师标准>>> //定时器 package ThreadTime; import java.util.Calendar; import jav ...

  3. office2016安装后新建图标(word\excel\ppt)等文件图标均显示白色

    虽然激活的2016打开也可以输入文件,但是图标看着很不舒服! 找了找 Excel文件不显示图标的第一种方式:图标未知 如下面的截图,一个Excel文档,在桌面上预览,可以看到excel文件不显示图标. ...

  4. Java准确获取Word/Excel/PPT/PDF的页数(附Word页数读不准的处理办法)

    Java准确获取Word/Excel/PPT/PDF的页数(附Word页数读不准的处理办法) 1.需求背景 2.环境准备工作 2.1 JACOB介绍及安装 2.2 Microsoft Office W ...

  5. php word excel转pdf文件怎么打开,php office文件(word/excel/ppt)转pdf文件,pptpdf

    php office文件(word/excel/ppt)转pdf文件,pptpdf 把代码放到了github上,点击进入 前阶段有个项目用到了线上预览功能, 关于预览office文件实现核心就是,把o ...

  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. Python办公自动化(八)|使用Python转换PDF,Word/Excel/PPT/md/HTML都能转

    Word转PDF Word转PDF应该是最常见的需求了,毕竟使用PDF格式可以更方便展示文档,虽然在Word中可以直接导出为PDF格式,但是使用Python可以批量转换,更加高效. 目前在Python ...

  8. 文末福利|使用Python转换PDF,Word/Excel/PPT/md/HTML都能转!

    往期精选 Python办公自动化|从Word到Excel Python办公自动化|从Excel到Word Python办公自动化|对比文件,光速完成 Python办公自动化|Excel表格,自动更新 ...

  9. 使用Python转换PDF,Word/Excel/PPT/md/HTML都能转!

    同一个操作执行两次,就要考虑自动化! 大家好,又到了Python办公自动化专题. 今天讲的是各位一定会接触到的PDF转换,关于各种格式的文件转换为PDF有很多第三方工具与网站可以实现,但是使用Pyth ...

  10. python把excel变成ppt_【转】使用Python转换PDF,Word/Excel/PPT/md/HTML都能转!

    今天讲的是各位一定会接触到的PDF转换,关于各种格式的文件转换为PDF有很多第三方工具与网站可以实现,但是使用Python的好处不仅可以批量转换,同时一旦脚本写完了以后就可以一键执行,彻底解放双手,那 ...

最新文章

  1. PMP 学习之一:PMP五大过程组十大知识领域47个子过程
  2. Bag-of-words model
  3. 将一个c 语言源程序文件中所有注释去掉后,存入另一个文件.,C实验内容.doc
  4. 起底《最后生还者2》:开发者们设计剧情应该规避的误区
  5. 2016年2月流量入口占比动态:搜索引擎大涨2.14%
  6. 微信小程序进度条详解 progress 自定圆形进度条
  7. 专科计算机组成原理大一试题及答案,计算机组成原理专科试题答案.doc
  8. 如何使用Transact-SQL进行事务处理[示例]
  9. 学习网络编程推荐安装的软件
  10. 随机搜索RandomizedSearchCV原理
  11. 无线模块发送接收笔记
  12. 锁仓怎么解_锁仓和解锁的方法
  13. 【C语言必经之路——第13节】C语言中的数据类型详解
  14. 什么是值传递,什么是引用传递。为什么说Java中只有值传递。
  15. 浅谈SPICE原理及应用
  16. 交换机Access端口,Trunk端口与Hybrid的区别与应用
  17. 直接收藏-超级好用的国内配色网站
  18. Esp8266的Flash读写操作以及Flash上传文件
  19. gitlab-ci添加安卓项目构建流程
  20. 1. 医院的就诊流程是怎样的?

热门文章

  1. vue项目中 集成plus
  2. 为什么要使用服务发现
  3. 初级会计实务--第三章第二节、应付及预收账款
  4. 五天搞定英语语法系列汇总
  5. 偏向锁、轻量级锁、重量级锁的区别和解析
  6. html情侣计时器,手机桌面恋爱计时器
  7. BOM系列之Navigator对象
  8. 大题历年题合集-信息资源管理
  9. 正交、独立、不相关区别
  10. vSphere ESXI 7.0镜像 Rufus U盘安装盘制作(Windows)