【问题标题】:Ghost Script pdf thumbnail in .NET.NET 中的 Ghost 脚本 pdf 缩略图
【发布时间】:2014-06-08 08:18:29
【问题描述】:

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

  1. 从这个链接我得到了使用ghostscript的想法How to generate thumbnail for some pages of a PDF file?

您可能会使用其中一种通用 PDF 库: • Ghostscript - C,在 GPL 下可用 • Poppler - C++,在 GPL 下可用 • Adob​​e PDF Library SDK - 昂贵 如果上述选项之一不起作用,Google 会提供很多 PDF 到图像的转换器。

  1. 然后Generate a pdf thumbnail (open source/free) 告诉我去寻找提到的包装器

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)

【问题讨论】:

    标签: .net ghostscript ghostscriptsharp


    【解决方案1】:

    关于错误:

    您遇到的错误可能是由于找不到 gsdll32.dll 或您使用了错误的 Ghostscript 版本安装。对于 64 位系统,您需要安装具有 gsdll64.dll 的 64 位 Ghostscript 库。如果您为 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://ghostscriptnet.codeplex.com/SourceControl/latest#Ghostscript.NET/Ghostscript.NET.Samples/Samples/RasterizerSample.cs

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

    【讨论】:

      【解决方案2】:

      我在我的 ASP.NET Core 1.0 项目中使用了 NuGet 的 Ghostscript.NET。不要忘记从here 安装 GhostScript。

      还要注意根据您的平台 + 应用配置使用的 32/64 位版本的 DLL。

      我希望此功能支持其他文件类型,例如 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;
              }
          }
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 2014-02-25
          • 2011-05-07
          • 2012-02-19
          • 2017-02-01
          • 2017-08-14
          • 1970-01-01
          • 2021-06-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多