6

I would like to convert a PDF file to .GIF using C# and magicknet.dll. I have added the reference to the MagickNet Dll to my project.

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: 
Andrea
  • 11,801
  • 17
  • 65
  • 72
ankush
  • 1,051
  • 3
  • 19
  • 34
  • http://stackoverflow.com/questions/1920269/net-library-for-exporting-pdf-pages-to-a-imagesjpg-or-png – Jørn Schou-Rode Feb 01 '10 at 11:11
  • Is there a way to determine the number of pages or even better is there a way to split the pdf with out passing the firstpage or lastpage? – ankush Feb 10 '10 at 11:43

2 Answers2

1

ImageMagick requires GhostScript to Interpret PDF files. If you want you can call the GhostScript dll directly (contact me via my profile, I will send you a c# wrapper)

Alternatively you can use the GhostScript command line or a commercial 3rd party component, eg the PDF libraries from Tall Components.

Mark Redman
  • 24,079
  • 20
  • 92
  • 147
  • hii Mark.. thanks you ... actually i have ghostscript dll... but when i add this dll in my program then an error arise ... dat make sure that this dll is accessible and it is a valid com component.. – ankush Feb 01 '10 at 13:34
  • 1
    The GhostScript dll is not a COM component and is an unmanaged dll, its required platform invoke calls. – Mark Redman Feb 01 '10 at 14:30
  • so how can i add GhostScript dll in my project? i am using visual studio 2008 on window vista. i want to add GhostScript dll in my web application. – ankush Feb 02 '10 at 07:11
  • As I mentioned in my answer, email me (email on profile page) and I will send you a C# wrapper that will enable you to convert PDF to images. Apart from that you will need to look at the GhostScript documentation to see what the interface is and use pInvoke calls to connect to the Ghostcript dll. – Mark Redman Feb 02 '10 at 07:50
  • hiii mark.. i used ur code .. but it is giving a error ... that unable to load dll..... please help me lock (typeof(Pdf_2_image.GhostScript)) { GC.Collect(); Pdf_2_image.GhostScript.OutputDevice outputDevice = Pdf_2_image.GhostScript.OutputDevice.jpeg; Pdf_2_image.GhostScript.DeviceOption[] deviceOptions = Pdf_2_image.GhostScript.DeviceOptions.jpg(100); using (Pdf_2_image.GhostScript ghostScript = new Pdf_2_image.GhostScript(@"C:\Program Files\gs\gs8.70\bin")) ; { – ankush Feb 03 '10 at 07:56
  • Is there a way to determine the number of pages or even better is there a way to split the pdf with out passing the firstpage or lastpage? – ankush Feb 10 '10 at 11:43
  • We use the iTextSharp Library to split/join PDF's – Mark Redman Feb 10 '10 at 12:13
1

Magic.Net is a C# port for popular library ImageMagick. Install Magick.net using Nuget package from url https://www.nuget.org/packages/Magick.NET-Q16-AnyCPU/ . This way you can use C#. See code below

Note it will append images vertically. Similarly you can append horizontally i.e. 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);
        }
}
Sujit Singh
  • 752
  • 1
  • 9
  • 23