• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 10089
  • Last Modified:

How to convert PDF to JPG within VB.Net


   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.

Karen Grube

3 Solutions
take  a look at http://www.ujihara.jp/iTextdotNET/
Hi Karen

If you've got the full version of Acrobat then the example here http://www.codeproject.com/dotnet/pdfthumbnail.asp
should get you going.  The API's used are only available with the full version & are not included in the free reader.

Karl Heinz KremerCommented:
The codeproject.com link provided by Andy does work even with newer versions of Acrobat (the functions used are in the 6 and 7 SDK). Which version of Acrobat do you have?

The SDK is a collection of documentation, header files for plug-n programming and samples. You can get everything you need to program for Acrobat for free, but you won't get access to the sample projects, and some of the documentation is only available for paying SDK customers or ADN members.

All documents you need are available here: http://partners.adobe.com/public/developer/acrobat/sdk/index_doc.html
You are interested in the Acrobat IAC Reference (http://partners.adobe.com/public/developer/en/acrobat/sdk/pdf/iac/IACReference.pdf). You will not have access to the "IAC Overview" document, which explains how to use the IAC Reference, but it's not required to start programming with the reference document.

Look for the pad lock icons next to the documents, these are the ones you only get with the full SDK.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Karl Heinz KremerCommented:
If you have Acrobat 7, you can also use the JSObject (which you can get in VB.NET via the PDDoc object: PDDoc.GetJSObject()). WIth this JSObject, you have access to the JavaScript functions that are described in these documents: http://partners.adobe.com/public/developer/en/acrobat/sdk/AcroJS.pdf and http://partners.adobe.com/public/developer/en/acrobat/sdk/pdf/javascript/VBJavaScript.pdf). You want to use the Doc.saveAs() method, and specify that you want to save as JPEG. This way, you don't have to convert the bitmap data to JPEG yourself (as you would with the CodeProject.com method).
klgrubeAuthor Commented:

  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

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/

klgrubeAuthor Commented:

  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

End Sub

Thanks again!
System.Exception was unhandled
  Message=Cannot create ActiveX component.
       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()
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now