Mittwoch, 2. März 2011

Printing pdf from your app

My dear readers!

I've got a user call having to do with printing from a self made application by using the installed pdf-reader (try Foxit... the best for me!).
I've tried some time using the Keybd_Event-syntax from my Delphi/Pascal for virtual key-activations.
If you're using Delphi or Free Pascal you can put the code below directly into a button-event (OnClick) of your application. Other programming-languages will offer a pretty similar syntax. With the sleep-property you can do some experiments.

procedure TForm1.Button3Click(Sender: TObject);
begin
// At the uses-part don't forget the ShellAPI ;-)

// Show/open the pdf-document ...
ShellExecute(hinstance,'open',PChar('c:\temp\test.pdf'),nil,nil,SW_NORMAL);

sleep(2000); // sleep/wait for 2 seconds

// Virtual Keys [Strg] + [P] to open the print dialog ...
Keybd_Event(VK_CONTROL,0,0,0);
Keybd_Event(Ord('P'),MapVirtualKey(Ord('P'), 0),0,0);
Keybd_Event(Ord('P'),MapVirtualKey(Ord('P'), 0),KEYEVENTF_KEYUP,0);
Keybd_Event(VK_CONTROL,0,KEYEVENTF_KEYUP,0);

// Virtual key [ENTER] to start printout ...
Keybd_Event(VK_RETURN,1,0,0);
Keybd_Event(VK_RETURN, 1, KEYEVENTF_KEYUP, 0);

sleep(2000); // sleep/wait for 2 seconds

// Virtual keys [Alt] + [F4] to close the active reader-window ...
Keybd_Event(VK_MENU,0,0,0);
Keybd_Event(VK_F4,0,0,0);
Keybd_Event(VK_F4,0,KEYEVENTF_KEYUP,0);
Keybd_Event(VK_MENU,0,KEYEVENTF_KEYUP,0);

end;