Voici un tas de routines je l'ai écrit dans mon libary. Si vous passez un fichier pdf en paramètre à PrintUsingShell, il devrait imprimer si un programme Acrobat Reader a été installé (il pourrait aussi fonctionner avec d'autres logiciels pdf s'ils s'enregistraient eux-mêmes dans le registre).
PrintUsingShell(x);
procedure PrintUsingShell(psFileName :string);
var s : string;
i : integer;
begin
if not FileExists(psFileName)
then
Exit;
s := FindShellPrintCmd(ExtractFileExt(psFileName));
i := Pos('%1',s);
if i > 0
then begin
System.Delete(s,i,2);
System.Insert(psFileName,s,i);
Execute(s);
end;
end;
function FindShellCmd(psExtension:string;psCmd:string): string;
var r : TRegistry;
sName : string;
begin
psExtension := Trim(psExtension);
if psExtension = ''
then begin
Result := '';
Exit;
end;
psCmd := Trim(psCmd);
if psCmd = ''
then
psCmd := 'OPEN'
else
psCmd := UpperCase(psCmd);
if psExtension[1] <> '.'
then
psExtension := '.' + psExtension;
r := TRegistry.Create(KEY_READ);
try
r.RootKey := HKEY_LOCAL_MACHINE;
r.OpenKeyReadOnly('software\classes\' + psExtension);
sName := r.ReadString('');
r.CloseKey();
r.OpenKeyReadOnly('software\classes\' + sName + '\Shell\' + psCmd + '\Command');
Result := r.ReadString('');
r.CloseKey();
finally
FreeAndNil(r);
end;
end;
function FindShellOpenCmd(psExtension:string):string;
begin
Result := FindShellCmd(psExtension,'OPEN');
end;
function FindShellPrintCmd(psExtension:string):string;
begin
Result := FindShellCmd(psExtension,'PRINT');
end;
{$ifdef windows}
function LocalExecute(psExeName:string ; wait:boolean ; how:word):word;
var i : integer;
prog,parm:string;
msg:TMsg;
rc : word;
begin
i := pos(psExeName,' ');
if i = 0
then begin
prog := psExeName;
parm := '';
end
else begin
prog := copy(psExeName,1,i-1);
parm := copy(psExeName,i+1,255);
end;
if pos(prog,'.') <> 0
then
prog := prog + '.exe';
psExeName := prog + ' ' + parm + #0;
rc := WinExec(@psExeName[1] , how);
if wait
then begin
if (rc > 32)
then begin
repeat
if PeekMessage(Msg,0,0,0,PM_REMOVE)
then begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
until (GetModuleUsage(rc) = 0)
end;
end;
end; { LocalExecute }
{$endif}
{$ifdef win32}
function LocalExecute32(FileName:String; Wait:boolean; Visibility : integer;
lWaitFor:Cardinal=INFINITE):integer;
var zAppName:array[0..512] of char;
zCurDir:array[0..255] of char;
WorkDir:String;
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin
StrPCopy(zAppName,FileName);
GetDir(0,WorkDir);
StrPCopy(zCurDir,WorkDir);
FillChar(StartupInfo,Sizeof(StartupInfo),#0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := Visibility;
if not CreateProcess(nil,
zAppName, { pointer to command line string }
nil, { pointer to process security attributes }
nil, { pointer to thread security attributes }
false, { handle inheritance flag }
CREATE_NEW_CONSOLE or { creation flags }
NORMAL_PRIORITY_CLASS,
nil, { pointer to new environment block }
nil, { pointer to current directory name }
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) { pointer to PROCESS_INF }
then Result := -1
else begin
if Wait
then begin
Result := WaitforSingleObject(ProcessInfo.hProcess,lWaitFor);
GetExitCodeProcess(ProcessInfo.hProcess,LongWord(Result));
end;
end;
end;
{$endif}
function Execute(psExeName:string):integer;
begin
{$ifdef windows} result := LocalExecute(psExeName, false , SW_SHOW); {$endif}
{$ifdef win32} result := LocalExecute32(psExeName, false , SW_SHOW); {$endif}
end;
Note: s'il vous plaît les essayer sur votre version Delphi et système d'exploitation (je les ai développé sous Delphi 7 et les ont utilisés sous Windows XP).
Si vous voulez une impression native (sans Acrobat Reader - mais qui n'a pas installé Acrobat Reader ces jours-ci?), Vous pouvez utiliser le jeu de composants suivant: Pdft print components from WpCubed.
MISE À JOUR
Sur demande j'ai ajouté la fonction Execute de ma bibliothèque ...
Thx! Je ne pense pas pouvoir utiliser les solutions shell. Le programme sur lequel je travaille est un ERP sur mesure et l'impression doit être effectuée côté client. Je suppose que la première option nécessite l'installation d'Adobe Reader? Les bibliothèques tierces sont très utiles aussi, mais seulement les essais sont gratuits bien sûr :) Je vais devoir parler à mon patron, mais nous avons un budget serré;) – Liezzzje
Pour la solution ActiveX, Adobe Reader sera nécessaire aussi, oui. Mais quelles sont vos préoccupations à propos de la solution Shellexecute? Ce serait probablement la méthode la moins chère ... – Leo
Excellent! N'oubliez pas d'ajouter ShellApi, Windows à votre clause uses. – JoeGalind