简体   繁体   English

如何使用C#和ImageMagick将PDF转换为图像?

[英]How to convert PDF to images using C# and ImageMagick?

I would like to convert a PDF file to .GIF using C# and magicknet.dll. 我想使用C#和magicknet.dll将PDF文件转换为.GIF。 I have added the reference to the MagickNet Dll to my project. 我已将MagickNet Dll的引用添加到我的项目中。

MagickNet.Magick.Init();
MagickNet.Image img = new MagickNet.Image("d:/aa.pdf");
img.Write("d:/bb.gif");
MagickNet.Magick.Term();
img.Dispose();
System.Runtime.InteropServices.SEHException was unhandled by user code
  Message="External component has thrown an exception."
  Source="ImageMagickNET"
  ErrorCode=-2147467259
  StackTrace:
       at Magick.Image.{ctor}(Image* , basic_string\,std::allocator >* )
       at ImageMagickNET.Image..ctor(String imageSpec)
       at Test1._Default.Button1_Click(Object sender, EventArgs e) in C:\Users\PANKAJ\Documents\Visual Studio 2008\Projects\Test1\Test1\Default.aspx.cs:line 31
       at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException:

ImageMagick requires GhostScript to Interpret PDF files. ImageMagick需要GhostScript来解释PDF文件。 If you want you can call the GhostScript dll directly (contact me via my profile, I will send you ac# wrapper) 如果你想要你可以直接调用GhostScript dll(通过我的个人资料与我联系,我会发给你ac#wrapper)

Alternatively you can use the GhostScript command line or a commercial 3rd party component, eg the PDF libraries from Tall Components. 或者,您可以使用GhostScript命令行或商业第三方组件,例如Tall Components的PDF库。

Magic.Net is a C# port for popular library ImageMagick. Magic.Net是流行库ImageMagick的C#端口。 Install Magick.net using Nuget package from url https://www.nuget.org/packages/Magick.NET-Q16-AnyCPU/ . 使用来自url https://www.nuget.org/packages/Magick.NET-Q16-AnyCPU/的 Nuget包安装Magick.net。 This way you can use C#. 这样你就可以使用C#了。 See code below 见下面的代码

Note it will append images vertically. 请注意,它将垂直附加图像。 Similarly you can append horizontally ie substitute images.AppendHorizontally 类似地,您可以水平追加,即替换图像。水平附近

using ImageMagick;

string inputPdf= @"C:\my docs\input.pdf";
string outputPng= @"C:\my docs\output.png";

using (MagickImageCollection images = new MagickImageCollection())
{
    images.Read(inputPdf);
    using (IMagickImage vertical = images.AppendVertically())
        {
            vertical.Format = MagickFormat.Png;
            vertical.Density = new Density(300);  
            vertical.Write(outputPng);
        }
}

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

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