Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need a script to print the 2nd page of 1300 pdf documents

Status
Not open for further replies.

Starrett

MIS
Feb 23, 2005
14
US
Hello everyone,

I'm no programmer but I do create batch files quite often for things, although I have never used VBscript.
I have no idea how to do what I'm asking for with a batch file, so I figured VBscript might be more capable.

I have 1300 pdf files that I need only the second page of each printed.
If anyone has a way to do this, could you please share it.
Thanks
 
Starrett,

This is how it can be done.
Code:
pdffilespec="d:\test\abc.pdf"    'input[1] pdf filepath
pfrom=2    'input[2] print from page# 
pto=3    'input[3] print to page# inclusive

set oie=createobject("internetexplorer.application")
with oie
    .navigate "about:blank"
    .visible=false
    .document.write "<object id='PDF' name='PDF' classid='clsid:CA8A9780-280D-11CF-A24D-444553540000'>"
    .refresh
    while .readystate<>4 : wscript.sleep 100 : wend
    .document.all("PDF").loadfile pdffilespec
    while .readystate<>4 : wscript.sleep 100 : wend
    .document.all("PDF").printpages pfrom,pto
    while .readystate<>4 : wscript.sleep 100 : wend
    .quit
end with
set oie=nothing
regards - tsuji
 
Thankyou tsuji,

I'm a little unclear how to input the name of each of my files into this script.
I can create a batch to do this:
for /F "tokens=1" %%a in (pdflist.txt) do tsujiscript.vbs %%a

I don't where in your script I can input the next pdf filename to print the second page of.
Could you elaborate a bit more for me?

PS: If I want only the second page, would I make the pfrom= and pto= the number 2 only; Print from page 2 to page 2.

Thanks
 
Starrett,

[1] Certainly, pto can be set to 2 and your understanding is correct, printing only page 2.

[2] If you do that, you can modify the in-take of pdffilespec as follows with minimal validation.
[tt]
if wscript.arguments.count=0 then
wscript.quit
elseif not createobject("scripting.filesystemobject").fileexists(wscript.arguments(0)) then
wscript.quit
else
pdffilespec=wscript.arguments(0)
end if
[/tt]
And you commandline should be read as:
[tt]
for /F "tokens=1" %%a in (pdflist.txt) do cscript //b //nologo printpdf.vbs %%a
[/tt]
Whereas your pdflist.txt is line delimited data of fully qualified path pointing to each .pdf file.

As this is yet minimal error-trapping, so be careful in the preparation of pdflist.txt.

You can sure do it pure vbs way rather than hybrid way. But, anything works will do.

[3] First step to test, is take one file and modify the script presented previous as such with pto=2, save it to .vbs and double-click see if it works.

- tsuji
 
Tsuji,

I tested the original script.
Changed pdffilespec="C:\pdfs\test.pdf"
Test.pdf was a 10 page pdf file.
I ran the script and had to allow the script to run with MS Anti-Spyware Beta and then had to allow Acrobat 7.0 to let a script run it.
Nothing happened.

Does this script use Internet Explorer in any way?
I'm running XP with SP2, I will disable the POPup stopper in IE that was added by SP2 and try again.
 
Starrett,

The pdf.ocx may have a warning interactive baring the functionality. I see there might be problem ahead.
Generally speaking, this approach is memory intensive and very exposed to memeory leak. I studied it with interest on the principle behind it. But, we have a more efficient way to handle large number of selective printing. I had looked into a component PDF995, or something like that, which might serve better. But, I do not have time to look for my notes. Maybe I will take a look during weekend. Sorry, maybe it looks likely a unsuccessful approach. There must be some component to help you. My look into this issue is biased to acadamic rather than practical. Maybe later...

- tsuji
 
Starrett,

I think I can give you more precise advice in this issue. The reason I backoff soon after giving you previous advice is that I test it on acrobat 6.0 (not even 7.0) and it gave some inconsistent behavior. I am sure I have the script going just fine months ago if not year. After testing and looking into the tlb of pdf.ocx, I come to the conclusion version 6 and 7 and even version 5 no longer function properly with printpages. It was working without a warning popup back at 4.0 if I recalled.

It is not unsurmountable, but it is less elegant.

As to the script, I can make a version with some due care at various locations, not to say the previous one does not work. Only this time, I have to use sendkeys to get the warning popup out of the way. In the meantime, I can make the script cosmetically more complete.

Code:
pdffilespec="d:\test\abc.pdf"    'input[1] pdf filepath
pfrom=2    'input[2] print from page# 
pto=2    'input[3] print to page# inclusive

