Ghostscript.Net Pdf 转 Image

需求:

  项目中需要实现PPT转Image的功能,之前项目中用的是使用Office COM组件实现的功能,通过.NET与Office COM组件的互操作(Interop)来操作Office文档

但是在生产环境中经常发生错误 "检索 COM 类工厂中 CLSID 为 {91493441-5A91-11CF-8700-00AA0060263B} 的组件失败,原因是出现以下错误: 8000401a 因为配置标识不正确,系统无法开始服务器进程。请检查用户名和密码。 (异常来自 HRESULT:0x8000401A)。",很是郁闷,在网上找了很多资料也没能完美的解决,然后就换了一种解决方案将PPT 转换成PDF 然后在将PDF 转换成Image。

  最后是使用 Ghostscript.Net 这个开源组件,来重写这个功能模块。

  第一步:

    下载 Ghostscript AGPL Release  32bit安装到本地 

      

  第二步:

    有两种使用方案:

      1、使用本地安装的【服务器上需要安装上述程序】

      2、找到dll拷贝到项目中,在项目中应用【不许安装,只需拷贝dll到bin下】

                  

示例代码C#:

 1       static void Main(string[] args)
 2         {
 3             int desired_x_dpi = 96;
 4             int desired_y_dpi = 96;
 5             string inputPdfPath = "";
 6             string outputPath = "";
 7 
 8             //本地安装版本代码
 9             //var _lastInstalledVersion =
10             //      GhostscriptVersionInfo.GetLastInstalledVersion(
11             //              GhostscriptLicense.GPL | GhostscriptLicense.AFPL,
12             //              GhostscriptLicense.GPL);
13 
14             //拷贝到项目版本
15             var _lastInstalledVersion = new GhostscriptVersionInfo($"{System.Environment.CurrentDirectory}\\gsdll32.dll");
16 
17             var _rasterizer = new GhostscriptRasterizer();
18 
19             _rasterizer.Open(inputPdfPath, _lastInstalledVersion, false);
20 
21             for (int pageNumber = 1; pageNumber <= _rasterizer.PageCount; pageNumber++)
22             {
23                 string pageFilePath = Path.Combine(outputPath, "Page-" + pageNumber.ToString() + ".Jpeg");
24                 Image img = _rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber);
25                 img.Save(pageFilePath, ImageFormat.Jpeg);
26 
27                 Console.WriteLine(pageFilePath);
28             }
29         }

 

参考资料:

  https://ghostscriptnet.codeplex.com/SourceControl/latest#Ghostscript.NET/Ghostscript.NET.Samples/Samples

posted @ 2017-03-02 13:25  伏允坤  阅读(3180)  评论(1编辑  收藏  举报