Page tree

Welcome to FreeSoftwareServers Confluence Wiki

Skip to end of metadata
Go to start of metadata

How I Handle Printing Specific Page(s): Note: I only use this when needing to print specific pages as it opens UI and uses SENDKEYS = unreliable, but only/best option for specific pages. But when I print the whole doc, I don't use VBS so therefor this method REQUIRES the argument for pages in the format of "2" or "1-2". Accepts OPTIONAL DUPLEXSENDKEYS as arg to setup Duplex Printing!

Note: I tried setting "orientation", but adobe doesn't honor anything if "Auto" is selected and there is no way to determine what is selected (you can just keep looping pressing right)  Update, you can set it via "page setup", but I'm done improving my Batch/VBS Hybring stuff, please see POWERSHELL PRINT for better solutions.

I also tried setting greyscale, but adobe remembers last setting, so again, no way to reset to default. If you really need/want, I'd recommend setting up additional printers w/ new default settings and see how that goes, but I have enough options for my needs (probably more, I went a little overkill on my print projects...)

https://stackoverflow.com/questions/38698411/print-a-specific-pdf-page-using-command-line#:%7E:text=No.,displays%20the%20Print%20dialog%20box

VBScript/Batch Hybrid:

<!-- : Begin batch script
@ECHO OFF
CLS

cscript //nologo "%~f0?.wsf" "PROGPATH=C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" "FILEPATH=C:\tmp\test.pdf" "DUPLEXSENDKEYS=TrUe" "PAGERANGE=" "COPIES=" "DUPLEXFLIPON=Short" //job:PDF
exit /b
PAUSE
----- Begin wsf script --->
<package>
  <job id="PDF">
    <script language="VBScript">
        Dim ObjArgs
        Set ObjArgs = wscript.arguments

		Dim ObjShell
        Set ObjShell = WScript.CreateObject ("WScript.shell"

        Dim DUPLEXSENDKEYS
        DUPLEXSENDKEYS= Null 
        Dim DUPLEXFLIPON
        DUPLEXFLIPON = Null
        Dim SINGLEORDOUBLESIDED
        SINGLEORDOUBLESIDED = Null
        Dim PAGERANGE          
        PAGERANGE = Null
        Dim COPIES
        COPIES = Null

        Dim ObjDictionary
        Set ObjDictionary = CreateObject("Scripting.Dictionary")
        For I = 0 to ObjArgs.Count-1
        ObjDictionary.CompareMode = vbTextCompare
        ObjDictionary.Add Split(ObjArgs(I), "=")(0), Split(ObjArgs(I), "=")(1)
        Next

        )
        cmd = ObjDictionary("PROGPATH") & " /P " & ObjDictionary("FILEPATH")
        Objshell.exec(cmd)
        WScript.Sleep 5000

        'Double Sided
        ObjShell.AppActivate "Print"
        ObjShell.SENDKEYS "{TAB}", TRUE ' More "Bring Focus ...DUPLEXSENDKEYS = Hacky
        WScript.Sleep 500
        SINGLEORDOUBLESIDED = StrComp(ObjDictionary("SINGLEORDOUBLESIDED"), "Double", 1)
        If SINGLEORDOUBLESIDED = 0 Then
         DUPLEXSENDKEYS = StrComp(ObjDictionary("DUPLEXSENDKEYS"), "FALSE", 1)
         If DUPLEXSENDKEYS = 0 Then 
          WScript.Sleep 500       
          ObjShell.SENDKEYS "%b", TRUE ' Alt + b [Print On Both Sides]
          WScript.Sleep 500
          ObjShell.SENDKEYS "%b", TRUE ' Alt + b [Print On Both Sides]
          WScript.Sleep 500  
          ObjShell.SENDKEYS "%l", TRUE '  Alt + l {Long Edge]
          DUPLEXFLIPON = StrComp(ObjDictionary("DUPLEXFLIPON"), "SHORT", 1)
           If DUPLEXFLIPON = 0 Then
            ObjShell.SENDKEYS "{RIGHT}", TRUE ' Short Edge
           End If
          End If
         DUPLEXSENDKEYS = StrComp(ObjDictionary("DUPLEXSENDKEYS"), "TRUE", 1)
         If DUPLEXSENDKEYS = 0 Then 
          WScript.Sleep 500       
          ObjShell.SENDKEYS "%b", TRUE ' Alt + b [Print On Both Sides]
          WScript.Sleep 500 
          ObjShell.SENDKEYS "%l", TRUE '  Alt + l {Long Edge]
          DUPLEXFLIPON = StrComp(ObjDictionary("DUPLEXFLIPON"), "SHORT", 1)
           If DUPLEXFLIPON = 0 Then
            ObjShell.SENDKEYS "{RIGHT}", TRUE ' Short Edge
           End If
         End If
        End If

        If Not ObjDictionary("PAGERANGE") = "" Then
         ObjShell.AppActivate "Print"
         ObjShell.SENDKEYS "{TAB}", TRUE ' More "Bring Focus ...DUPLEXSENDKEYS = Hacky
         WScript.Sleep 1000
         ObjShell.SENDKEYS "%g", TRUE ' Alt + g [Pages]
         ObjShell.SENDKEYS "{TAB}", TRUE ' Input Pages
         ObjShell.SENDKEYS ObjDictionary("PAGERANGE"), TRUE ' Start and End Page
         WScript.Sleep 500
        End If

        If Not ObjDictionary("COPIES") = "" Then
         ObjShell.AppActivate "Print"
         ObjShell.SENDKEYS "{TAB}", TRUE ' More "Bring Focus ...DUPLEXSENDKEYS = Hacky
         WScript.Sleep 1000
         ObjShell.SENDKEYS "%c", TRUE ' Alt + g [COPIES]
         ObjShell.SENDKEYS ObjDictionary("COPIES"), TRUE ' Start and End Page
         WScript.Sleep 500
        End If
                 
        'Done Setup
        WScript.Sleep 100
        ObjShell.SENDKEYS "{ENTER}{ENTER}", TRUE ' Print!
        WScript.Sleep 100
    
        Dim FPath
        TEMP=ObjShell.ExpandEnvironmentStrings("%TEMP%")
        FPath = TEMP & "\jobrunning.log"

        Dim ObjFS
        Set ObjFS = CreateObject("Scripting.FileSystemObject")

        IF ObjFS.FileExists(FPath) THEN
            ObjFS.DeleteFile FPath
        END IF

        Set ObjShell = Nothing
        Set FPath = Nothing
        Set ObjFS = Nothing
        Set ObjArgs = Nothing
        Set ObjDictionary = Nothing
        Set ObjShell = Nothing
    </script>
