USING VB6 or COMMANDLINE , EXTRACTING 1 OR MORE PAGES FROM A PDF FILE AND SAVE SEPARATE OR PRINT

I have one large PDF file containing invoices.  
Where knowing 1 or more page numbers,  how to extract these 1 or more pages in VB6 or some script or commandline option
to a separate PDF file , or print them out
BIAPROAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
kbireckiConnect With a Mentor Commented:
I use the free utility PDFTK.


pdftk.exe InputFile.pdf cat 2-5 output NewFile.pdf

Open in new window

0
 
Ariful AmbiaHead of ITCommented:
There is no effective way to do this. you may find some PDF cutter type software in the web through which you can try to separate the invoices.
0
 
Neil RussellTechnical Development LeadCommented:
Oh yes you can!!

Have a look here, its simple but NOT cheap.

http://www.verypdf.com/pdfprint/pdf-print-cmd.html
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
aikimarkCommented:
I'm going to use PDFSAM with a VB project for one of my clients.
http://www.pdfsam.org/
0
 
BIAPROAuthor Commented:
when downloading the source, my Nod32 antivirus
breaks off the download , stating that it contains
a type off:   win32/installcore.c  thread.
0
 
aikimarkCommented:
You may want to get a second AV opinion.

It is possible that they have recently acquired an infection.  Look around to see if there are some other sites, like CNet or SourceForge, that might have a PDFSAM download.
0
 
kbireckiCommented:
I realized later I left out the info to run this from VB6.

You could use a Shell or Shell Execute command to directly run this statement, but VB6 will continue processing and if it expects the extracted pages to exist, they likely won't by the time your VB6 app tries to use it, simply because PDFTK (or any tool) will take longer to extract the page(s).  Shell and ShellExecute don't wait for the command to finish.  So I use ShellAndWait (code below) that I found online.  This will cause VB6 to wait for completion of execution and still allow for a maximum amount of time for the external process to complete to avoid an infinite loop.  I execute the command like this:

Dim strInputFile as string     'Assume full path
Dim strOutputFile as string    'Assume full path
Dim strPDFTK as string     'Assume full path to PDFTK
Dim strQ as string            'Holds double quotes to enclose full paths potentially with spaces
Dim strPageRange as string
dim intDelayInSeconds as integer

strQ = """"
strInputFile = strQ & "C:\You Path\Your File.pdf" & strQ
strOutputFile = strQ & "C:\You Path\Your Results.pdf" & strQ
strPDFTK = strQ & "C:\Program Files\PDFTK\pdftk.exe" & strQ
strPageRange = "2-5"
intMaxDelayInSeconds = 2

ShellAndWait strPDFTK & " " & strInputFile & " cat " & strPageRange  & " output " & strOutputFile, _
                         vbNormalFocus, _
                         intMaxDelayInSeconds 

Open in new window


and the ShellAndWait routine:

'******************************************
'Source: http://www.vbforums.com/showthread.php?t=505172
'(Alt Source with more error handling and details: http://www.cpearson.com/excel/ShellAndWait.aspx)
'
'Execute Shell command and wait for result, but allow other processes to continue in app via DoEvents.
'
'Example:
'Private Sub Form_Load()
'  Me.Show
'  ShellAndWait "Notepad.exe", , 3
'  Me.Caption = "done"
'End Sub
'
Option Explicit

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess _
    As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle _
    As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Public Sub ShellAndWait(ByVal program_name As String, _
                         Optional ByVal window_style As VbAppWinStyle = vbNormalFocus, _
                         Optional ByVal max_wait_seconds As Long = 0)
    Dim lngProcessId As Long
    Dim lngProcessHandle As Long
    Dim datStartTime As Date
    Dim lngCursor As Long
    Const WAIT_TIMEOUT = &H102
    Const SYNCHRONIZE As Long = &H100000
    Const INFINITE As Long = &HFFFFFFFF

    lngCursor = Screen.MousePointer
    Screen.MousePointer = vbHourglass
    ' Start the program.
    On Error GoTo ShellError
    lngProcessId = Shell(program_name, window_style)
    On Error GoTo 0
    
    DoEvents

    ' Wait for the program to finish.
    ' Get the process handle.
    lngProcessHandle = OpenProcess(SYNCHRONIZE, 0, lngProcessId)
    If lngProcessHandle <> 0 Then
        datStartTime = Now
        Do
          If WaitForSingleObject(lngProcessHandle, 250) <> WAIT_TIMEOUT Then
            Exit Do
          End If
          DoEvents
          If max_wait_seconds > 0 Then
            If DateDiff("s", datStartTime, Now) > max_wait_seconds Then Exit Do
          End If
        Loop
        CloseHandle lngProcessHandle
    End If
    Screen.MousePointer = lngCursor
    
    Exit Sub
    
ShellError:
End Sub

Open in new window

0
 
BIAPROAuthor Commented:
works great !! just for euro 24.00  good deal  thanks
0
 
BIAPROAuthor Commented:
perfect solution!
0
 
kbireckiCommented:
Glad it works for you. Regarding your reference to €24.00, do you mean for the book?  After I found this utility and discovered how useful it is, I ordered the book myself just to support the author.  It's awesome that he put that utility online for free!
0
 
BIAPROAuthor Commented:
well i got a click for version to use with more then one location, so i did my tribute to him
programmers need to drink too, right ?
0
All Courses

From novice to tech pro — start learning today.