问题
I have an ASP.NET WebForms application (written in C#) that allows users to upload files using the FileUpload control.
What'd be great is if I could automatically generate thumbnails from files. Images such as JPG/PNG are trivial of course, but users will often upload .DOC and .PDF files - is there a way I can essentially convert .DOC and .PDF files to images so I can get a thumbnail?
Thanks!
回答1:
For PDf you can try ABCPDF from websupergoo. With that you should be able to generate a thumbnail, Documentation link for creating a PNG from a PDF. http://www.websupergoo.com/helppdf8net/source/4-examples/19-rendering.htm
also for Doc, it is a little more complex, you are going to have to install the office application on the server that host the apps and from there manipulate the image.
Look at this article. It lets you extract the thumbnail image that Windows Explorer uses for Office documents and regular images. http://msdn.microsoft.com/en-us/library/aa289172.aspx
回答2:
On paper the most hopeful approach would be to do so server side. So, if the file were uploaded, the server could generate a preview image.
On Windows, there would be a few options.
a) Windows Shell has a way to do it. The path is dangerous though. b) https://filepreviews.io/ Is a service that does it, but they charge. c) box is another service that does it.
Now on Linux, or even, perhaps, a Linux in a Windows subsystem, could do it with some command line utilities.
a) Convert the doc to pdf:
Convert DOC to PDF
b) Then, get a bitmap of the first page of the pdf, use ghostscript. ghostscript also exists for .NET and Windows.
Bitmap of PDF First Page
回答3:
For PDF you may find my answer here: Generate a pdf thumbnail (open source/free)
Note that this one answer is the only that is legal without any additional software costs - some open source software comes at a hefty license if you don't use it according to their terms.
Also it should be easy to use this answer for the doc file - as far as I know OpenOffice generates such a thumbnail.
回答4:
You can use GhostScriptWrapper for generating image thumbnails directly from pdf.You can get ghostScriptWrapper from here https://github.com/mephraim/ghostscriptsharp or you can include ghostscript dll from nuget package manager solution.
回答5:
Client-side, I haven't found anything useful.
However, Server-side, there is the option of converting first .docx and .xlsx files to a temporary PDF file, and then create the thumbnail. Use Microsoft.Office.Interop for the first step, and then Microsoft.WindowsAPICodePack.Shell.ShellFile for the second step.
Here is an article that might help you out
来源:https://stackoverflow.com/questions/17522506/c-asp-net-get-thumbnail-from-pdf-doc-files