为什么我不能用ImageMagickNet打开PDF文件?

时间:2012-07-06 19:33:46

标签: c# .net pdf imagemagick imagemagick.net

以下是相关代码:

MagickNet.InitializeMagick();
ImageMagickNET.Image image = new ImageMagickNET.Image(@"C:\temp.pdf");
image.Quality = 100;
image.CompressType = ImageMagickNET.CompressionType.LosslessJPEGCompression;
image.Write(@"C:\temp.jpg");

我相当肯定这段代码应该有效,但我得到了一个非常有用的例外:External component has thrown an exception.

此行例抛出此异常:ImageMagickNET.Image image = new ImageMagickNET.Image(@"C:\temp.pdf");

InnerException:null

堆栈跟踪:

   at Magick.Image.{ctor}(Image* , basic_string<char\,std::char_traits<char>\,std::allocator<char> >* )
   at ImageMagickNET.Image..ctor(String imageSpec)
   at WindowsFormsApplication1.Form1.ReadQRCode(String doc) in C:\Users\me\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.Designer.cs:line 126
   at WindowsFormsApplication1.Form1.seperatePDFsInOrder(String fileName) in C:\Users\me\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.Designer.cs:line 109
   at WindowsFormsApplication1.Form1.InitializeComponent() in C:\Users\me\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.Designer.cs:line 44
   at WindowsFormsApplication1.Form1..ctor() in C:\Users\me\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 16
   at WindowsFormsApplication1.Program.Main() in C:\Users\me\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 20
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

任何人都知道我可能做错了什么?

1 个答案:

答案 0 :(得分:2)

您似乎在第二个引用行中将image = new声明为 * .pdf 文件。最好用c:\tmp.jpg试一试。甚至更好c:\temp\tmp.jpg ...

ImageMagick应用其'我想将此文件解析为PDF' -mode,如果它看到后缀* .pdf。 (仅当文件名没有后缀时才应用其魔术文件类型发现例程。)

运行代码的用户也可能无法写入文件c:\tmp.jpg。可能有两个原因:

  1. c:\因为该用户无法写入目录。
  2. 该文件已存在,且该用户无法覆盖(可能属于其他用户)。
  3. 最后,请注意,ImageMagick将PDF作为输入处理的能力依赖于外部“委托”:它本身无法完成该作业,它需要在同一主机上安装Ghostscript才能调用它让它完成这项工作......