shtml="<html><body>" & vbcrlf
shtml=shtml & "<object id=""PDF"" name=""PDF"" classid=""clsid:CA8A9780-280D-11CF-A24D-444553540000""" 
shtml=shtml & " border=""0"" width=""550"" height=""450"">" & vbcrlf
shtml=shtml & "<param name=""src"" value=""" & pdffilespec & """>" & vbcrlf
shtml=shtml & "</object></body></html>"

set wshshell=createobject("wscript.shell")
set oie=createobject("internetexplorer.application")
with oie
    .navigate "about:blank"
    while .readystate<>4 : wscript.sleep 100 : wend
    .visible=false
    '.visible=true    'alternative for visual inspection
    .document.open
    .document.write (shtml)    'must enclose shtml by parentheses
    .document.close
    wscript.sleep 100
    .document.all("PDF").loadfile pdffilespec
    wscript.sleep 100
    .document.all("PDF").printpages pfrom,pto
    wscript.sleep 100
    wshshell.appactivate "Acrobat Reader"
    wscript.sleep 50
    wshshell.sendkeys "{tab}"
    wscript.sleep 50
    wshshell.sendkeys "{enter}"
    wscript.sleep 50
    .quit
end with
set oie=nothing
set wshshell=nothing
After you test it with one file working without user's intervention satisfactory, you can then feed the script with variable pdffilespec successive at the .loadfile line within the "with oie-end with" block.

- tsuji
 
Tsuji,

I tested this script on a PDF named abc.pdf with 7 pages and received the following error:
Windows Script Host
---------------------------
Script: C:\Documents and Settings\Starrett\Desktop\Printpdfpg2.vbs
Line: 22
Char: 5
Error: Unspecified error
Code: 80004005
Source: (null)
---------------------------

I appreciate your time involved working on this.
Thanks
 
Tsuji,

Sorry, I forgot to edit the pdffilespec to change to my desktop path.
When I correct the pdffilespec, I receive this from Adobe:
Adobe Reader
------------------
File does not begin with '%PDF-'.

Any idea's.
 
Starrett,

I suggest install again acrobat reader if necessary. [1] Use the line .visible=true rather than -false (by switching the comment). Also [2] comment out the .quit line. This allows you to see what happens on the screen.

If you click out Reader and load the file from the gui, does it load, your pdf file?

- tsuji
 
Tsuji,

If I open IE and then go to File/Open and choose the abc.pdf on my desktop it opens the pdf up within IE.

I changed the comment lines as you suggested:
When I run the script.
1)Script opens IE to the About:Blank page.
2)The error is displayed
Adobe Reader
------------------
File does not begin with '%PDF-'.
3)Clicked 'OK' to the error.
4)Script loads the PDF very fast - But the Full Adobe Tool Bar is not loaded - It then removes the document from the Acrobat interface and then gives me the same message as in number 2 above.
5)Clicked 'OK' on the error and the Acrobat interface is removed from IE - About:Blank is all that is shown.
 
What if, instead of opening the ie, directly open the Reader and load it from there?
- tsuji
 
That also works.
Started Acrobat Reader
Selected File/Open/abc.pdf
PDF opens.

Is there a way to script printing the second page of these PDF's with just the Acrobat program by itself?
Was this test just to verify the pdf wasn't corrupted?
 
Are you cut-and-paste the script as presented or have you some elements added? IE version 6? Also try some other source's pdf? (I can hardly believe the behavior you describe!?)
- tsuji
 
Tsuji,

I have cut and pasted the script into a file printpdfpg2.vbs.
I modified the pdffilespec and commented what you told me to. See below.

pdffilespec="C:\Documents and Settings\Starrett\Desktop\abc.pdf" 'input[1] pdf filepath
pfrom=2 'input[2] print from page#
pto=2 'input[3] print to page# inclusive

shtml="<html><body>" & vbcrlf
shtml=shtml & "<object id=""PDF"" name=""PDF"" classid=""clsid:CA8A9780-280D-11CF-A24D-444553540000"""
shtml=shtml & " border=""0"" width=""550"" height=""450"">" & vbcrlf
shtml=shtml & "<param name=""src"" value=""" & pdffilespec & """>" & vbcrlf
shtml=shtml & "</object></body></html>"

set wshshell=createobject("wscript.shell")
set oie=createobject("internetexplorer.application")
with oie
.navigate "about:blank"
while .readystate<>4 : wscript.sleep 100 : wend
'.visible=false
.visible=true 'alternative for visual inspection
.document.open
.document.write (shtml) 'must enclose shtml by parentheses
.document.close
wscript.sleep 100
.document.all("PDF").loadfile pdffilespec
wscript.sleep 100
.document.all("PDF").printpages pfrom,pto
wscript.sleep 100
wshshell.appactivate "Acrobat Reader"
wscript.sleep 50
wshshell.sendkeys "{tab}"
wscript.sleep 50
wshshell.sendkeys "{enter}"
wscript.sleep 50
'.quit
end with
set oie=nothing
set wshshell=nothing

I'm running WindowsXP with SP2
McAfee AV
Adobe Acrobat 7.0
Windows Anti-Spyware Beta 1.0

The only restriction I get is from MS Anti-Spy when I first start the script - I have to allow it the first time it is run.
 
Starrett,

Thanks for the feedback. Try change this line.
[tt]
shtml=shtml & "<param name=""src"" value=[red]""""[/red]>" & vbcrlf
[/tt]
as I use .loadfile below.

- tsuji
 
Tsuji,

After I changed the shtml=shtml as you suggested:
I run the script.
1)Script opens IE to the About:Blank page.
2)The error is displayed
Adobe Reader
------------------
File does not begin with '%PDF-'.
3)Clicked 'OK' to the error.
4)Script loads the PDF very fast - But the Full Adobe Tool Bar is not loaded - It then removes the document from the Acrobat interface and then gives me the same message as in number 2 above.
5)Clicked 'OK' on the error and the Acrobat interface is removed from IE - About:Blank is all that is shown.

This is the error I was previously getting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top