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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.PDD oc")
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/
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.PDD
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/
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.PDD oc")
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
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.PDD
If gPDDoc.Open("C:\test.pdf")
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.VisualBas ic
StackTrace:
at Microsoft.VisualBasic.Inte raction.Cr eateObject (String ProgId, String ServerName)
at Davetet.Form1.Button4_Clic k(Object sender, EventArgs e) in C:\Users\TCKEYUPOGLU\docum ents\visua l studio 2010\Projects\Davetet\Dave tet\Form1. vb:line 306
at System.Windows.Forms.Contr ol.OnClick (EventArgs e)
at System.Windows.Forms.Butto n.OnClick( EventArgs e)
at System.Windows.Forms.Butto n.OnMouseU p(MouseEve ntArgs mevent)
at System.Windows.Forms.Contr ol.WmMouse Up(Message & m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Contr ol.WndProc (Message& m)
at System.Windows.Forms.Butto nBase.WndP roc(Messag e& m)
at System.Windows.Forms.Butto n.WndProc( Message& m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.OnMessa ge(Message & m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.WndProc (Message& m)
at System.Windows.Forms.Nativ eWindow.De buggableCa llback(Int Ptr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Unsaf eNativeMet hods.Dispa tchMessage W(MSG& msg)
at System.Windows.Forms.Appli cation.Com ponentMana ger.System .Windows.F orms.Unsaf eNativeMet hods.IMsoC omponentMa nager.FPus hMessageLo op(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Appli cation.Thr eadContext .RunMessag eLoopInner (Int32 reason, ApplicationContext context)
at System.Windows.Forms.Appli cation.Thr eadContext .RunMessag eLoop(Int3 2 reason, ApplicationContext context)
at System.Windows.Forms.Appli cation.Run (Applicati onContext context)
at Microsoft.VisualBasic.Appl icationSer vices.Wind owsFormsAp plicationB ase.OnRun( )
at Microsoft.VisualBasic.Appl icationSer vices.Wind owsFormsAp plicationB ase.DoAppl icationMod el()
at Microsoft.VisualBasic.Appl icationSer vices.Wind owsFormsAp plicationB ase.Run(St ring[] commandLine)
at Davetet.My.MyApplication.M ain(String [] Args) in 17d14f5c-a337-4978-8281-53 493378c107 1.vb:line 81
at System.AppDomain._nExecute Assembly(R untimeAsse mbly assembly, String[] args)
at System.AppDomain.nExecuteA ssembly(Ru ntimeAssem bly assembly, String[] args)
at System.Runtime.Hosting.Man ifestRunne r.Run(Bool ean checkAptModel)
at System.Runtime.Hosting.Man ifestRunne r.ExecuteA sAssembly( )
at System.Runtime.Hosting.App licationAc tivator.Cr eateInstan ce(Activat ionContext activationContext, String[] activationCustomData)
at System.Runtime.Hosting.App licationAc tivator.Cr eateInstan ce(Activat ionContext activationContext)
at System.Activator.CreateIns tance(Acti vationCont ext activationContext)
at Microsoft.VisualStudio.Hos tingProces s.HostProc .RunUsersA ssemblyDeb ugInZone()
at System.Threading.ThreadHel per.Thread Start_Cont ext(Object state)
at System.Threading.Execution Context.Ru n(Executio nContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.Execution Context.Ru n(Executio nContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHel per.Thread Start()
InnerException:
Message=Cannot create ActiveX component.
Source=Microsoft.VisualBas
StackTrace:
at Microsoft.VisualBasic.Inte
at Davetet.Form1.Button4_Clic
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Nativ
at System.Windows.Forms.Unsaf
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at Microsoft.VisualBasic.Appl
at Microsoft.VisualBasic.Appl
at Microsoft.VisualBasic.Appl
at Davetet.My.MyApplication.M
at System.AppDomain._nExecute
at System.AppDomain.nExecuteA
at System.Runtime.Hosting.Man
at System.Runtime.Hosting.Man
at System.Runtime.Hosting.App
at System.Runtime.Hosting.App
at System.Activator.CreateIns
at Microsoft.VisualStudio.Hos
at System.Threading.ThreadHel
at System.Threading.Execution
at System.Threading.Execution
at System.Threading.ThreadHel
InnerException:
take a look at http://www.ujihara.jp/iTextdotNET/