0

Possible Duplicate:
Best way to convert pdf files to tiff files

I'm looking for a free library that allows me to convert a PDF document to a (or several) TIFF images. If Tiff is not possible it can also be other images formats.

I read about a way to do this with Ghostscript, but I couldn't find a good explanation. So any hints how to do that?

EDIT: According to the comment I use this command to execute:

Process.Start("gswin32c", "-dNOPAUSE -q -sDEVICE=tiffg4 -dBATCH -sOutputFile=" + fileName + ".tif " +  fileName + ".pdf");

I see that GhostScript is executed in the command line, But I always get an error:

  System.ArgumentException was unhandled
  Message=The contact with id '16778241' does not currently exist.
Parameter name: contact
  Source=Microsoft.Surface.Presentation
  ParamName=contact
  StackTrace:
       at Microsoft.Surface.Presentation.Contacts.CheckIfCanBeCapturedOrReleased(Contact contact)
       at Microsoft.Surface.Presentation.Contacts.CaptureContact(Contact contact, IInputElement element)
       at Microsoft.Surface.Presentation.Controls.Primitives.ButtonBaseAdapter.OnContactDown(ContactEventArgs e)
       at Microsoft.Surface.Presentation.Controls.SurfaceButton.OnContactDown(ContactEventArgs e)
       at Microsoft.Surface.Presentation.Controls.SurfaceButton.Microsoft.Surface.Presentation.Controls.IContactEventThunk.OnContactDownThunk(ContactEventArgs e)
       at Microsoft.Surface.Presentation.Controls.InputElementAdapter.OnContactDownThunk(Object sender, ContactEventArgs e)
       at Microsoft.Surface.Presentation.ContactEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
       at System.Windows.Input.InputManager.ProcessStagingArea()
       at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
       at Microsoft.Surface.Presentation.InputSurfaceProviderBase.DoProcessInput(Object obj)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.ProcessQueue()
       at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
       at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.Run()
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at Prototype_Concept_2.App.Main() in C:\Users\Roflcoptr\Documents\Visual Studio 2008\Projects\Prototype_Concept_2\Prototype_Concept_2\obj\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(Assembly 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)
       at System.Threading.ThreadHelper.ThreadStart()

InnerException:

Why this?

Community
  • 1
  • 1
  • You could print and then scan the document. ;-D – Jeffrey L Whitledge Feb 22 '11 at 20:31
  • What type of PDF files do you need to support? There are at least 4 flavors with varying degrees of difficulty. – JohnFx Feb 22 '11 at 20:32
  • use `Process.Start("gswin32c XXXX").WaitForExit()` to invoke the second answer in the linked question. – jgauffin Feb 22 '11 at 20:35
  • @jgauffin but then how can i ensure that gswin32c is available on every pc where my application is running? –  Feb 22 '11 at 20:45
  • I dont think it is a duplicate since I want to do that programatically in my application. –  Feb 22 '11 at 20:49
  • 1
    There are dozens of commercial components which can do this. I chose ghostscript (which I invoke through code) since it's fast and the result got a high quality. How you can make sure that it's available? It's a console app, just include it in your installer (if the license permits it).. – jgauffin Feb 22 '11 at 20:55
  • @jgauffin Thanks I tried it and updated my question. –  Feb 22 '11 at 21:08
  • You got to include the path to gswin32c or set the working directory. – jgauffin Feb 22 '11 at 21:10
  • @jgauffin hmm ok I thought if i copy the file into my /bin/Debug folder it would work. But thanks I'll try. –  Feb 22 '11 at 21:13
  • From MSDN: This overload does not allow command-line arguments for the process. If you need to specify one or more command-line arguments for the process, use the Process.Start(ProcessStartInfo) or Process.Start(String, String) overloads. – Yoshi Feb 22 '11 at 21:25
  • @Roflcoptr You checked out http://www.codeproject.com/KB/cs/GhostScriptUseWithCSharp.aspx which will allow you to download the entire .sln? – Aaron McIver Feb 22 '11 at 21:25
  • @JeffreyLWhitledge: Why don't we just etch it into a menhir? It would take about the same time as your solution. – hofnarwillie Jul 09 '13 at 22:41

2 Answers2

2

You cannot use the Process.Start(string filename) overload to supply command line parameters. From MSDN:

This overload does not allow command-line arguments for the process. If you need to specify one or more command-line arguments for the process, use the Process.Start(ProcessStartInfo) or Process.Start(String, String) overloads. http://msdn.microsoft.com/en-us/library/53ezey2s.aspx

So try the following instead:

Process.Start(@"C:\Users\Roflcoptr\Documents\Visual Studio 2008\Projects\Prototype_Concept_2\Prototype_Concept_2\bin\Debug\gswin32c", "-dNOPAUSE -q -g300x300 -sDEVICE=tiffg4 -dBATCH -sOutputFile=Report_22_02_2011_21_18.tif Report_22_02_2011_21_18.pdf");

Yoshi
  • 3,325
  • 1
  • 19
  • 24
  • I updated the question to show how I do it – RoflcoptrException Feb 22 '11 at 21:43
  • @Roflcoptr The latest exception you posted doesn't seem to be related to the Process.Start call. Try putting the Process.Start in a fresh C# console application as the first call in the static Main to isolate the issue. However, if you are saying that the `Process.Start` now works and you want help with another problem, please post the code that is relevant to the new problem, as the `Process.Start` does not seem to be related to the problem any more. – Yoshi Feb 22 '11 at 22:47
  • you're right, I added the method to a backgroundworker and now it is running as expected! Thanks! – RoflcoptrException Feb 22 '11 at 23:10
1

Can you post up the Process.Start() line of code?

I am guessing the error of file not found is coming from the path being incorrect. That being missing the quotes. Because of the space in visual and studio it would assume that "C:\Users\Roflcoptr\Documents\Visual" is the directory.

Process.Start(@"\"C:\Users\Roflcoptr\Documents\Visual Studio 2008\Projects\Prototype_Concept_2\Prototype_Concept_2\bin\Debug\gswin32c\" -dNOPAUSE -q -g300x300 -sDEVICE=tiffg4 -dBATCH -sOutputFile=Report_22_02_2011_21_18.tif Report_22_02_2011_21_18.pdf");

The best way to test this, maybe to first fire up a command prompt and see if you can run that command line successfully before using it in Process.Start() method call.

JonWillis
  • 3,146
  • 5
  • 32
  • 54
  • Yoshi (below) is correct, you do need to seperate the augments. I can remember doing this for a basic Excel to CSV tool i wrote that called another application. – JonWillis Feb 22 '11 at 21:38
  • I updated the question to show how I do it – RoflcoptrException Feb 22 '11 at 21:43
  • @Roflcoptr, from the stacktrace the Process.Start() has executed, and the error is occuring elsewhere in the code. Can you step through the code with the debugger to confirm process.start() completes successfully. – JonWillis Feb 22 '11 at 21:56
  • I completes. I can see it because the file is generated. But as soon as it is finsihed generating the exception is thrown. But I dont know why because I just used your line of code and nothing else. If i remove this line it works, if I add the line, the image is generated, but the exception is thrown – RoflcoptrException Feb 22 '11 at 21:57
  • @rolfcoptr, from the comments in yoshi's post it appears to be working, given it works ok in a standard alone project. I can only assume the problem lies in either lines of code after the process.start or there is a threading issue. – JonWillis Feb 23 '11 at 11:53