WORD转PDF所需jar包:

https://yangtaotao.lanzous.com/ice1jlc

PDF转图片所需jar包:

https://yangtaotao.lanzous.com/ice169c

由于项目中之前就有,所以直接照搬。word转pdf,使用的是ASPOSE.word ,ASPOSE.word的licence需要收费

package com.biolims.report.service;

import java.io.FileOutputStream;
import java.io.InputStream; import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat; /**
*
* 由于ASPOSE比较吃内存,操作大一点的文件就会堆溢出,所以请先设置好java虚拟机参数:-Xms512m -Xmx512m(参考值)<br>
* 如有疑问,请在CSDN下载界面留言,或者联系QQ569925980<br>
*
* @author Spark
*
*/
public class Test { /**
* 获取license
*
* @return
*/
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = Test.class.getClassLoader().getResourceAsStream("\\license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void savedocx(String inPath, String outPath) {
if (!getLicense()) {
return;
} try {
long old = System.currentTimeMillis();
Document doc = new Document(inPath);// 原始word路径 String pdfFile = outPath;
FileOutputStream fileOS = new FileOutputStream(pdfFile); doc.save(fileOS, SaveFormat.PDF); long now = System.currentTimeMillis();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 支持DOC, DOCX, OOXML, RTF, HTML, OpenDocument, PDF, EPUB, XPS, SWF等相互转换<br>
*
* @param args
*/
public static void main(String[] args) {
// 验证License
if (!getLicense()) {
return;
} try {
long old = System.currentTimeMillis();
Document doc = new Document("D:\\home\\lims\\报告管理需求 - 20190905.docx");// 原始word路径 String pdfFile = "D:\\home\\ff.pdf";
FileOutputStream fileOS = new FileOutputStream(pdfFile); doc.save(fileOS, SaveFormat.PDF); long now = System.currentTimeMillis();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
} catch (Exception e) {
e.printStackTrace();
}
}
}

调用结合我之前发的导出word的文章

/**
* 根据模板生成word
*
* @param path2
* 模板的路径
* @param params
* 需要替换的参数
* @param tableList
* 需要插入的参数,里面的list为每个表的数据
* @param fileName
* 生成word文件的文件名
* @param response
*/
public FileInfo getWord(String path2, Map<String, Object> params, List<List<String[]>> tableList, String fileName,
HttpServletResponse response) throws Exception {
File file = new File(path2);
InputStream is = new FileInputStream(file);
CustomXWPFDocument doc = new CustomXWPFDocument(is);
this.replaceInPara(doc, params); // 替换文本里面的变量
this.replaceInTable(doc, params, tableList); // 替换表格里面的变量 // OutputStream os = response.getOutputStream();
// response.setContentType("application/x-download");
// response.setHeader("Content-disposition", "attachment; filename=" + fileName);
// doc.write(os); SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
String formFile = ConfigFileUtil.getValueByKey("file.report.form.path");
File deskFile = new File(formFile, "report" + sdf.format(new Date()) + ".docx");
String path = deskFile.getPath();
// 写到目标文件
OutputStream output = new FileOutputStream(deskFile);
// document.write(output);
doc.write(output);
output.close();
String name = sdf.format(new Date());
String newPdfPath=formFile+"report" + name + ".pdf"; //新生成的pdf文件路径
Test.savedocx(path, newPdfPath); String newImgPdfPath = pdf2Image(newPdfPath); File p = new File(deskFile.getPath());
FileInfo fi = new FileInfo(); fi.setFileName("report" + "" + name + ".pdf");
fi.setUploadTime(new Date());
fi.setOwnerModel("ReportItem");
fi.setUseType("0");
fi.setModelContentId("");
fi.setFileType("pdf");
fi.setFilePath(newImgPdfPath); this.close(is); return fi; }

pdf转图片,再插入pdf,使PDF就算用专业软件也不能打开,但是我们领导觉得可能多页会出现问题,但是还没有好的解决办法,如果有了请评论告知,不胜感激

public static String pdf2Image(String PdfFilePath) throws Exception {
File file = new File(PdfFilePath);
PdfDocument template_writer_pdfdoc = new PdfDocument(new PdfReader(PdfFilePath));
Rectangle size=template_writer_pdfdoc.getFirstPage().getPageSize();
int pages = template_writer_pdfdoc.getNumberOfPages();
PDDocument pdDocument;
String pdfPath="";
try {
String imgPDFPath = file.getParent();
int dot = file.getName().lastIndexOf('.');
String imagePDFName = file.getName().substring(0, dot); // 获取图片文件名
pdDocument = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(pdDocument);
/* dpi越大转换后越清晰,相对转换速度越慢 */
StringBuffer imgFilePath = null;
List<String> pathList=new ArrayList<String>();
for (int i = 0; i < pages; i++) {
String imgFilePathPrefix = imgPDFPath + File.separator + imagePDFName;
imgFilePath = new StringBuffer();
imgFilePath.append(imgFilePathPrefix);
imgFilePath.append("_");
imgFilePath.append(String.valueOf(i + 1));
imgFilePath.append(".jpg");
File dstFile = new File(imgFilePath.toString());
BufferedImage image = renderer.renderImageWithDPI(i, 300);
ImageIO.write(image, "jpg", dstFile);
pathList.add(imgFilePath.toString());
} template_writer_pdfdoc.close();
File file2 = new File(imgPDFPath + File.separator + file.getName());
// 第一步:创建一个document对象。
com.itextpdf.text.Document document = new com.itextpdf.text.Document();
document.setMargins(0, 0, 0, 0);
// 第二步:
// 创建一个PdfWriter实例,
com.itextpdf.text.pdf.PdfWriter.getInstance(document, new FileOutputStream(file2));
// 第三步:打开文档。 document.open();
for(String s:pathList) {
com.itextpdf.text.Image img = com.itextpdf.text.Image.getInstance(s);
img.setAlignment(com.itextpdf.text.Image.ALIGN_CENTER);
img.scaleAbsoluteHeight(size.getHeight());
img.scaleAbsoluteWidth(size.getWidth());
// 根据图片大小设置页面,一定要先设置页面,再newPage(),否则无效
document.setPageSize(new com.itextpdf.text.Rectangle(size.getWidth(), size.getHeight()));
document.newPage();
document.add(img);
}
document.close();
pdfPath=imgPDFPath + File.separator + file.getName();
} catch (IOException e) {
e.printStackTrace();
}finally { }
return pdfPath;
}

Word转pdf,再转图片插入PDF的更多相关文章

  1. 把文档转化为PDF再用PS处理PDF

    最近工作中遇到类似下面这样的一个文档. 文档当前设置的是A4 横版打印,可以明显的看到打印的分界线,这样直接打印出来,是没有下面那行“bbbbbbbbbbbbbbbbbb”的,怎么办?可以通过 页面布 ...

  2. word 转pdf 再转图片--用在轻社群发文章

    #! /usr/bin/env python # -*- coding: utf-8 -*- import fitz import glob import os from win32com.clien ...

  3. 压缩图片或pdf

    压缩图片或pdf { /// <summary> /// 压缩图片或pdf大小的Level /// </summary> public enum ReduceSizeLevel ...

  4. Aspose.Pdf合并图片到PDF文件

    将图片和PDF文件合成为新的PDF文件,可以先将图片转换为PDF文件, 然后合成PDF即可, 将图片转换成PDF文件有如下方法: Aspose.Pdf.Document Aspose.Pdf.Gene ...

  5. Java 添加、提取PDF中的图片

    Spire.Cloud.SDK for Java提供了PdfImagesApi接口可用于添加图片到PDF文档addImage().提取PDF中的图片extractImages(),具体操作步骤和Jav ...

  6. 【使用Itext处理PDF文档(新建PDF文件、修改PDF文件、PDF中插入图片、将PDF文件转换为图片)】

    iText简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转 ...

  7. CAD转PDF再由pdf转jpg图片

    免费的PDF转JPG图片 https://www.gaitubao.com/pdf-to-jpg/

  8. word和.txt文件转html 及pdf文件, 使用poi jsoup itext心得

    word和.txt文件转html 及pdf文件, 使用poi jsoup  itext心得本人第一次写博客,有上面不足的或者需要改正的希望大家指出来,一起学习交流讨论.由于在项目中遇到了这一个问题,在 ...

  9. 批量将网页转换成图片或PDF文档技巧分享

    工作中我们有时要将一些批量的网页转换成图片或者PDF文档格式,尽管多数浏览器具有滚动截屏或者打印输出PDF文档功能.可是假设有几十上百张网页须要处理,那也是要人命的.所以我一直想找一款可以批量处理该工 ...

随机推荐

  1. 接口 ThreadMXBean 一个很好用的线程管理接口类 可以参考 jdk 帮助文档

    概述  软件包   类  使用  树  已过时  索引  帮助  JavaTM Platform Standard Ed. 6  上一个类   下一个类 框架    无框架    所有类 摘要: 嵌套 ...

  2. mysql 不能对同一个表进行 update(delete) 和 select 联合操作

    eq:     update a set  a.x = 1  where a.y  in (select a.x from  a); 上边语法是错误的,在对aupdate 时不能再条件中对同一个a表进 ...

  3. java接口工厂模式理解

    作为实际java开发经验还不到一年的我,第一次写博客,诚惶诚恐,怕把自己的谬误公之于众,误人子弟,不过转念一想,若是能有同行加以指点评判,将他们的真知灼见描述出来,那这篇文章就算抛转引玉了. 最近在阅 ...

  4. 设计模式中巧记I/O

    一.I/O 1. I/O操作中的设计模式 概要 以设计模式角度,自顶向下理解I/O源码结构 理解字节与字符的关系 1.1 装饰者模式(输入流为例) 背景:通过继承扩展对象耦合度高,使用装饰者扩展可以在 ...

  5. JAVA—线程(Thread)

    1.线程的状态有哪些 我记得在操作系统原理的书上有一张具体的图,暂时找不到书... new:新建状态,被创建出来后未启动时的线程状态. runnable:就绪状态,表示可以运行. blocked:阻塞 ...

  6. B2. Character Swap (Hard Version)

    链接: http://codeforces.com/contest/1243/problem/B2 题目大意: 两个字符串,判断能否通过交换为从而使得这两个字符串完全一致,如不可以的话,直接输出NO, ...

  7. 自己写的一个HTML的小网页

    上次在上直播课的时候,教员提到了html这种标记语言.自己就在W3school上面学了一点点关于html的一些皮毛,自己动手写了一个小网页,同时自己对CTF这一块比较感兴趣,但是自己还是一个干干净净的 ...

  8. Python实现按键精灵(一)-键鼠操作

    需要安装 pywin32库 pip install pywin32 import win32api import time #鼠标移动 def mouse_move(x,y): win32api.Se ...

  9. python 异步Web框架sanic

    我们继续学习Python异步编程,这里将介绍异步Web框架sanic,为什么不是tornado?从框架的易用性来说,Flask要远远比tornado简单,可惜flask不支持异步,而sanic就是类似 ...

  10. Python selenium Chrome正在受到自动软件的控制 disable-infobars无效 的解决方法

    问题解决 前两天更新了google浏览器版本,今天运行以前的脚本,发现options一个参数的配置不生效了. 运行了几次都发现该参数没有生效,也检查了自己的代码参数,没有写错,于是就有了这一波“网中寻 ...