</package>

Test:

wscript  "C:\tmp\pdf_print_sendkeys.vbs" "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" "C:\tmp\test.pdf" "2-3"

You can use `print.exe` but I wouldn't advise it.

@ECHO OFF
CLS

set PRINTCMD=print
set SERVER=
set SHARE=

%PRINTCMD% /D:"\\%SERVER%\%SHARE%" %~1

I found that "print" caused issues with some PDF's like when I split a pdf into specific pages via "Microsoft Print to PDF". So, I now use "AcroRd32.exe" which is apparently unsupported/undocumented (except on SO of course!), but works much better.

https://stackoverflow.com/questions/619158/adobe-reader-command-line-reference

https://www.robvanderwoude.com/commandlineswitches.php#Acrobat

The following switches are available:

  • /n - Launch a new instance of Reader even if one is already open
  • /s - Don't show the splash screen
  • /o - Don't show the open file dialog
  • /h - Open as a minimized window
  • /p <filename> - Open and go straight to the print dialog
  • /t <filename> <printername> <drivername> <portname> - Print the file the specified printer. 'Note <printername> <drivername> <portname> are all optional

  • /a - "Open Parameter" --> https://www.evermap.com/AutoBookmark/Manual/OpenParameters.htm

  • Syntax definitionDescription
    nameddest=nameSpecifies a named destination in the PDF.
    For example:
    mydoc.pdf#nameddest=chapter1
    where chapter1 is a named destionation
    page=pagenumSpecifies a numbered page in the document, using an integer value. The document’s first page has a pagenum value of 1.
    comment=commentIDSpecifies a comment on a given page in the PDF. Use the page command before this command; for example: #page=1&comment=452fde0e-fd22-457c-84aa-2cf5bed5a349
    zoom=scale
    zoom=scale,left,top
    Sets the zoom and scroll factors, using float or integer values. For example, a scale value of 100 indicates a zoom value of 100%. Scroll values left and top are in a coordinate system where 0,0 represents the top left corner of the visible page, regardless of document rotation.
    view=Fit
    view=FitH
    view=FitH,top
    view=FitV
    view=FitV,left
    view=FitB
    view=FitBH
    view=FitBH,top
    view=FitBV
    view=FitBV,left
    Set the view of the displayed page, using the keyword values defined in the PDF language specification. See the PDF Reference. Scroll values left and top are floats or integers in a coordinate system where 0,0 represents the top left corner of the visible page, regardless of document rotation. Use the page command before this command.
    NOTE:Not supported on the command line.
    viewrect=left,top,wd,htSets the view rectangle using float or integer values in a coordinate system where 0,0 represents the top left corner of the visible page, regardless of document rotation. Use the page command before this command. NOTE:Not supported on the command line.
    pagemode=bookmarks
    pagemode=thumbs
    pagemode=none (default)
    Displays bookmarks or thumbnails.
    scrollbar=1|0Turns scrollbars on or off.
    search=wordListOpens the Search UI and performs a search for the specified word list in the document. Matching words are highlighted in the document. The words must be enclosed in quotes and separated by spaces; for example: #search=”word1 word2”
    statusbar=1|0Turns the status bar on or off.
    messages=1|0Turns the document message bar on or off.
    navpanes=1|0Turns the navigation panes and tabs on or off.
    highlight=lt,rt,top,btmHighlights a specified rectangle on the displayed page. (Use the page command before this command.) The rectangle values are integers in a coordinate system where 0,0 represents the top left corner of the visible page, regardless of document rotation.
    help=index|contents|searchUsed only when opening a file from the command line or an Apple Event. Opens the document in the Help window, with the specified navigation panel selected.
    fdf=URLSpecifies an FDF file to be used to populate form fields in the PDF file being opened.
    For example: #fdf=http://example.org/doc.fdf
    NOTE:The fdf parameter should be specifed last in the URL.
    collab=settingSets the collaboration store to be used to supply and store comments for the document. This overrides the default comment server for the review or the default preference. setting is of the form store_type@location, where valid values for store_type are:
    ?DAVFDF (webDAV)
    ?FSFDF (Network store)
    ?DB (ADBC) For example: #collab=DAVFDF@http://review_server/Collab/user1 For more information, see Acrobat Online Collaboration: Administration and Setup.
    @ECHO OFF
    CLS
    
    set PRINTCMD=start "" "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" /N /H /T
    set SERVER=
    set SHARE=
    
    ECHO PRINTING "%~1" ON \\%SERVER%\%SHARE%
    ::PAUSE
    ::ECHO %PRINTCMD% "%~1" "\\%SERVER%\%SHARE%" 
    ::PAUSE
    %PRINTCMD% "%~1" "\\%SERVER%\%SHARE%" 
    TIMEOUT 5

    Calling:

    SET PDF_SS_P=call "Print_PDF_Single_Sided.cmd"
    SET PWD=\\SERVER\SHARE\Folder Space\
    SET F1="%PWD%HR Package.docx"
    %PDF_SS_P% %F1%

    Notes:

  • Adobe hangs after printing, so you must use "Start" for each print job which starts it's own thread or script will hang
  • Start looks for window title first, so you must use "" after start. Eg START "" "C:\Adobe"...
  • To select Portrait/Landscape, open Adobe, Print → Orientation → Auto → Print. Now next time it should automatically use the best/correct orientation. You can use Adobe pro to set orientation, but this works fine in my testing.
  • I couldn't get Wildcards in Filenames to work, haven't debugged yet.
  • No labels