文档在线预览(一)通过将txt、word、pdf、ppt文件转成图片实现在线预览功能 您所在的位置:网站首页 office将pdf转成ppt 文档在线预览(一)通过将txt、word、pdf、ppt文件转成图片实现在线预览功能

文档在线预览(一)通过将txt、word、pdf、ppt文件转成图片实现在线预览功能

2024-07-09 14:01| 来源: 网络整理| 查看: 265

文章目录 一、前言1、aspose2 、poi + pdfbox3 spire 二、将文件转换成图片,并生成到本地1、将word文件转成图片(1)使用aspose(2)使用pdfbox(3)使用spire 2、将txt文件转成图片(同word文件转成图片)(1)使用aspose 3、将pdf文件转图片(1)使用aspose(2)使用pdfbox(3)使用spire 4、将ppt文件转图片(1)使用aspose(2)使用pdfbox(3)使用spire 三、利用多线程提升文件写入本地的效率四、将文件转换成图片流1、将word文件转成图片流(1)使用aspose(2)使用pdfbox(3)使用spire 2、将txt文件转成图片流(1)使用aspose 3、将pdf转成图片流(1)使用aspose(2)使用pdfbox(3)使用spire 4、将ppt文件转图片流(1)使用aspose(2)使用pdfbox(3)使用spire 总结

一、前言

​ 如果不想网页上的文章被复制(没错,说的就是某点),如果想实现文档不需要下载下来就能在线预览查看(常见于文档付费下载网站、邮箱附件预览),该怎么做?常见的做法就是将他们转化成图片。

以下代码分别提供基于aspose、pdfbox、spire来实现来实现txt、word、pdf、ppt、word等文件转图片的需求。

1、aspose

Aspose 是一家致力于.Net ,Java,SharePoint,JasperReports和SSRS组件的提供商,数十个国家的数千机构都有用过aspose组件,创建、编辑、转换或渲染 Office、OpenOffice、PDF、图像、ZIP、CAD、XPS、EPS、PSD 和更多文件格式。注意aspose是商用组件,未经授权导出文件里面都是是水印(尊重版权,远离破解版)。

需要在项目的pom文件里添加如下依赖

com.aspose aspose-words 23.1 com.aspose aspose-pdf 23.1 com.aspose aspose-cells 23.1 com.aspose aspose-slides 23.1 2 、poi + pdfbox

因为aspose和spire虽然好用,但是都是是商用组件,所以这里也提供使用开源库操作的方式的方式。

POI是Apache软件基金会用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程序对Microsoft Office格式档案读和写的功能。

Apache PDFBox是一个开源Java库,支持PDF文档的开发和转换。 使用此库,您可以开发用于创建,转换和操作PDF文档的Java程序。

需要在项目的pom文件里添加如下依赖

org.apache.pdfbox pdfbox 2.0.4 org.apache.poi poi 5.2.0 org.apache.poi poi-ooxml 5.2.0 org.apache.poi poi-scratchpad 5.2.0 org.apache.poi poi-excelant 5.2.0 3 spire

spire一款专业的Office编程组件,涵盖了对Word、Excel、PPT、PDF等文件的读写、编辑、查看功能。spire提供免费版本,但是存在只能导出前3页以及只能导出前500行的限制,只要达到其一就会触发限制。需要超出前3页以及只能导出前500行的限制的这需要购买付费版(尊重版权,远离破解版)。这里使用免费版进行演示。

spire在添加pom之前还得先添加maven仓库来源

com.e-iceblue e-iceblue https://repo.e-iceblue.cn/repository/maven-public/

接着在项目的pom文件里添加如下依赖

免费版:

e-iceblue spire.office.free 5.3.1

付费版版:

e-iceblue spire.office 5.3.1 二、将文件转换成图片,并生成到本地 1、将word文件转成图片 (1)使用aspose public static void wordToImage(String wordPath, String imagePath) throws Exception { Document doc = new Document(wordPath); File file = new File(wordPath); String filename = file.getName(); String pathPre = imagePath + File.separator + filename.substring(0, filename.lastIndexOf(".")); for (int i = 0; i imagePath = FileUtil.getNewFileFullPath(wordPath, imagePath, "png"); try(FileInputStream fileInputStream = new FileInputStream(wordPath); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()){ XWPFDocument document = new XWPFDocument(fileInputStream); PdfOptions pdfOptions = PdfOptions.create(); PdfConverter.getInstance().convert(document, byteArrayOutputStream, pdfOptions); document.close(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); PDDocument doc = PDDocument.load(byteArrayInputStream); PDFRenderer renderer = new PDFRenderer(doc); for (int i = 0; i File file = new File(wordPath); String filename = file.getName(); String pathPre = imagePath + File.separator + filename.substring(0, filename.lastIndexOf(".")); //加载Word文档 Document document = new Document(); document.loadFromFile(wordPath); //将Word文档转换为图片 BufferedImage[] images = document.saveToImages(0, document.getPageCount()-1, ImageType.Bitmap); //保存图片 for (int i = 0; i wordToImage(txtPath, imagePath); }

验证:

public static void main(String[] args) throws Exception { FileConvertUtil.wordToImage("D:\\书籍\\电子书\\其它\\《山海经》异兽图.doc", "D:\\test\\word"); }

验证结果: 请添加图片描述

3、将pdf文件转图片 (1)使用aspose public static void pdfToImage(String pdfPath, String imagePath) throws Exception { File file = new File(pdfPath); String filename = file.getName(); String pathPre = imagePath + File.separator + filename.substring(0, filename.lastIndexOf(".")); PDDocument doc = PDDocument.load(file); PDFRenderer renderer = new PDFRenderer(doc); for (int i = 0; i FileConvertUtil.pdfToImage("D:\\书籍\\电子书\\其它\\自然哲学的数学原理.pdf", "D:\\test\\pdf"); }

验证结果: 请添加图片描述

(2)使用pdfbox public void pdfToImage(String pdfPath, String imagePath) throws Exception { String pathPre = FileUtil.getNewMultiFileFullPathPre(pdfPath, imagePath); PDDocument doc = PDDocument.load(new File(pdfPath)); PDFRenderer renderer = new PDFRenderer(doc); for (int i = 0; i PdfDocument pdf = new PdfDocument(); pdf.loadFromFile(pdfPath); File file = new File(pdfPath); String filename = file.getName(); String pathPre = imagePath + File.separator + filename.substring(0, filename.lastIndexOf(".")); for (int i = 0; i File file = new File(pptPath); String filename = file.getName(); String pathPre = imagePath + File.separator + filename.substring(0, filename.lastIndexOf(".")); Presentation presentation = new Presentation(pptPath); for (int i = 0; i File file = new File(pptPath); String filename = file.getName().substring(0, file.getName().lastIndexOf(".")); List images = pptToBufferedImages(pptPath); String dicPath = imagePath + File.separator + filename; File dic = new File(dicPath); if (!dic.exists()) { dic.mkdir(); } for (int i = 0; i long old = System.currentTimeMillis(); File file = new File(pdfPath); PDDocument doc = PDDocument.load(file); PDFRenderer renderer = new PDFRenderer(doc); int pageCount = doc.getNumberOfPages(); int numCores = Runtime.getRuntime().availableProcessors(); ExecutorService executorService = Executors.newFixedThreadPool(numCores); for (int i = 0; i try { BufferedImage image = renderer.renderImageWithDPI(finalI, 144); // Windows native DPI String filename = file.getName(); filename = filename.substring(0, filename.lastIndexOf(".")); String pathname = imagePath + File.separator + filename + (finalI + 1) + ".png"; ImageIO.write(image, "PNG", new File(pathname)); } catch (Exception ex) { ex.printStackTrace(); } }); } executorService.shutdown(); executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); doc.close(); long now = System.currentTimeMillis(); System.out.println("pdfToImage 多线程 转换完成..用时:" + (now - old) + "ms"); }

多线程执行导出 自然哲学的数学原理.pdf 耗时如下: 请添加图片描述

从上图可以看到本次执行只花了24045ms,只花了原先差不多六分之一的时间,极大地提升了执行效率。除了pdf,word、txt转图片也可以做这样的多线程改造:

//将word转成图片(多线程) public static void wordToImageAsync(String wordPath, String imagePath) throws Exception { Document doc = new Document(wordPath); File file = new File(wordPath); String filename = file.getName(); String pathPre = imagePath + File.separator + filename.substring(0, filename.lastIndexOf(".")); int numCores = Runtime.getRuntime().availableProcessors(); ExecutorService executorService = Executors.newFixedThreadPool(numCores); for (int i = 0; i try { Document extractedPage = doc.extractPages(finalI, 1); String path = pathPre + (finalI + 1) + ".png"; extractedPage.save(path, SaveFormat.PNG); } catch (Exception ex) { ex.printStackTrace(); } }); } } //将txt转成图片(多线程) public static void txtToImageAsync(String txtPath, String imagePath) throws Exception { wordToImageAsync(txtPath, imagePath); } 四、将文件转换成图片流

有的时候我们转成图片后并不需要在本地生成图片,而是需要将图片返回或者上传到图片服务器,这时候就需要将转换后的图片转成流返回以方便进行传输,代码示例如下:

1、将word文件转成图片流 (1)使用aspose public static List wordToImageStream(String wordPath) throws Exception { Document doc = new Document(wordPath); List list = new ArrayList(); for (int i = 0; i Document extractedPage = doc.extractPages(i, 1); extractedPage.save(outputStream, SaveFormat.*PNG*); list.add(outputStream.toByteArray()); } } return list; } (2)使用pdfbox public List wordToImageStream(String wordPath) throws Exception { List images = new ArrayList(); try(FileInputStream fileInputStream = new FileInputStream(wordPath); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()){ XWPFDocument document = new XWPFDocument(fileInputStream); PdfOptions pdfOptions = PdfOptions.create(); PdfConverter.getInstance().convert(document, byteArrayOutputStream, pdfOptions); document.close(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); PDDocument doc = PDDocument.load(byteArrayInputStream); PDFRenderer renderer = new PDFRenderer(doc); for (int i = 0; i try { return FileUtil.imageToByte(image); } catch (IOException e) { throw new RuntimeException(e); } }).collect(Collectors.toList()); } (3)使用spire public List wordToImageStream(String wordPath) throws Exception { Document document = new Document(); document.loadFromFile(wordPath); BufferedImage[] bufferedImages = document.saveToImages(ImageType.Bitmap); return FileUtil.toByteArrays(bufferedImages); } 2、将txt文件转成图片流 (1)使用aspose public static List txtToImageStream(String txtPath) throws Exception { return *wordToImagetream*(txtPath); } 3、将pdf转成图片流 (1)使用aspose public static List pdfToImageStream(String pdfPath) throws Exception { File file = new File(pdfPath); PDDocument doc = PDDocument.*load*(file); PDFRenderer renderer = new PDFRenderer(doc); List list = new ArrayList(); for (int i = 0; i BufferedImage image = renderer.renderImageWithDPI(i, 144); // Windows native DPI ImageIO.*write*(image, "PNG", outputStream); list.add(outputStream.toByteArray()); } } doc.close(); return list; } (2)使用pdfbox public List pdfToImageStream(String pdfPath) throws Exception { File file = new File(pdfPath); PDDocument doc = PDDocument.load(file); PDFRenderer renderer = new PDFRenderer(doc); List list = new ArrayList(); for (int i = 0; i BufferedImage image = renderer.renderImageWithDPI(i, 144); // Windows native DPI ImageIO.write(image, "PNG", outputStream); list.add(outputStream.toByteArray()); } } doc.close(); return list; } (3)使用spire public List pdfToImageStream(String pdfPath) throws Exception { PdfDocument pdf = new PdfDocument(); pdf.loadFromFile(pdfPath); File file = new File(pdfPath); String filename = file.getName(); List list = new ArrayList(); for (int i = 0; i List list = new ArrayList(); Presentation presentation = new Presentation(pptPath); for (int i = 0; i List images = pptToBufferedImages(pptPath); if(CollectionUtils.isEmpty(images)){ return null; } return images.stream().map(image-> { try { return FileUtil.imageToByte(image); } catch (IOException e) { throw new RuntimeException(e); } }).collect(Collectors.toList()); } (3)使用spire public List pptToImageStream(String pptPath) throws Exception { List list = new ArrayList(); Presentation presentation = new Presentation(); presentation.loadFromFile(pptPath); for (int i = 0; i


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有