Extract Pdf pages by range with PdfTk server

prati

Board Regular
Joined
Jan 25, 2021
Messages
51
Office Version
  1. 2019
Platform
  1. Windows
Hey,

I'm using Pdftk server - a command line tool for multiple tasks like merge pdf, split, rotate and more - PDFtk Server
I have a question regarding extracting pdf pages.
I created a macro VBA that extract pages from combined pdf and then creates new 2 files as follows:
1- Is my article.pdf, only the article Itself.
2- Is the annexes of the atricle.
The problem - in order to extract pages to 2 files, I have to input the range manually, and writing the exact range in numbers

----------------------------------------------------------------------------------------------------------------------------------------
Sub Extracting_Pdf_Using_PdfTk_Server
Dim wShell As Object 'WshShell
Dim Wexec As Object 'WshExec
Dim Wsh As Object 'WshShell
Dim WhatSplit As String

On Error Resume Next

Set Wsh = CreateObject("WScript.Shell") 'New WshShell
Set fso = CreateObject("Scripting.FileSystemObject") 'New FileSystemObject
Set wShell = CreateObject("WScript.Shell") 'New WshShell
If FileExists("C:\Temp\Article.pdf") Then
WhatSplit = "C:\Temp\Article.pdf"
End If

''''''''''''''Now I have to check how many pages the article include, (range IV6 in my excel sheet includes the number of pages)''''''''''''''''

1624225827074.png


I'm doing the task manually and this is not smart way at all.

If Range("IV6") = "0" Then
Wsh.Run ("cmd /c PDFtk " & WhatSplit & " cat 1-end output C:\Temp\Main_Document.pdf"""), 0, True

ElseIf Range("IV6") = "1" Then
Wsh.Run ("cmd /c PDFtk " & WhatSplit & " cat 1-1 output C:\Temp\Main_Document.pdf"""), 0, True
Wsh.Run ("cmd /c PDFtk " & WhatSplit & " cat 2-end output C:\Temp\Annexes.pdf"""), 0, True

ElseIf Range("IV6") = "2" Then
Wsh.Run ("cmd /c PDFtk " & WhatSplit & " cat 1-2 output C:\Temp\Main_Document.pdf"""), 0, True
Wsh.Run ("cmd /c PDFtk " & WhatSplit & " cat 3-end output C:\Temp\Annexes.pdf"""), 0, True

ElseIf Range("IV6") = "3" Then
Wsh.Run ("cmd /c PDFtk " & WhatSplit & " cat 1-3 output C:\Temp\Main_Document.pdf"""), 0, True
Wsh.Run ("cmd /c PDFtk " & WhatSplit & " cat 4-end output C:\Temp\Annexes.pdf"""), 0, True

ElseIf Range("IV6") = "4" Then
Wsh.Run ("cmd /c PDFtk " & WhatSplit & " cat 1-4 output C:\Temp\Main_Document.pdf"""), 0, True
Wsh.Run ("cmd /c PDFtk " & WhatSplit & " cat 5-end output C:\Temp\Annexes.pdf"""), 0, True

ElseIf Range("IV6") = "5" Then
Wsh.Run ("cmd /c PDFtk " & WhatSplit & " cat 1-5 output C:\Temp\Main_Document.pdf"""), 0, True
Wsh.Run ("cmd /c PDFtk " & WhatSplit & " cat 6-end output C:\Temp\Annexes.pdf"""), 0, True

''''''''And so on, keep checking how many pages the article has....writing ton of lines of code.... until 5000'''''''''''
....
....
....
ElseIf Range("IV6") = "5000" Then
Wsh.Run ("cmd /c PDFtk " & WhatSplit & " cat 1-5000 output C:\Temp\Main_Document.pdf"""), 0, True
Wsh.Run ("cmd /c PDFtk " & WhatSplit & " cat 5001-end output C:\Temp\Annexes.pdf"""), 0, True

End if

End sub()

The above Code works fine but It contain tons of code. It forces me to write the range in nubers 1-200, 1-300 and so on.....
I have tried alternative way of extracting pdf but i got a problem

---------------------------------------------------------------------------------------------------------------------------------------------

Sub Extracting_Pdf_Using_PdfTk_Server
Dim wShell As Object 'WshShell
Dim Wexec As Object 'WshExec
Dim Wsh As Object 'WshShell
Dim WhatSplit As String
Dim j as integer (also tried as string)

On Error Resume Next

