Jump to content

PDF Printer


Vakari
 Share

Recommended Posts

Hello everyone. I am not a programmer by any means, and have recently stumbled upon AutoIT and am eager to learn as much as I can about it.

I'm in the process of making a few programs to help my pathetic life at work run a little smoother, and could use some input.

One of the few helpful programs I have created is designed to print PDFs for me. One of my jobs is to process information that all gets returned as PDF documents. Sometimes I only use 3 to 4 PDFs, other times I may have 150+. These all need to be printed and distributed as necessary.

I'm sure my code is quote inefficient, as I have never programmed anything before. I seem to get into jams with loops on occasion, and I think this might be the underlying cause for my problem. The systems at work are fairly decent, 3GHz Pentium 4s, 512MB unknown RAM. By the time this program gets to the 30th PDF, the fans in the system start picking up speed, and are soon running full blast. The performance monitor in task manager shows capped CPU usage at all times when it is running. I can't seem to figure out a way to minimize the use of the CPU when it isn't needed, for example, while waiting for the "Progress" bar in Acrobat to go away.

If anyone feels like looking at the code below, feel free to point out any rediculous things I threw in there. Once compiled, the executable is placed in the folder containing PDFs I need to print. Once executed, there is spare time to get a drink, etc. while it does its thing.

Note that one of the major functions I care about is skipping page 1 of each PDF. Its a summary page that nobody needs. And not that anyone would want it, I don't really care what you do with the code below. I'm sure anything that could possibly be done to it would make it better.

#Include <File.au3>
#Include <Array.au3>

HotKeySet("{Esc}", "Close")                                     ; Pressing 'Esc' closes the program

Opt("WinTitleMatchMode", 2)

$search = FileFindFirstFile("*.pdf")                            ; Look for PDF files only

If $search = -1 Then                                            ; Warns the user if there are no PDFs available and exits if so
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

$FileList = _FileListToArray(@WorkingDir, "*.pdf", 1)
$FileCount = $FileList[0]
ProgressOn("Status", "Printing PDFs", "0 / " & $FileCount, @DesktopWidth - 306, 0, 16)
$Increase = 100 / $FileCount
$FileNum = 1

While 1
    $file = FileFindNextFile($search)                           ; Finds the next file in the folder 
    If @error Then ExitLoop                                     ; Exits if FileFindNextFile does not find another file
    ProgressSet($FileNum * $Increase, $FileNum & " / " & $FileCount); Sets Progress bar status
    $FileNum = $FileNum + 1                                     ; Increases Progress bar for the next time it is adjusted
    ShellExecute($file)                                         ; Opens the file with default program
    WinWaitActive($file)                                        ; Waits for the application window to become active. Necessary?
    ControlSend($file, "", "", "^p")                            ; Presses Ctrl+P to open the print dialog
    Opt("WinTitleMatchMode", 1)                                 ; Window name searches must match from the start of the title
    WinWaitActive("Print")                                      ; Waits for print dialog window to become active
    ControlClick("Print", "", "Button6")                        ; Clicks 'Pages' radio button
    $pages = ControlGetText("Print", "", "Edit1")               ; $pages becomes the default range of pages printed. ex. 1 - 45 if PDF has 45 pages
    $pages = StringRight($pages, StringLen($pages) -1)          ; $pages becomes " - 45" if PDF has 45 pages
    ControlSetText("Print", "", "Edit1", "2" & $pages)          ; Replaces default pages so page 1 is skipped. ex. "2 - 45"
    ControlClick("Print", "", "Button16")                       ; Clicks 'Print' button
    WinWaitActive("Progress")                                   ; Waits for the 'Progress' window to become active. Necessary?
    While WinExists("Progress")                                 ; Waits for the 'Progress' window to go away
        Sleep(50)
    WEnd
    Opt("WinTitleMatchMode", 2)                                 ; Reset window match mode
    WinClose($file)                                             ; Closes the application and file
WEnd
ProgressOff()                                                   ; Turns of the progress bar
FileClose($search)                                              ; Closes the search file

Func Close()
    Exit
EndFunc
Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...