Link to home
Start Free TrialLog in
Avatar of klgrube
klgrube

asked on

How to convert PDF to JPG within VB.Net



Hi!

   This task is seemingly really simple.  From within VB.Net, I need to somehow save each page of a pdf document as a jpg.

    I know I can buy software that will convert a pdf to a jpg from a command line, and I know I can even use ghostscript to do this, but my theory is that I should be able to do this using the Interop.Acrobat or Acrodistxlib libraries.  Besides, our IT Manager would rather we use the tools we have.  However, I'm not able to find any documentation on these libraries - at least not without having to pay to be a member of Adobe's developer network, which I can't afford, and you can't even get their SDK without being a member.

   So, all I know to do at this point is add the Acrobat references to my VB.Net project, along with System.IO to help me navigate and retrieve the name of the PDF file I need to convert.   I'm clueless as to what to do next.   Any truly relevant sample would be greatly appreciated.  Even pointing me to some really good documentation that specifically discusses these libraries would probably help.  Alternatively, if there's just a simple way to call Acrobat or distiller from a command line and tell it to save the source pdf file as a jpg, that would be great too.

Thanks!
Karen Grube
klgrube@yahoo.com

Avatar of zupi5
zupi5

hi,
take  a look at http://www.ujihara.jp/iTextdotNET/
SOLUTION
Avatar of heintalus
heintalus

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of klgrube

ASKER

Hi!

  Thanks everyone so much!

   Andy, your answer did do exactly what I asked, but the resulting jpg was really poor resolution.  As one of the comments in CodeProject discusses, that's because of the way the clipboard and the drawing object and the monitor screen resolution all work together whe ncreating the bitmap.  That's fine for a thumbnail to be displayed on a web page, but not for saving a printable version of a document.  But, because the solution did work, I'm awarding you some of the points here.

   The solution I ended up using, though, was the one from khkremer.  After just a little reading through the two resources in the second post, I came up with this really simple solution.  It ended up taking only a few lines of code!  After adding the Acrobat Reference to the VB Project (we're using Acrobat 6) and adding a button to the form, I added the following code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim gApp As Acrobat.CAcroApp
        Dim gPDDoc As Acrobat.CAcroPDDoc
        Dim jso As Object

        gApp = CreateObject("AcroExch.App")
        gPDDoc = CreateObject("AcroExch.PDDoc")
        If gPDDoc.Open("C:\.pdf") Then
            jso = gPDDoc.GetJSObject
            jso.SaveAs("c:\test.jpg", "com.adobe.acrobat.jpeg")
        End If
        gPDDoc.Close()

End Sub

  Oh, one small note:  the pdf file I worked with had two pages, but you'll notice that I didn't have to say anything in code about having to save it as different jpg's.  The 'SaveAs' method was smart enough to save the files as "test_Page_1.jpg" and "test_Page_2.jpg"  And the output was perfect!

  I love it when an answer is this simple! Feel free to take a look at the reference materials in the answer above for more details on the Acrobat and javascript objects.

   My project is due tomorrow morning!  Now, I think I can actually finish it!  Thanks again/

Avatar of klgrube

ASKER

Hi!  

  I'm sorry. I erased part of the file name in the sample code when I posted it above.  I left out the name of the pdf file.  Here's the corrected version.


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim gApp As Acrobat.CAcroApp
        Dim gPDDoc As Acrobat.CAcroPDDoc
        Dim jso As Object

        gApp = CreateObject("AcroExch.App")
        gPDDoc = CreateObject("AcroExch.PDDoc")
        If gPDDoc.Open("C:\test.pdf") Then
            jso = gPDDoc.GetJSObject
            jso.SaveAs("c:\test.jpg", "com.adobe.acrobat.jpeg")
        End If
        gPDDoc.Close()

End Sub


Thanks again!
Karen
System.Exception was unhandled
  Message=Cannot create ActiveX component.
  Source=Microsoft.VisualBasic
  StackTrace:
       at Microsoft.VisualBasic.Interaction.CreateObject(String ProgId, String ServerName)
       at Davetet.Form1.Button4_Click(Object sender, EventArgs e) in C:\Users\TCKEYUPOGLU\documents\visual studio 2010\Projects\Davetet\Davetet\Form1.vb:line 306
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at Davetet.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
       at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
       at System.Activator.CreateInstance(ActivationContext activationContext)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
       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()
  InnerException: