文档转图片整理归纳
如下文档都可转换:
- excel(.xls、.xlsx)
- word(.doc、.docx)
- ppt
适用于文档预览 图片生成等需求
代码及maven jar包下载地址:下载地址
所有转换都整理归纳到了一块,无水印。
有问题联系QQ:2331202797
如下是生成部分的代码:
package com.lxf.utils;
import com.aspose.cells.*;
import com.aspose.words.Document;
import com.aspose.words.ImageSaveOptions;
import com.aspose.words.SaveFormat;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.xslf.usermodel.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
/**
* word、excel、ppt、pdf 转图片
* @QQ
* @Author lxf
* @Date 2021/3/11
**/
public class AsposeUtils {
private final static String XLS = ".xls";
private final static String XLSX = ".xlsx";
private final static String DOC = ".doc";
private final static String DOCX = ".docx";
private final static String PDF = ".pdf";
private final static String PPT = ".ppt";
private final static String PPTX = ".pptx";
//生成图片时临时存放缓存地址
private final static String TEMP = System.getProperty("user.dir") + File.separator + "media" + File.separator + "temp" + File.separator;
public static List<BufferedImage> toImages(String fileType,InputStream inputStream){
List<BufferedImage> list = new ArrayList<>();
switch (fileType){
case XLS:
list = excelToImages(inputStream);
break;
case XLSX:
list = excelToImages(inputStream);
break;
case DOC:
list = wordToImages(inputStream);
break;
case DOCX:
list = wordToImages(inputStream);
break;
case PPT:
list = pptToImages(inputStream);
break;
case PPTX:
list = pptxToImages(inputStream);
break;
case PDF:
list = pdfToImages(inputStream);
break;
}
return list;
}
/**
* excel所有sheet转成多个图片
* @param excelInputStream excel文件流
* @return 多个图片
*/
public static List<BufferedImage> excelToImages(InputStream excelInputStream){
List<BufferedImage> images = new ArrayList<>();
try {
Workbook workbook = new Workbook(excelInputStream);
excelInputStream.close();
WorksheetCollection worksheets = workbook.getWorksheets();
Iterator<Worksheet> iterator = worksheets.iterator();
while(iterator.hasNext()) {
Worksheet worksheet = iterator.next();
worksheet.getPageSetup().setLeftMargin(0);
worksheet.getPageSetup().setRightMargin(0);
worksheet.getPageSetup().setTopMargin(0);
worksheet.getPageSetup().setBottomMargin(0);
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.setImageFormat(ImageFormat.getJpeg());
imgOptions.setOnePagePerSheet(true);
imgOptions.setPrintingPage(PrintingPageType.IGNORE_BLANK);
SheetRender sr = new SheetRender(worksheet, imgOptions);
for (int i = 0; i < sr.getPageCount(); i++) {
File file = new File(TEMP + getUUID() + ".jpg");
FileOutputStream fileOutputStream = new FileOutputStream(file);
sr.toImage(i,fileOutputStream);
images.add(ImageIO.read(file));
fileOutputStream.close();
file.delete();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return images;
}
/**
* word转成多个图片
* @param wordInputStream excel文件流
* @return 多个图片
*/
public static List<BufferedImage> wordToImages(InputStream wordInputStream){
List<BufferedImage> images = new ArrayList<>();
try {
Document document = new Document(wordInputStream);
wordInputStream.close();
ImageSaveOptions iso = new ImageSaveOptions(SaveFormat.JPEG);
iso.setResolution(128);
iso.setPrettyFormat(true);
iso.setUseAntiAliasing(true);
for (int i = 0; i < document.getPageCount(); i++) {
iso.setPageIndex(i);
File file = new File(TEMP + getUUID() + ".jpg");
FileOutputStream fileOutputStream = new FileOutputStream(file);
document.save(fileOutputStream,iso);
images.add(ImageIO.read(file));
fileOutputStream.close();
file.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
return images;
}
/**
* pdf转成多个图片
* @param pdfInputStream
* @return
*/
public static List<BufferedImage> pdfToImages(InputStream pdfInputStream) {
List<BufferedImage> list = new ArrayList<>();
try {
PDDocument doc = PDDocument.load(pdfInputStream);
pdfInputStream.close();
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); // 第二个参数越大生成图片分辨率越高,转换时间也就越长
list.add(image);
}
doc.close(); // 关闭文件,不然该pdf文件会一直被占用。
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
/**
* pptx 转成多个图片
* @param pptxInputStream
* @return
*/
public static List<BufferedImage> pptxToImages(InputStream pptxInputStream) {
List<BufferedImage> list = new ArrayList<>();
try {
XMLSlideShow ppt = new XMLSlideShow(pptxInputStream);
pptxInputStream.close();
Dimension pgsize = ppt.getPageSize();
for (XSLFSlide slide : ppt.getSlides()) {
//设置字体
for (XSLFShape shape : slide.getShapes()){
if (shape instanceof XSLFTextShape){
XSLFTextShape sh = (XSLFTextShape) shape;
List<XSLFTextParagraph> textParagraphs = sh.getTextParagraphs();
for (XSLFTextParagraph xslfTextParagraph : textParagraphs) {
List<XSLFTextRun> textRuns = xslfTextParagraph.getTextRuns();
for (XSLFTextRun xslfTextRun : textRuns) {
xslfTextRun.setFontFamily("宋体");
}
}
}
}
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
// clear the drawing area
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
// render
slide.draw(graphics);
list.add(img);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
/**
* ppt 转成多个图片
* @param pptInputStream
* @return
*/
public static List<BufferedImage> pptToImages(InputStream pptInputStream) {
List<BufferedImage> list = new ArrayList<>();
try {
HSLFSlideShow ppt = new HSLFSlideShow(pptInputStream);
pptInputStream.close();
Dimension pgsize = ppt.getPageSize();
for (HSLFSlide slide : ppt.getSlides()) {
//设置字体
for (HSLFShape shape : slide.getShapes()) {
if (shape instanceof HSLFTextShape) {
HSLFTextShape sh = (HSLFTextShape) shape;
List<HSLFTextParagraph> textParagraphs = sh.getTextParagraphs();
for (HSLFTextParagraph hslfTextParagraph : textParagraphs) {
List<HSLFTextRun> textRuns = hslfTextParagraph.getTextRuns();
for (HSLFTextRun hslfTextRun : textRuns) {
hslfTextRun.setFontFamily("宋体");
hslfTextRun.setFontIndex(1);
}
}
}
}
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
// clear the drawing area
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
// render
slide.draw(graphics);
list.add(img);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
private static String getUUID(){
return UUID.randomUUID().toString().replaceAll("-","");
}
public static void main(String[] args) throws IOException {
String filePath = System.getProperty("user.dir") + File.separator + "media" + File.separator;
//excel转换 需要初始化license,不加license或有水印。springboot或web服务 启动时初始化一次即可
License aposeLic = new License();
aposeLic.setLicense(new FileInputStream(System.getProperty("user.dir") + File.separator + "AsposeLicense" + File.separator + "license.xml"));
List<BufferedImage> images = toImages(".xlsx", new FileInputStream(filePath + "test.xlsx"));
for (int i = 0; i < images.size(); i++) {
ImageIO.write(images.get(i),"jpeg",new FileOutputStream(filePath + "test"+(i+1)+".jpeg"));
}
}
}