.net NET中的鬼脚本pdf缩略图

.net NET中的鬼脚本pdf缩略图,.net,ghostscript,ghostscriptsharp,.net,Ghostscript,Ghostscriptsharp,我想在我的网站(ASP.NET)上显示上传的pdf文件的缩略图。 到目前为止,我已经做了以下事情 通过这个链接,我想到了使用ghostscript 您可能会使用其中一个通用PDF库: •Ghostscript-C,可在GPL下获得 GPL下的PopFuel-C++ •Adobe PDF Library SDK-价格昂贵 谷歌展示了相当多的PDF到图像转换器,如果上面的一个选项不起作用,你可以将其合并 然后叫我去找那个包装纸 Matthew Ephraim发布了一个Ghostscript的开源包装

我想在我的网站(ASP.NET)上显示上传的pdf文件的缩略图。 到目前为止,我已经做了以下事情

  • 通过这个链接,我想到了使用ghostscript
  • 您可能会使用其中一个通用PDF库: •Ghostscript-C,可在GPL下获得 GPL下的PopFuel-C++ •Adobe PDF Library SDK-价格昂贵 谷歌展示了相当多的PDF到图像转换器,如果上面的一个选项不起作用,你可以将其合并

  • 然后叫我去找那个包装纸
  • Matthew Ephraim发布了一个Ghostscript的开源包装器,它听起来像是做了你想做的事情,并且是C语言的。 链接到源代码: 链接到博客帖子: 您可以简单地调用GeneratePageThumb方法生成缩略图(或者使用带有起始和结束页码的GeneratePageThumbs为多个单独的页面生成缩略图,每个页面都是一个单独的输出文件),默认文件格式为jpeg,但您可以更改它,以及许多其他选项,通过使用alternate GenerateOutput方法调用并指定文件格式、页面大小等选项

    现在,按照的说明,我已经在我的系统上安装了ghostscript,它是windows 8 64位的

    现在,我已经创建了一个包含上面的家伙的测试项目的解决方案,在我自己的项目中,我调用了他的项目的一个函数

    try
            {
                GhostscriptSharpTests.GhostscriptSharpTests ss = new GhostscriptSharpTests.GhostscriptSharpTests();
                ss.GenerateSinglePageThumbnail();
            }
            catch (Exception ex)
            { 
    
            }
    
    但我有一个例外:

    无法加载DLL“gsdll32.DLL”:找不到指定的模块。(来自HRESULT的异常:0x8007007E)

    关于错误:

    您收到的错误可能是由于找不到gsdll32.dll或使用了错误的Ghostscript版本安装。对于64位系统,您需要安装具有gsdll64.dll的64位Ghostscript库。如果从目标为任何CPU平台编译应用程序,则在64位系统上,它将作为64位进程运行,并且需要gsdll64.dll。如果将应用程序编译为x86并在64位系统上运行,则应用程序将以32位进程运行,并且可以使用gsdll32.dll。使用DllImport时,请确保您尝试调用的dll位于ApplicationOne执行的同一(bin)文件夹中,也可以位于windows\system中。如果需要自定义dll位置,可以在通常不建议使用的DllImport(
    [DllImport(“C:\Program Files\gs\gs9.14\bin\gsdll32.dll”,EntryPoint=“gsapi\u new\u instance”))中使用完整路径

    你们为什么不干脆用图书馆呢。它是一个经过良好测试的本机Ghostscript库包装器,允许您执行所需操作,并且与x86和x64 Ghostscript库兼容

    演示如何将pdf光栅化为图像的示例代码如下:


    尝试使用不同的(较低的)值“desired_x_dpi”和“desired_y_dpi”,输出图像将更小。

    我在ASP.NET Core 1.0项目中使用了Ghostscript.NET。别忘了从安装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;
            }
        }
    

    将gsdll32.dll保留在项目中,但将其设置为复制到output/bin文件夹中,并应在应用程序中拾取它