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)''''''''''''''''
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.
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
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)''''''''''''''''
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.
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