简体   繁体   English

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

[英]How to use Ghostscript for converting PDF to Image

I found that Ghostscript is able to convert PDF to Image format. 我发现Ghostscript能够将PDF转换为图像格式。

I tried PDF to Image Converter but not able to understand it clearly. 我尝试使用PDF到图像转换器,但无法清楚地理解它。

I have installed gs905w64.exe but when I tried to add reference to my web application I am getting this error. 我已经安装了gs905w64.exe但是当我尝试add reference对我的Web应用程序的add reference ,我收到此错误。

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

You can use C# to run the GhostScript command line or use Platform Invoke (pInvoke) calls to call the GhostScript dll directly. 您可以使用C#运行GhostScript命令行或使用Platform Invoke(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. GhostScript主要基于文件,因此输入是磁盘上文件的路径,输出是磁盘上文件的创建。 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. 用于调用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. 我有C#包装器,可以用来调用ghostscript dll,如果你给我发电子邮件(地址在个人资料上),我会发给你。

HTH HTH

UPDATE: 更新:

code repo moved to https://bitbucket.org/brightertools/ghostscript 代码回购转移到https://bitbucket.org/brightertools/ghostscript

You Do not need to add any DLL reference to your project. 您不需要向项目添加任何DLL引用。 First download the gs910w32.exe application file then install it to your local computer. 首先下载gs910w32.exe应用程序文件,然后将其安装到本地计算机。 Get the location of the installed .exe file eg:- 获取已安装的.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. 您可以使用-r标志在参数中指定输出图像的宽高比。 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. 如果你使用“-r300”,图像的宽度将是3000像素,高度将相应地改变,从上面的参数你将得到1024到768大小的jpg图像。

The gsdll32.dll file is not a managed .NET library. gsdll32.dll文件不是托管的.NET库。 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. 您必须将其作为“内容”(菜单:添加现有项目)包含在项目中,然后让VS将其复制到输出目录。 Meanwhile you should read the Ghostscript API docs and this article on PInvoke.net on how to reference the Ghostscript functions. 同时你应该阅读PInvoke.net的Ghostscript API文档本文,了解如何引用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库。

Why do you try to add the library as reference to your project? 为什么要尝试添加库作为项目的参考? gsdll32.dll is 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. 当我使用Visual C#Express 2010构建示例项目时,我得到一个exe文件。 If I execute it it tries to access the gsdll32.dll . 如果我执行它,它会尝试访问gsdll32.dll The problem is now that on a 64bit system a 64bit executable is generated but the gsdll32.dll is compiled for 32bit. 问题是现在在64位系统上生成64位可执行文件,但gsdll32.dll编译为32位。

The correct solution would be to modify the source code and replace gsdll32.dll with gsdll64.dll everywhere it occurs. 正确的解决方案是修改源代码并将gsdll32.dll替换为gsdll64.dll The simpler solution is to use the 64 bit version of Ghostscript, copy the gsdll64.dll into the same directory as the ConvertPDF.exe and rename it to gsdll32.dll . 更简单的解决方案是使用64位版本的Ghostscript,将gsdll64.dll复制到与ConvertPDF.exe相同的目录中,并将其重命名为gsdll32.dll This definitely works - just tested and converted a PDF to TIFF. 这肯定有效 - 只是测试并将PDF转换为TIFF。

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 - >工具 - >库包管理器 - >包管理器控制台

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM