如何使用Ghostscript将PDF转换为图像

use*_*755 4 c# pdf image ghostscript

我发现Ghostscript能够将PDF转换为图像格式.

我尝试使用PDF到图像转换器,但无法清楚地理解它.

我已安装gs905w64.exe,但当我尝试访问add reference我的Web应用程序时,我收到此错误.

A reference to gsdll32.dll could not be added. No type libraries were found in the component.

Mar*_*man 9

您可以使用C#运行GhostScript命令行或使用Platform Invoke(pInvoke)调用直接调用GhostScript dll.

GhostScript主要基于文件,因此输入是磁盘上文件的路径,输出是磁盘上文件的创建.用于调用dll或exe的参数基本相同,因此直接调用dll没有很大的好处,但确实可以提供更好的代码.

我有C#包装器,可以用来调用ghostscript dll,如果你给我发电子邮件(地址在个人资料上),我会发给你.

HTH

更新:

代码回购转移到https://bitbucket.org/brightertools/ghostscript


S.R*_*nth 8

您不需要向项目添加任何DLL引用.首先下载gs910w32.exe应用程序文件,然后将其安装到本地计算机.获取已安装的.exe文件的位置,例如: -

"C:\ Program Files(x86)\ gs\gs8.64\bin\gswin32.exe"

在您的C#应用​​程序中使用它:

 
  private void PdfToJpg(string inputPDFFile, string outputImagesPath)
        {
            string ghostScriptPath = @"C:\Program Files (x86)\gs\gs8.64\bin\gswin32.exe";
            String ars = "-dNOPAUSE -sDEVICE=jpeg -r102.4 -o" + outputImagesPath + "%d.jpg -sPAPERSIZE=a4 " + inputPDFFile;
            Process proc = new Process();
            proc.StartInfo.FileName = ghostScriptPath;
            proc.StartInfo.Arguments = ars;
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            proc.Start();
            proc.WaitForExit();
        }
 

如果输入的PDF文件名有任何空格,则需要将参数更改为

 
String ars = "-dNOPAUSE -sDEVICE=jpeg -r102.4 -o" + outputImagesPath + "%d.jpg -sPAPERSIZE=a4 " +"\"" + inputPDFFile + "\"";
 

您可以使用-r标志在参数中指定输出图像的宽高比.如果你使用"-r300",图像的宽度将是3000像素,高度将相应地改变,从上面的参数你将得到1024到768大小的jpg图像.


fer*_*ero 6

gsdll32.dll文件不是托管的.NET库.您无法在项目中引用它.您必须将其作为"内容"(菜单:添加现有项目)包含在项目中,然后让VS将其复制到输出目录.同时你应该阅读PInvoke.net的Ghostscript API文档本文,了解如何引用Ghostscript函数.

请记住,Ghostscript是所有非托管代码,您必须在使用库后自行清理.

编辑:罗伯特说的也很重要.当然,您必须使用正确版本的Ghostscript库.