How to preview any files inside a Java application

2020-06-04 06:04发布

问题:

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).

The files are mostly Office documents and 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.

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

Any ideas ?

回答1:

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

Alfresco has the same problem and resolved it with :

  • 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 ImageMagick to create small version of the file I suppose

Personnaly, I will try to implement it this way :

  • Converting office documents to PDF thanks to open office in socket mode
  • Transform the first page of the PDF into a PNG thanks to JPedal library (the LGPL version)
  • Diplay that PNG to the end user
  • For images I would perhaps use ImageMagick too ... but for now, I'm using Seam Image.scaleToFit API


回答2:

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. Here is a good overview which ways are possible.

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



回答3:

I asked this a long time ago: solution