PDA

View Full Version : How to split a PDF into pages using VBA



NashProjects
03-25-2009, 07:29 AM
Hi all,

Im stumped with a problem.

I have a PDF file and I need to split this into individual pages... I would normally do this manually but I have thousands per month and so I want to automate this process.

I am an expert in excel vba but this has baffled me as i cant seem to find any activex dll's which have this functionality?

I would appreciate if anyone can help me with this problem!!!

Thanks in advance guys.

Kenneth Hobs
03-25-2009, 08:46 AM
Welcome to the forum!

Doing it from a pdf file, you will need to use adobe methods from their sdk or a 3rd party program like pdfsam that does this easily. You can probably send a command line switch to pdfsam. I used it or another free 3rd partry program to do similar things to merge pdfs. Use VBA's Shell() to run the 3rd party program.

NashProjects
03-25-2009, 08:55 AM
Thanks Kenneth, your suggestion is really appreciated!

I dont suppose i can trouble you for a simple idiots guide to using something like pdfsam and calling it from shell

obviously i know it'l lbe something like

Shell pdfsam.exe

but in which order would i put the options/filenames etc?

Thanks in advance

Kenneth Hobs
03-25-2009, 10:00 AM
I did not test this. The concept would be similar. There is a newer version of pdfsam at http://pdfsam.org. (http://pdfsam.org/)

See the help in the pdfsam installed pdf help file for command line parameters.
pdfsam="java -jar c:\myfiles\pdfsam0.7\lib\pdfsam-console-0.7.0.jar"
pdfFiles="-f e:\masoud\1a.pdf -f e:\masoud\2.pdf -f e:\masoud\3.pdf -f e:\masoud\4.pdf -f e:\masoud\1b.pdf"
pdfOut="-o e:\masoud\1.pdf"
pdfsamStr=pdfsam & " " & pdfFiles & " " & pdfOut & " -overwrite concat"
Shell pdfsamStr, vbNormalFocus The main thing to do is to try it from the Run dialog, WIN+R, and once you get the string right, you can build it in vba. You may need to add quotes around file paths as part of the string to pass to Shell().

You may also need "cmd /c " as the first part of variable pdfsam to get Shell() to use it properly.

I used version 0.7 in this example. The latest stable version is 1.0.3 so use it and modify the pdfsam variable to use your path and your name for the latest version as applicable.

NashProjects
03-25-2009, 12:55 PM
Thank you very much Kenneth, I did take a look at the help files but they completely baffled me.

With your explanation here I hope to be able to work it out and get something to work.

I'll post whatever I have that works up here for others when im done.

Thanks for your help to get me started, I really appreciate it.

Kenneth Hobs
03-25-2009, 01:04 PM
Do you have a sample pdf that I could test? You could post the link or post it to a free shared site like 4shared.com. How would you split it?

If you have used command line switches, the help should help. If not, it will take more effort.

NashProjects
03-26-2009, 06:45 AM
Hi again Kenneth,

Thanks for your help so far...

this is the code im using

pdfsam = "cmd /c java -jar C:\Documents and Settings\Nashl\Desktop\pdfsam-1.0.3-out\lib\pdfsam-console-1.1.5e.jar"
pdfFiles = "-f F:\Temp\temp.pdf"
pdfOut = "-o F:\Temp\"
pdfsamStr = pdfsam & " -p 1 " & pdfFiles & " " & pdfOut & " -s SPLIT"
Shell pdfsamStr, vbNormalFocus


I it keeps popping up saying that it cannot find the zip file or something...
Im not sure what its talking about?

Regards

NashProjects
03-26-2009, 06:52 AM
As you can see im getting to grips with the way pdfsam works but its just the zip file thats baffled me.... let me quickly get a pic of it... i'll upload it then post the link on here

NashProjects
03-26-2009, 06:53 AM
http://www.nashprojects.com/Nash.jpg

Kenneth Hobs
03-26-2009, 07:22 AM
I will test this a bit more but notice what I said about the quotes. Your "Document and Setttings" has space characters so you need something like:
Sub Test()
Dim pdfsam As String, pdfFiles As String, pdfsamStr As String
Dim q As String
q = """"
pdfsam = "cmd /c java -jar " & q & _
"C:\Documents and Settings\Nashl\Desktop\pdfsam-1.0.3-out\lib\" & _
"pdfsam-console-1.1.5e.jar" & q
pdfFiles = "-f F:\Temp\temp.pdf"
pdfOut = "-o F:\Temp\"
pdfsamStr = pdfsam & " -p 1 " & pdfFiles & " " & pdfOut & " -s SPLIT"
Shell pdfsamStr, vbNormalFocus
End Sub

NashProjects
03-26-2009, 07:48 AM
Hi Kenneth,

Yes i initally tested:

pdfsam = "cmd /c java -jar "&(chr(34))&"C:\Documents and Settings\Nashl\Desktop\pdfsam-1.0.3-out\lib\pdfsam-console-1.1.5e.jar"&(chr(34))&""
pdfFiles = "-f "&(chr(34))&"F:\Temp\temp.pdf"&(chr(34))&""
pdfOut = "-o "&(chr(34))&"F:\Temp\"&(chr(34))&""
pdfsamStr = pdfsam & " -p 1 " & pdfFiles & " " & pdfOut & " -s SPLIT"
Shell pdfsamStr, vbNormalFocus

But that really didnt work!! it gave some sort of weird error i didnt understand at all....

Kenneth Hobs
03-26-2009, 09:12 AM
I think it is time to post to pdfsam's forum. Post the string for the playing the jar file or the bat file. You can set q="" to not include the quotes.
e.g.
Sub Test()
Dim pdfsam As String, pdfFiles As String, pdfsamStr As String
Dim q As String
q = """"
pdfsam = "cmd /c java -jar " & q & _
"C:\Documents and Settings\Nashl\Desktop\pdfsam-1.0.3-out\lib\" & _
"pdfsam-console-1.1.5e.jar" & q
pdfFiles = "-f F:\Temp\temp.pdf"
pdfOut = "-o F:\Temp\"
pdfsamStr = pdfsam & " -p 1 " & pdfFiles & " " & pdfOut & " -s SPLIT"
debug.print pdfsamStr
Shell pdfsamStr, vbNormalFocus
End Sub

Copy the Immediate window's contents to the pdfsam forum. You might find a clue on the forum but I did not. They had one thread where an xml file is set which is what the bat file does.

I got the zip error when I tried using the bat file. Here it is:
Sub TestBat()
Dim pdfsam As String, pdfFiles As String, pdfsamStr As String
Dim pdfOut As String, q As String
q = ""
pdfsam = "cmd /k java -jar " & q & _
"C:\MyFiles\pdfsam\1.0.3\bin\" & _
"run-console.bat" & q
pdfFiles = "-f " & q & "C:\MyFiles\pdfsam\1.0.3\doc\pdfsam-1.0.0-tutorial.pdf" & q
pdfOut = "-o " & q & "c:\temp" & "\" & q
pdfsamStr = pdfsam & " " & pdfFiles & " " & pdfOut & " -p ken.pdf -s BURST split"
Debug.Print pdfsamStr
Shell pdfsamStr, vbNormalFocus
End Sub

NashProjects
03-26-2009, 10:35 AM
Great thank you so much for your help to this point.

Really appreciate it Kenneth.