Word,Pdf转图片
业务需要,百度了好久,Pdf转图片还好转,Word就比较麻烦,要么需要依赖office或者其他软件,要么转出来乱码,最后也只能使用非开源的Aspose,也会出现乱码问题,注意要放字体在服务器
PDF转图片
PDF转图片
import cn.hutool.core.util.ObjectUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author huboxin
*/
public class PdfUtils {
private static Logger log = LogManager.getLogger(PdfUtils.class);
/**
* 将PDF按页数每页转换成一个jpg图片
*
* @param filePath
* @return
*/
@SuppressWarnings("all")
public static List<String> pdfToImagePath(String filePath) {
List<String> list = new ArrayList<>();
String fileDirectory = filePath.substring(0, filePath.lastIndexOf("."));//获取去除后缀的文件路径
String imagePath;
File file = new File(filePath);
try {
File f = new File(fileDirectory);
if (!f.exists()) {
f.mkdir();
}
PDDocument doc = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
for (int i = 0; i < pageCount; i++) {
// 方式1,第二个参数是设置缩放比(即像素)
// BufferedImage image = renderer.renderImageWithDPI(i, 296);
// 方式2,第二个参数是设置缩放比(即像素)
BufferedImage image = renderer.renderImage(i, 1.25f); //第二个参数越大生成图片分辨率越高,转换时间也就越长
imagePath = fileDirectory + "/" + i + ".jpg";
ImageIO.write(image, "PNG", new File(imagePath));
list.add(imagePath);
}
doc.close(); //关闭文件,不然该pdf文件会一直被占用。
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
/**
* @param pdfFile
* @param outpath
* @Description pdf转成一张图片
* @created 2019年4月19日 下午1:54:13
*/
@SuppressWarnings("all")
private static void pdf2multiImage(String pdfFile, String outpath) {
try {
InputStream is = new FileInputStream(pdfFile);
PDDocument pdf = PDDocument.load(is);
int actSize = pdf.getNumberOfPages();
List<BufferedImage> piclist = new ArrayList<BufferedImage>();
for (int i = 0; i < actSize; i++) {
BufferedImage image = new PDFRenderer(pdf).renderImageWithDPI(i, 130, ImageType.RGB);
piclist.add(image);
}
yPic(piclist, outpath);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param pdfFile
* @param outpath
* @Description pdf转成流
* @created 2019年4月19日 下午1:54:13
*/
@SuppressWarnings("all")
public static InputStream pdf2multiImage(MultipartFile pdfFile) {
if(ObjectUtil.isEmpty(pdfFile)){
return null;
}
try {
InputStream is = pdfFile.getInputStream();
PDDocument pdf = PDDocument.load(is);
int actSize = pdf.getNumberOfPages();
List<BufferedImage> piclist = new ArrayList<BufferedImage>();
for (int i = 0; i < actSize; i++) {
BufferedImage image = new PDFRenderer(pdf).renderImageWithDPI(i, 130, ImageType.RGB);
piclist.add(image);
}
is.close();
return yPic(piclist, null);
} catch (IOException e) {
e.printStackTrace();
log.error("PDF转化图片失败");
}
return null;
}
/**
* 将宽度相同的图片,竖向追加在一起 ##注意:宽度必须相同
*
* @param piclist 文件流数组
* @param outPath 输出路径
*/
@SuppressWarnings("all")
public static InputStream yPic(List<BufferedImage> piclist, String outPath) {// 纵向处理图片
ByteArrayInputStream byteArrayInputStream = null;
if (piclist == null || piclist.size() <= 0) {
System.out.println("图片数组为空!");
return byteArrayInputStream;
}
try {
int height = 0, // 总高度
width = 0, // 总宽度
_height = 0, // 临时的高度 , 或保存偏移高度
__height = 0, // 临时的高度,主要保存每个高度
picNum = piclist.size();// 图片的数量
int[] heightArray = new int[picNum]; // 保存每个文件的高度
BufferedImage buffer = null; // 保存图片流
List<int[]> imgRGB = new ArrayList<int[]>(); // 保存所有的图片的RGB
int[] _imgRGB; // 保存一张图片中的RGB数据
for (int i = 0; i < picNum; i++) {
buffer = piclist.get(i);
heightArray[i] = _height = buffer.getHeight();// 图片高度
if (i == 0) {
width = buffer.getWidth();// 图片宽度
}
height += _height; // 获取总高度
_imgRGB = new int[width * _height];// 从图片中读取RGB
_imgRGB = buffer.getRGB(0, 0, width, _height, _imgRGB, 0, width);
imgRGB.add(_imgRGB);
}
_height = 0; // 设置偏移高度为0
// 生成新图片
BufferedImage imageResult = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < picNum; i++) {
__height = heightArray[i];
if (i != 0) {
_height += __height; // 计算偏移高度
}
imageResult.setRGB(0, _height, width, __height, imgRGB.get(i), 0, width); // 写入流中
}
// File outFile = new File(outPath);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageIO.write(imageResult, "jpg", bs);
byteArrayInputStream = new ByteArrayInputStream(bs.toByteArray());
return byteArrayInputStream;
} catch (Exception e) {
e.printStackTrace();
}
return byteArrayInputStream;
}
public static void main(String[] args) {
PdfUtils.pdf2multiImage("D:/123.pdf", "D:/123.jpg");
}
}
Pom依赖
Pom依赖
<!--pdf转图片-->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.4</version>
</dependency>
Word转图片
需要引入jar包,非开源jar,Aspose,Hutool的依赖就不用放了
Aspose链接自取:https://pan.baidu.com/s/1ISGN2tlobTHH2KzuMvnszg
提取码:n6xk
Pom依赖
<!--word转图片-->
<dependency>
<groupId>aspose.slides</groupId>
<artifactId>slides</artifactId>
<version>19.3</version>
<scope>system</scope>
<systemPath>${basedir}/lib/words.jar</systemPath>
</dependency>
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.amdox.servicelanclazz.dto.Msg;
import com.aspose.words.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class AsposeUtils {
private static Logger log = LogManager.getLogger(AsposeUtils.class);
/**
* word文档转图片png,jpge
* */
public String wordToPicture(InputStream inputStream,String tofileStyle,File toFile){
int saveFormat = SaveFormat.PNG;
if("jpeg".equalsIgnoreCase(tofileStyle)){
saveFormat = SaveFormat.JPEG;
}else if("svg".equalsIgnoreCase(tofileStyle)){
saveFormat = SaveFormat.SVG;
}
try {
List<BufferedImage> list = AsposeLocal.wordToImg(inputStream,saveFormat);
BufferedImage mergeImage = AsposeLocal.mergeImage(false, list);
ImageIO.write(mergeImage, tofileStyle, toFile); //将其保存在toFile下
return toFile.getAbsolutePath();
}catch (Exception e){
System.out.println("word转换图片失败");
}
return JSON.toJSONString(new Msg<>().setCode("word转换图片失败").fail());
}
/**
* word文档转图片png,jpge
* */
public static InputStream wordToJpg(InputStream inputStream){
if(ObjectUtil.isEmpty(inputStream)){
return null;
}
ByteArrayInputStream byteArrayInputStream = null;
try {
List<BufferedImage> list = AsposeLocal.wordToImg(inputStream,SaveFormat.PNG);
BufferedImage mergeImage = AsposeLocal.mergeImage(false, list);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageIO.write(mergeImage, "jpg", bs);
byteArrayInputStream = new ByteArrayInputStream(bs.toByteArray());
return byteArrayInputStream;
}catch (Exception e){
log.error("word转换图片失败");
}
return byteArrayInputStream;
}
public static class AsposeLocal{
/**
* @Description: 验证aspose.word组件是否授权:无授权的文件有水印标记
*/
public static boolean isWordLicense() {
boolean result = false;
try {
String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";
ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes());
//InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("license.xml");
com.aspose.words.License license = new com.aspose.words.License();
license.setLicense(inputStream);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* @Description: word和txt文件转换图片
*/
public static List<BufferedImage> wordToImg(InputStream inputStream, int pageNum,int saveFormat) throws Exception {
try {
FontSettings.setFontsFolders(new String[]{"fonts/","C:/Windows/Fonts"}, true);
Document doc = new Document(inputStream);
ImageSaveOptions options = new ImageSaveOptions(saveFormat);
options.setPrettyFormat(true);
options.setUseAntiAliasing(true);
options.setUseHighQualityRendering(true);
int pageCount = doc.getPageCount();
if (pageCount > pageNum) {//生成前pageCount张
pageCount = pageNum;
}
List<BufferedImage> imageList = new ArrayList<BufferedImage>();
for (int i = 0; i < pageCount; i++) {
OutputStream output = new ByteArrayOutputStream();
options.setPageIndex(i);
doc.save(output, options);
ImageInputStream imageInputStream = ImageIO.createImageInputStream(parse(output));
imageList.add(ImageIO.read(imageInputStream));
}
return imageList;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/**
* @Description: word和txt文件转换图片
*/
public static List<BufferedImage> wordToImg(InputStream inputStream,int saveFormat) throws Exception {
try {
FontSettings.setFontsFolders(new String[]{"fonts/","C:/Windows/Fonts"}, true);
Document doc = new Document(inputStream);
ImageSaveOptions options = new ImageSaveOptions(saveFormat);
options.setPrettyFormat(true);
options.setUseAntiAliasing(true);
options.setUseHighQualityRendering(true);
int pageCount = doc.getPageCount();
List<BufferedImage> imageList = new ArrayList<BufferedImage>();
for (int i = 0; i < pageCount; i++) {
OutputStream output = new ByteArrayOutputStream();
options.setPageIndex(i);
ImageInputStream imageInputStream = null;
try {
doc.save(output, options);
imageInputStream = ImageIO.createImageInputStream(parse(output));
} catch (Exception e) {
log.warn("文件页码超出范围,停止扫描");
break;
}
imageList.add(ImageIO.read(imageInputStream));
}
return imageList;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
//outputStream转inputStream
public static ByteArrayInputStream parse(OutputStream out) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos = (ByteArrayOutputStream) out;
ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
return swapStream;
}
/**
* 合并任数量的图片成一张图片
*
* @param isHorizontal true代表水平合并,fasle代表垂直合并
* @param imgs 待合并的图片数组
* @return
* @throws IOException
*/
public static BufferedImage mergeImage(boolean isHorizontal, List<BufferedImage> imgs) throws IOException {
// 生成新图片
BufferedImage destImage = null;
// 计算新图片的长和高
int allw = 0, allh = 0, allwMax = 0, allhMax = 0;
// 获取总长、总宽、最长、最宽
for (int i = 0; i < imgs.size(); i++) {
BufferedImage img = imgs.get(i);
allw += img.getWidth();
if (imgs.size() != i + 1) {
allh += img.getHeight() + 5;
} else {
allh += img.getHeight();
}
if (img.getWidth() > allwMax) {
allwMax = img.getWidth();
}
if (img.getHeight() > allhMax) {
allhMax = img.getHeight();
}
}
// 创建新图片
if (isHorizontal) {
destImage = new BufferedImage(allw, allhMax, BufferedImage.TYPE_INT_RGB);
} else {
destImage = new BufferedImage(allwMax, allh, BufferedImage.TYPE_INT_RGB);
}
Graphics2D g2 = (Graphics2D) destImage.getGraphics();
g2.setBackground(Color.LIGHT_GRAY);
g2.clearRect(0, 0, allw, allh);
g2.setPaint(Color.RED);
// 合并所有子图片到新图片
int wx = 0, wy = 0;
for (int i = 0; i < imgs.size(); i++) {
BufferedImage img = imgs.get(i);
int w1 = img.getWidth();
int h1 = img.getHeight();
// 从图片中读取RGB
int[] ImageArrayOne = new int[w1 * h1];
ImageArrayOne = img.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中
if (isHorizontal) { // 水平方向合并
destImage.setRGB(wx, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
} else { // 垂直方向合并
destImage.setRGB(0, wy, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
}
wx += w1;
wy += h1 + 5;
}
return destImage;
}
public static void word2pdf(String docPath,String savePath){
try {
String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";
ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
License license = new License();
license.setLicense(is);
com.aspose.words.Document document = new com.aspose.words.Document(docPath);
document.save(new FileOutputStream(new File(savePath)),SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
ClassPathResource fonts = new ClassPathResource("fonts");
String path = fonts.getPath();
System.out.println(path);
}
/*public static void main(String[] args) throws Exception{
try {
//doc路径
// Document document = new Document("C:\\Users\\15438\\Desktop\\测试测试测试.doc");
//pdf路径
File outputFile = new File("g:\\建模定稿56.jpg");
//操作文档保存
//document.save(outputFile.getAbsolutePath(), SaveFormat.PNG);
InputStream inStream = new FileInputStream("C:\\Users\\15438\\Desktop\\测试测试测试.doc");
List<BufferedImage> wordToImg = wordToImg(inStream,5);//
BufferedImage mergeImage = AsposeLocal.mergeImage(false, wordToImg);
ImageIO.write(mergeImage, "jpg", outputFile); //将其保存在C:/imageSort/targetPIC/下
} catch (Exception e) {
e.printStackTrace();
}
}*/