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

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

2023-06-28 08:39| 来源: 网络整理| 查看: 265

文章目录 一、将文件转换成图片,并生成到本地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 二、利用多线程提升文件写入本地的效率三、将文件转换成图片流1、将word文件转成图片流(1)使用aspose 2、将txt文件转成图片流(1)使用aspose 3、将pdf转成图片流(1)使用aspose(2)使用pdfbox(3)使用spire 4、将ppt文件转图片流(1)使用aspose(2)使用pdfbox 总结解决多图片展示问题的解决方案: 如果不想网页上的文章被复制(没错,说的就是某点),如果想实现文档不需要下载下来就能在线预览查看(常见于文档付费下载网站、邮箱附件预览),该怎么做?常见的做法就是将他们转化成图片。以下代码基于 aspose-words(用于txt、word转图片),pdfbox(用于pdf转图片),封装成一个工具类来实现txt、word、pdf等文件转图片的需求。

首先在项目的pom文件里添加如下依赖

e-iceblue spire.office.free 5.3.1 com.aspose aspose-words 23.1 com.aspose aspose-pdf 23.1 com.aspose aspose-cells 23.1 com.aspose aspose-slides 23.1 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 一、将文件转换成图片,并生成到本地 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、将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()); } 总结

将文件转成图片实现预览的这种方式的优点是:

1、图片在线预览控件比较多,也比较成熟,前端起来比较方便

2、文档转成图片后能有效减少文档内容被复制的情况

3、浏览器也天然支持

这种方式的缺点是:

1、文档往往都不只一页,所有同城的做法将文档的每一页都生成一张图片,所以前后端都需要考虑处理多张图片的问题

2、如果图片都以base64的格式返回给前端,会造成返回体过大的问题,如果返回有加日志还会存在日志体较长,增加日志服务器的问题。

3、因为base64的格式直接返回返回体过长,好一点的做法现将图片上传到图片服务器,只返回图片的url,这样解决了图片返回体过长的问题,但要先将多张图片先上传到图片服务器,这样会不可避免的拖慢接口的返回速度,尤其是在文档页数较多的时候,同时也会增加图片服务器的压力。

解决多图片展示问题的解决方案:

应该如何解决多图片展示问题呢,其实很简单,可以参考开源组件kkfileview解决多图片展示问题的(都参考了为什么不直接拿来用,滑稽表情)的做法,即将生成的多张图片全都放到一个html页面里,用html保持样式并实现多张图片展示,再将html返回。 ​ kkfileview展示效果如下: 请添加图片描述

下图是kkfileview返回的html代码,从html代码我们可以看到kkfileview其实是将文件(txt文件除外)每页的内容都转成了图片,然后将这些图片都嵌入到一个html里,再返回给用户一个html页面。 请添加图片描述



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

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