简体   繁体   English

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

[英]Why can't I open a PDF file with ImageMagickNet?

Here is the code in question: 这是有问题的代码:

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

I'm fairly certain this code should work, but I get the exceptionally informative exception of: External component has thrown an exception. 我相当肯定这段代码应该可以工作,但是我得到了一个非常有用的例外: External component has thrown an exception.

This exception is thrown on the line: ImageMagickNET.Image image = new ImageMagickNET.Image(@"C:\\temp.pdf"); 抛出此异常: ImageMagickNET.Image image = new ImageMagickNET.Image(@"C:\\temp.pdf");

InnerException: null InnerException:null

StackTrace: 堆栈跟踪:

   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()

Anyone have any idea what I might be doing incorrectly? 任何人都知道我可能做错了什么?

You seem to declare your image = new as a *.pdf file in your 2nd quoted line. 您似乎在第二个引用行中声明了您的image = new作为* .pdf文件。 Better try it with c:\\tmp.jpg . 最好用c:\\tmp.jpg Or even better c:\\temp\\tmp.jpg ... 或者甚至更好的c:\\temp\\tmp.jpg ......

ImageMagick applies its 'I want to parse this file as a PDF' -mode if it sees the suffix *.pdf. 如果ImageMagick看到后缀* .pdf,则应用其“我想将此文件解析为PDF”模式。 (It applies its magic file type discovery routines only if the filename doesn't have a suffix.) (仅当文件名没有后缀时,它才应用其魔术文件类型发现例程。)

Also the user that is running the code possibly cannot write the file c:\\tmp.jpg . 运行代码的用户也可能无法写入文件c:\\tmp.jpg There could be two reasons: 可能有两个原因:

  1. c:\\ as a directory is not writeable for this user. c:\\因为该用户无法写入目录。
  2. the file already exists, and can't be overwritten by this user (it may belong to another user). 该文件已存在,并且不能被该用户覆盖(它可能属于另一个用户)。

Lastly, be aware that ImageMagick's capability to process PDFs as input relies on an external 'delegate': it can't do that job itself, it requires a Ghostscript installation on the same host to call it and let it do the job... 最后,请注意ImageMagick将PDF作为输入进行处理的能力依赖于外部“委托”:它本身无法完成该作业,它需要在同一主机上安装Ghostscript才能调用它并让它完成工作......

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

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