我想在我的网站(ASP.NET)上显示上传的 pdf 文件的缩略图。 到目前为止我已经做了以下事情。
您可能可以使用通用 PDF 库之一: • Ghostscript - C,可在 GPL 下使用 • Poppler - C++,可在 GPL 下使用 • Adobe PDF Library SDK - 昂贵 Google 展示了很多 PDF 到图像转换器,如果上述选项之一不起作用,您可以将其合并。
Matthew Ephraim 发布了 Ghostscript 的开源包装器,听起来它可以满足您的需求,并且是用 C# 编写的。 源代码链接:https://github.com/mephraim/ghostscriptsharp 博客文章链接:http://www.mattephraim.com/blog/2009/01/06/a-simple-c-wrapper-for-ghostscript/ 您可以简单地调用GeneratePageThumb方法来生成缩略图(或使用带有起始页码和结束页码的GeneratePageThumbs来生成多个单独页面的缩略图,每个页面都是一个单独的输出文件),默认文件格式是jpeg,但您可以可以通过使用备用的GenerateOutput方法调用并指定文件格式、页面大小等选项来更改它以及许多其他选项...
现在,按照 http://mattephraim.com/blog/2009/01/06/a-simple-c-wrapper-for-ghostscript/ 的说明进行操作时,我已在 Windows 8 64 位系统上安装了 Ghostscript 。
现在我创建了一个包含上述人员的测试项目的解决方案,在我自己的项目中我正在调用他项目的函数
try
{
GhostscriptSharpTests.GhostscriptSharpTests ss = new GhostscriptSharpTests.GhostscriptSharpTests();
ss.GenerateSinglePageThumbnail();
}
catch (Exception ex)
{
}
但我遇到了例外:
无法加载DLL“gsdll32.dll”:找不到指定的模块。 (HRESULT 异常:0x8007007E)
关于错误:
您收到的错误可能是由于找不到 gsdll32.dll 或您使用的 Ghostscript 版本安装错误造成的。对于 64 位系统,您需要安装 64 位 Ghostscript 库,其中包含 gsdll64.dll。如果您为 AnyCPU 平台目标编译应用程序,则在 64 位系统上它将作为 64 位进程运行,并且您将需要 gsdll64.dll。如果您将应用程序编译为 x86 并在 64 位系统上运行,则您的应用程序将作为 32 位进程运行,并且您可以使用 gsdll32.dll。当您使用 DllImport 时,请确保您尝试调用的 dll 位于应用程序执行的同一(bin)文件夹中,或者可以位于 windows\system 中。如果您想要自定义 dll 位置,您可以在 DllImport (
[DllImport("C:\Program Files\gs\gs9.14\bin\gsdll32.dll", EntryPoint = "gsapi_new_instance")]
) 中使用完整路径,这通常不推荐。
为什么不简单地使用 Ghostscript.NET 库。它是经过充分测试的本机 Ghostscript 库包装器,可让您完成所需的操作,并且与 x86 和 x64 Ghostscript 库兼容。
向您展示如何将 pdf 光栅化为图像的示例代码位于:https://github.com/ArtifexSoftware/Ghostscript.NET/blob/master/Ghostscript.NET.Samples/Samples/RasterizerSample1.cs
尝试使用“desired_x_dpi”和“desired_y_dpi”不同(较低)的值,输出图像会更小。
我在 ASP.NET Core 1.0 项目中使用了 Ghostscript.NET,并使用了 NuGet。不要忘记从
here
安装 GhostScript。
另请注意根据您的平台+应用程序配置使用的 DLL 的 32/64 位版本。
我希望此功能支持其他文件类型,具有名称和类型的通用图标,如 Word/excel 等。
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;
}
}