Set Wsh = CreateObject("WScript.Shell") 'New WshShell
Set fso = CreateObject("Scripting.FileSystemObject") 'New FileSystemObject
Set wShell = CreateObject("WScript.Shell") 'New WshShell
If FileExists("C:\Temp\Article.pdf") Then
WhatSplit = "C:\Temp\Article.pdf"
End If

j = Range("IV6")
Wsh.Run ("cmd /c PDFtk " & WhatSplit & " cat 1-j output C:\Temp\Main_Document.pdf"""), 0, True 'Trying to extract the article -doesn't work
Wsh.Run ("cmd /c PDFtk " & WhatSplit & " cat (j+1)-end output C:\Temp\Annexes.pdf"""), 0, True 'Trying to extract the annexes -also doesn't work

End sub()


----------------------------------------------------------------------------------------------------------------------------------------------

As I mentioned above, also the alternative way doesn't work, Despite the fact that j integer get the correct number of pages from the excel sheet.

I have checked in locals and j indeed get value of 80 (pages). The value is correct.

1624227099514.png


Maybe PdfTk server can't extract pages when the range is an integer or variable or string.
Maybe I must put the range manually like

Wsh.Run ("cmd /c PDFtk " & WhatSplit & " cat 1-5000 output C:\Temp\Main_Document.pdf"""), 0, True
Wsh.Run ("cmd /c PDFtk " & WhatSplit & " cat 5001-end output C:\Temp\Annexes.pdf"""), 0, True


I hope there is a clever way for extracting pages with PdfTk server, a way that will use the information of cell "IV6" that include the number of pages
 

Attachments

  • 1624225805124.png
    1624225805124.png
    4.2 KB · Views: 8

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi,
May be this:
VBA Code:
Sub Extracting_Pdf_Using_PdfTk_Server()
 
  Const InputPdf = "C:\Temp\Article.pdf"
  Const OutputPdf1 = "C:\Temp\Main_Document.pdf"
  Const OutputPdf2 = "C:\Temp\Annexes.pdf"
 
  Dim Wsh As Object
  Dim j As Long
  Dim s As String
 
  Set Wsh = CreateObject("WScript.Shell")
   
  If Dir(InputPdf) = "" Then
    MsgBox InputPdf & " not found", vbExclamation, "Exit"
    Exit Sub
  End If
 
  j = Range("IV6").Value
 
  If j = 0 Then
  
    s = "cmd /c PDFtk " & InputPdf & " cat 1-end output " & OutputPdf1
    'Debug.Print 1, s
    Wsh.Run s, 0, True
 
  Else
   
    s = "cmd /c PDFtk " & InputPdf & " cat 1-" & j & " output " & OutputPdf1
    'Debug.Print 2, s
    Wsh.Run s, 0, True
   
    s = "cmd /c PDFtk " & InputPdf & " cat " & j + 1 & "-end output " & OutputPdf2
    'Debug.Print 3, s
    Wsh.Run s, 0, True
   
  End If

End Sub
 
Upvote 0
Solution
Hi,
May be this:
VBA Code:
Sub Extracting_Pdf_Using_PdfTk_Server()
 
  Const InputPdf = "C:\Temp\Article.pdf"
  Const OutputPdf1 = "C:\Temp\Main_Document.pdf"
  Const OutputPdf2 = "C:\Temp\Annexes.pdf"
 
  Dim Wsh As Object
  Dim j As Long
  Dim s As String
 
  Set Wsh = CreateObject("WScript.Shell")
  
  If Dir(InputPdf) = "" Then
    MsgBox InputPdf & " not found", vbExclamation, "Exit"
    Exit Sub
  End If
 
  j = Range("IV6").Value
 
  If j = 0 Then
 
    s = "cmd /c PDFtk " & InputPdf & " cat 1-end output " & OutputPdf1
    'Debug.Print 1, s
    Wsh.Run s, 0, True
 
  Else
  
    s = "cmd /c PDFtk " & InputPdf & " cat 1-" & j & " output " & OutputPdf1
    'Debug.Print 2, s
    Wsh.Run s, 0, True
  
    s = "cmd /c PDFtk " & InputPdf & " cat " & j + 1 & "-end output " & OutputPdf2
    'Debug.Print 3, s
    Wsh.Run s, 0, True
  
  End If

End Sub
Wow
Exactly what I have searched for.
thanks a million

I have so much to learn
I thought the only way is to input range numbers
 
Upvote 0
Glad it helped, thank you for the feedback!
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top