Consulting

Results 1 to 13 of 13

Thread: How to split a PDF into pages using VBA

  1. #1

    How to split a PDF into pages using VBA

    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.
    Kind regards

    Lee Nash

    http://www.NashProjects.com


  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.

  3. #3
    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
    Kind regards

    Lee Nash

    http://www.NashProjects.com


  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    I did not test this. The concept would be similar. There is a newer version of pdfsam at http://pdfsam.org.

    See the help in the pdfsam installed pdf help file for command line parameters.
    [vba]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[/vba] 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.

  5. #5
    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.
    Kind regards

    Lee Nash

    http://www.NashProjects.com


  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.

  7. #7
    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
    Kind regards

    Lee Nash

    http://www.NashProjects.com


  8. #8
    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
    Kind regards

    Lee Nash

    http://www.NashProjects.com


  9. #9

  10. #10
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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:
    [VBA]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[/VBA]

  11. #11
    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....
    Kind regards

    Lee Nash

    http://www.NashProjects.com


  12. #12
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.
    [VBA]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[/VBA]

    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:
    [VBA]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
    [/VBA]

  13. #13
    Great thank you so much for your help to this point.

    Really appreciate it Kenneth.
    Kind regards

    Lee Nash

    http://www.NashProjects.com


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •