C# 如何使用 Ghostscript 将 PDF 转换为图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11517659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Ghostscript for converting PDF to Image
提问by user1530755
I found that Ghostscript is able to convert PDF to Image format.
我发现 Ghostscript 能够将 PDF 转换为图像格式。
I tried PDF to Image Converterbut not able to understand it clearly.
我尝试了PDF to Image Converter,但无法清楚地理解它。
I have installed gs905w64.exebut when I tried to add referenceto my web application I am getting this error.
我已经安装,gs905w64.exe但是当我尝试安装add reference我的 Web 应用程序时出现此错误。
A reference to gsdll32.dll could not be added. No type libraries were found in the component.
A reference to gsdll32.dll could not be added. No type libraries were found in the component.
采纳答案by Mark Redman
You can use C# to run the GhostScript command line or use Platform Invoke (pInvoke) calls to call the GhostScript dll directly.
您可以使用 C# 运行 GhostScript 命令行或使用平台调用 (pInvoke) 调用直接调用 GhostScript dll。
GhostScript is primarily file based, so the input is path to a file on disk and the output is the creation of files on disk. The parameters used to call either the dll or exe are basically the same, so there is not a huge benefit to calling the dll directly, but does make for nicer code.
GhostScript 主要基于文件,因此输入是磁盘上文件的路径,输出是磁盘上文件的创建。用于调用 dll 或 exe 的参数基本相同,因此直接调用 dll 并没有太大的好处,但确实可以使代码更好。
I have C# wrapper that can be used to call the ghostscript dll, if you email me (address on profile) I will sent it to you.
我有可用于调用 ghostscript dll 的 C# 包装器,如果您给我发电子邮件(个人资料中的地址),我会将其发送给您。
HTH
HTH
UPDATE:
更新:
code repo moved to https://bitbucket.org/brightertools/ghostscript
回答by Robert
Why do you try to add the library as reference to your project? gsdll32.dllis a native dll, not a Dot-Net library.
为什么要尝试将库添加为项目的参考?gsdll32.dll是本机 dll,而不是 Dot-Net 库。
When I build the sample project using Visual C# Express 2010 I get an exe file. If I execute it it tries to access the gsdll32.dll. The problem is now that on a 64bit system a 64bit executable is generated but the gsdll32.dllis compiled for 32bit.
当我使用 Visual C# Express 2010 构建示例项目时,我得到了一个 exe 文件。如果我执行它,它会尝试访问gsdll32.dll. 现在的问题是在 64 位系统上生成了 64 位可执行文件,但gsdll32.dll编译为 32 位。
The correct solution would be to modify the source code and replace gsdll32.dllwith gsdll64.dlleverywhere it occurs. The simpler solution is to use the 64 bit version of Ghostscript, copy the gsdll64.dllinto the same directory as the ConvertPDF.exeand rename it to gsdll32.dll. This definitely works - just tested and converted a PDF to TIFF.
正确的解决方案是修改源代码并gsdll32.dll用gsdll64.dll它出现的任何地方替换。更简单的解决方案是使用 64 位版本的 Ghostscript,将其复制gsdll64.dll到与 相同的目录中ConvertPDF.exe并将其重命名为gsdll32.dll. 这绝对有效 - 刚刚测试并将 PDF 转换为 TIFF。
回答by fero
The gsdll32.dllfile is nota managed .NET library. You can't reference it in your project. You have to include it in your project as "content" (menu: Add existing item) and let VS copy it to the output directory. Meanwhile you should read the Ghostscript API docsand this article on PInvoke.neton how to reference the Ghostscript functions.
该gsdll32.dll文件不是托管的 .NET 库。你不能在你的项目中引用它。您必须将其作为“内容”(菜单:添加现有项目)包含在您的项目中,并让 VS 将其复制到输出目录。同时,您应该阅读Ghostscript API 文档和PInvoke.net上有关如何引用 Ghostscript 函数的这篇文章。
Keep in mind that Ghostscript is all unmanaged code and that you have to do the clean-up yourself after using the library.
请记住,Ghostscript 都是非托管代码,您必须在使用库后自己进行清理。
Edit:What Robert said is important, too. Of course, you have to use the correct version of the Ghostscript library.
编辑:罗伯特所说的也很重要。当然,您必须使用正确版本的 Ghostscript 库。
回答by S.Roshanth
You Do not need to add any DLL reference to your project. First download the gs910w32.exe application file then install it to your local computer. Get the location of the installed .exe file eg:-
您不需要向项目添加任何 DLL 引用。首先下载 gs910w32.exe 应用程序文件,然后将其安装到本地计算机。获取已安装的 .exe 文件的位置,例如:-
"C:\Program Files (x86)\gs\gs8.64\bin\gswin32.exe"
“C:\Program Files (x86)\gs\gs8.64\bin\gswin32.exe”
use it in your C# application as :
在您的 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();
}
IF your input PDF file name has any spaces, you need to change the argument to
如果您输入的 PDF 文件名有任何空格,您需要将参数更改为
String ars = "-dNOPAUSE -sDEVICE=jpeg -r102.4 -o" + outputImagesPath + "%d.jpg -sPAPERSIZE=a4 " +"\"" + inputPDFFile + "\"";
you can specify the output image's aspect ratio in the argument with the -r flag. If you use "-r300" the width of the image will be 3000 pixel and height will change accordingly, from the above argument you will get 1024 to 768 size jpg image.
您可以使用 -r 标志在参数中指定输出图像的纵横比。如果您使用“-r300”,图像的宽度将为 3000 像素,高度将相应改变,根据上述参数,您将获得 1024 至 768 尺寸的 jpg 图像。
回答by Mayank
you need to run the below command to reference the library http://www.nuget.org/packages/GhostScriptSharp/
您需要运行以下命令来引用库 http://www.nuget.org/packages/GhostScriptSharp/
VS2012 --> Tools --> Library Package Manager --> Package Manager Console
VS2012 --> 工具 --> 库包管理器 --> 包管理器控制台

