Ghostscript 将PDF文件转换成PNG图片 问题一二

    由于项目需求,需要将原来的PDF文档转换成图片文件,在网上找了一些PDF转图片的方法;测试了几个后,都有这样或那样的问题

    1、PDFLibNet.dll,这个类型最初还是挺好用的,能转图片和HTML,但现在好象已经不再更新了;而且转换时(部分文档)会出现文字丢失的问题

    2、Adobe Acrobat X Pro,这个太大,还需要注册激活;且网上找到的代码在WEB方式下不太好用,需要建个服务或命令行程序来作为中间件调用

    3、Ghostscript,这个看网上也有比较多的演示代码,不多,也比较适合我的要求,于是着重测试它了

    但在网上的演示代码,需要用到PDFBOX,IKVM等DLL文件(PDFBOX好象也很长时间没更新了),但演示代码中却只需要用到一个得到PDF页数的操作;测试下来,感觉有点多余

    Ghostscript,可以直接输出所有的PDF页面;如果需要输出指定页时,才需要用到PDF页数这个数据。

    所以,只需要在服务器上安装Ghostscript就可以了;不过,在这里需要提出来的是,测试下的9.00版以上的感觉不是很好用,8.71版不错,挺好用的

    9.00及以上版本,对于字体的处理好象还存在BUG或是我自己没配置好(通过修改:lib/cidfmap)文档

    在测试中,字体不能正常加载处理;

Substituting font Times-Bold for TimesNewRomanPS-BoldMT.
Loading NimbusRomNo9L-Medi font from %rom%Resource/Font/NimbusRomNo9L-Medi... 2432860 1126845 2423648 1025245 3 done.
Loading a TT font from C:/WINDOWS/fonts/simsun.ttc to emulate a CID font SimSun ... Done.
Substituting font Times-Roman for TimesNewRomanPSMT.
Loading NimbusRomNo9L-Regu font from %rom%Resource/Font/NimbusRomNo9L-Regu... 3770200 2395423 2776248 1391721 3 done.

    轩换文档时依然出现文字丢失的情况

    后来换成8.71版,再进行测试:

修改:lib/cidfmap:

在% Substitutions节下方追加(下面的内容只是针对我在测试中不能正常转换的文档设置,修改后,文档转换成功):

/MicrosoftYaHei << /FileType /TrueType /SubfontID 0 /CSI [(GB1) 2] /Path (C:/WINDOWS/fonts/msyh.ttf) >> ;
/LucidaSansUnicode << /FileType /TrueType /SubfontID 0 /CSI [(GB1) 2] /Path (C:/WINDOWS/fonts/msyh.ttf) >> ;

运行结果:

Loading a TT font from C:/WINDOWS/fonts/msyh.ttf to emulate a CID font MicrosoftYaHei ... Done.
   **** Warning: Pattern stream has unbalanced q/Q operators (too many q's)
Loading a TT font from C:/WINDOWS/fonts/msyh.ttf to emulate a CID font LucidaSansUnicode ... Done.

     生成的图片文字正常,原来不能正常转换的PDF文档都能正确的转换成图片了,文字未出现丢失的情况

在实际的操作中,我去除了PDFBOX的DLL文档引用,因为项目中已经引用了itextsharp.dll,它是可以得到分页数的。

#region PdfToImages
        /// <summary>
        /// PDF 转 PNG,需要事先安装<c>GhostScript</c>
        /// </summary>
        /// <param name="pdfFile">PDF文档路径</param>
        /// <param name="imgPath">图片存储文件夹路径</param>
        public static void PdfToImages(string pdfFile, string imgPath)
        {
            //如果文档不存在
            if (!FileHelper.FileExists(pdfFile))
            {
                return;
            }

            int pageCount = Utils.GetPdfPageCount(pdfFile);

            //如果文档为空
            if (pageCount == 0)
            {
                return;
            }

            //转换成的图片文件
            //生成文件名规则 %d 表示以数据进行标号
            //%03d 表示001.png,002.png
            string imgFile = string.Concat(imgPath, "%d.png");

            string bin = System.Web.Configuration.WebConfigurationManager.AppSettings["GhostScriptBin"];
            string exe = @"gswin32c.exe";
            string arguments = System.Web.Configuration.WebConfigurationManager.AppSettings["GhostScriptArguments"];

            using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
            {
                try
                {
                    proc.StartInfo.FileName = string.Concat(bin, exe);
                    proc.StartInfo.WorkingDirectory = bin;
                    proc.StartInfo.Arguments = string.Concat(arguments, " -sOutputFile="", imgFile, ""  "", pdfFile, """);
                    proc.StartInfo.UseShellExecute = false;
                    proc.StartInfo.CreateNoWindow = true;
                    proc.StartInfo.RedirectStandardError = true;
                    proc.StartInfo.RedirectStandardInput = false;
                    proc.StartInfo.RedirectStandardOutput = true;
                    proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                    proc.ErrorDataReceived += new DataReceivedEventHandler(PdfToImages_ErrorDataReceived);//重定向错误输出
                    proc.OutputDataReceived += new DataReceivedEventHandler(PdfToImages_OutputDataReceived);//重定向输出
                    proc.Start();
                    proc.BeginErrorReadLine();//此行不加的话,可能有些PDF文档会转换失败
                    proc.BeginOutputReadLine();
                    //等待退出
                    proc.WaitForExit(int.MaxValue);
                }
                finally
                {
                    proc.Close();
                }
            }
        }
原文地址:https://www.cnblogs.com/dreamcat/p/3937775.html