windows 如何预览 Java 应用程序中的任何文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2950786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:33:45  来源:igfitidea点击:

How to preview any files inside a Java application?

javawindowswinapithumbnailspreview

提问by Studer

I'm using a JTree to browse the content of a folder and I want that when a user click on a file, the software shows a preview of it (a screenshot of its first page).

我正在使用 JTree 浏览文件夹的内容,我希望当用户单击文件时,软件会显示它的预览(其第一页的屏幕截图)。

The files are mostly Office documents and PDF.

这些文件主要是 Office 文档和 PDF。

I manage to do it for PDF file using a module downloaded from Sun, but I'd like to know if there is a way to do it using any software (JARs preferably) or even the built-in Windows API.

我设法使用从 Sun 下载的模块为 PDF 文件执行此操作,但我想知道是否有办法使用任何软件(最好是 JAR)甚至内置的 Windows API 执行此操作。

I was thinking of converting the file to PDF then do a preview of this PDF but this isn't optimal.

我正在考虑将文件转换为 PDF,然后预览此 PDF,但这不是最佳选择。

Any ideas ?

有任何想法吗 ?

采纳答案by Anthony O.

I've got the similar problem and the best I found after couple of days of googling is following.

我遇到了类似的问题,经过几天的谷歌搜索后我发现的最好的问题是。

Alfrescohas the same problem and resolved it with :

Alfresco也有同样的问题并通过以下方式解决:

  • An open office which runs in server mode (socket) and all the office documents are sent by alfresco to open office in order to convert them in PDF
  • Those PDF are converted to .swf viewer thanks to SWFTOOLS
  • This .swf is integrated in the HTML
  • For images, it uses ImageMagickto create small version of the file I suppose
  • 以服务器模式(套接字)运行的开放式办公室,所有的办公室文件都由 alfresco 发送到开放式办公室,以便将它们转换为 PDF
  • 由于SWFTOOLS,这些 PDF 被转换为 .swf 查看器
  • 此 .swf 已集成在 HTML 中
  • 对于图像,它使用ImageMagick创建我想的文件的小版本

Personnaly, I will try to implement it this way :

Personnaly,我将尝试以这种方式实现它:

  • Converting office documents to PDF thanks to open office in socket mode
  • Transform the first page of the PDF into a PNG thanks to JPedallibrary (the LGPL version)
  • Diplay that PNG to the end user
  • For images I would perhaps use ImageMagick too ... but for now, I'm using SeamImage.scaleToFitAPI
  • 由于在套接字模式下开放办公室,将办公室文档转换为 PDF
  • 借助 JPedal库(LGPL 版本)将 PDF 的第一页转换为 PNG
  • 将该 PNG 显示给最终用户
  • 对于图像,我可能也会使用 ImageMagick ......但现在,我使用的是Seam Image.scaleToFitAPI

回答by Dennis Stritzke

I had the same problem too and stumbled over this thread. Starting with the solution from Anthony I am using Libre Office in socket mode to convert office documents directly to a PNG. Unfortunately this isn't posible from PDF's. Hereis a good overview which ways are possible.

我也有同样的问题,偶然发现了这个线程。从 Anthony 的解决方案开始,我在套接字模式下使用 Libre Office 将办公文档直接转换为 PNG。不幸的是,这在 PDF 中是不可能的。是一个很好的概述,哪些方法是可能的。

unoconv --connection 'socket,host=127.0.0.1,port=2220,tcpNoDelay=1;urp;StarOffice.ComponentContext' -f png -e PageRange=1 your_file_name.extension

Little reference to start Libre Office in socket mode: click me

在套接字模式下启动 Libre Office 的小参考:点我

回答by Martijn Courteaux

I asked this a long time ago: solution

我很久以前问过这个问题:解决方案