I want to display thumbnails of the uploaded pdf files on my website(ASP.NET). So far I have done following things.
You could probably use one of the general-purpose PDF libraries: • Ghostscript - C, available under the GPL • Poppler - C++, available under the GPL • Adobe PDF Library SDK - expensive Google reveals quite a few PDF-to-image converters which you may be able to incorporate if one of the above options doesn't work.
Matthew Ephraim released an open source wrapper for Ghostscript that sounds like it does what you want and is in C#. Link to Source Code: https://github.com/mephraim/ghostscriptsharp Link to Blog Posting: http://www.mattephraim.com/blog/2009/01/06/a-simple-c-wrapper-for-ghostscript/ You can make a simple call to the GeneratePageThumb method to generate a thumbnail (or use GeneratePageThumbs with a start and end page number to generate thumbnails for multiple seperate pages, with each page being a seperate output file), default file format is jpeg but you can change it, and many other options, by using the alternate GenerateOutput method call and specify options such as file format, page size, etc...
Now while following instructions of http://mattephraim.com/blog/2009/01/06/a-simple-c-wrapper-for-ghostscript/ I have installed ghostscript on my system which is windows 8 64-bit.
Now I've created a solution containing the test project by above guy and in my own project i am calling a function of his project
try
{
GhostscriptSharpTests.GhostscriptSharpTests ss = new GhostscriptSharpTests.GhostscriptSharpTests();
ss.GenerateSinglePageThumbnail();
}
catch (Exception ex)
{
}
but I am getting an exception :
Unable to load DLL 'gsdll32.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
About the error:
The error you are getting could be due gsdll32.dll could not be found or a wrong Ghostscript version installation you used. For a 64bit system you need to install 64bit Ghostscript library which has gsdll64.dll. If you compile your application for AnyCPU platfrom target, on a 64bit system it will run as 64bit process and you will need gsdll64.dll. If you compile your application as x86 and run it on a 64bit system, your application will run as 32bit process and you can use gsdll32.dll. When you use DllImport make sure that dll your are trying to call is in a same (bin) folder your applicatione executes or it can be in windows\system. If you want custom dll location, you can use full path in a DllImport ([DllImport("C:\Program Files\gs\gs9.14\bin\gsdll32.dll", EntryPoint = "gsapi_new_instance")]
) which is normally not recommended.
Why dont you simply use Ghostscript.NET library. It's a well tested native Ghostscript library wrapper which will allow you to do what you need and it's compatible with both x86 and x64 Ghostscript libraries.
Sample code that shows you how to rasterize pdf to image is here: https://ghostscriptnet.codeplex.com/SourceControl/latest#Ghostscript.NET/Ghostscript.NET.Samples/Samples/RasterizerSample.cs
Try different (lower) values with "desired_x_dpi" and "desired_y_dpi" and output image will be smaller.
I used Ghostscript.NET using NuGet in my ASP.NET Core 1.0 Project. Don't forget to install GhostScript from here
.
Also notice the 32/64 bit version of DLL used based on your platform + app configuration.
I would like this function to support other filetypes with Name & generic icon of type like Word/excel, etc.
private void createThumbnail(string sourcePath, Guid targetFile, string fileExtension, string uploadPath, string Name)
{
if (fileExtension.ToUpper() != ".PDF") // todo: Use "Name" to create thumbnail for other types
return;
try
{
int dpi = 72;
//GhostscriptVersionInfo gvi = new GhostscriptVersionInfo(@"C:\Program Files\gs\gs9.20\bin\gsdll64.lib");
GhostscriptVersionInfo gvi = new GhostscriptVersionInfo(_AppSettings.GhostscriptLibPath);
_logger.LogInformation("[createThumbnail] gvi.DllPath: {0}, gvi.Version: {1}", gvi.DllPath, gvi.Version);
GhostscriptProcessor proc = new GhostscriptProcessor(gvi);
using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer())
{
rasterizer.Open(sourcePath, gvi, false);
int pageNumber = 1;
string targetPath = Path.Combine(uploadPath, targetFile + ".png");
Image img = rasterizer.GetPage(dpi, dpi, pageNumber);
Image newImage = img.GetThumbnailImage(_AppSettings.DocThumbnailWidth, _AppSettings.DocThumbnailHeight, null, new System.IntPtr());
newImage.Save(targetPath, ImageFormat.Png);
_logger.LogInformation("[createThumbnail] Thumbnail image saved, targetPath: {0}", targetPath);
}
}
catch (Exception e)
{
_logger.LogError("Thumbnail could not be generated for file: {0}", sourcePath, e);
//throw;
}
}
Keep the gsdll32.dll in the project but set it to be copied to the output/bin folder and it should pick it up in your app.
User contributions licensed under CC BY-SA 3.0