views:

667

answers:

4

I have been looking online for some time now, but I still haven't figured out how to print a PDF file in Delphi without showing the document itself, or a print dialog. I just want to open a file without showing it, and print it to the default printer.

I'm trying to print a batch of PDF documents, and there is no need for user interference.

+1  A: 

You can try using QPDF -- it is a PDF handling library, that has bindings for Delphi.

Kornel Kisielewicz
+6  A: 

There are some different possibilities to print PDFs... it depends whether you can require Adobe Reader to be installed (I don't know if you want to distribute your tool or just use it yourself).

1) It is possible to load the ActiveX control of Adobe Reader and use it for printing

pdfFile.src := 'filename.pdf'; 
pdfFile.LoadFile('filename.pdf'); 
pdfFile.print;

2) You can print PDFs with Adobe Reader itself (could be done with FoxIt too)

ShellExecute(0, 'open', 'acrord32', PChar('/p /h ' + FileName), nil, SW_HIDE);

3) You could also use Ghostview and Ghostprint

ShellExecute(Handle, 'open', 'gsprint.exe', PChar('"' + filename + '"'), '', SW_HIDE);

4) Or you could use a third party library... There are some available, but not all of them are free

Mef
Thx! I don't think I can use the shell solutions. The program I'm working on is a custom-made ERP and the printing has to be done on client-side. I suppose the first option does require the user to have Adobe Reader installed, too? The third party libraries are very useful too, but only the trials are for free of course :) I'll have to talk to my boss, but we're on a tight budget ;)
Liezzzje
For the ActiveX solution Adobe Reader woul be required as well, yes. But what are your concerns about the Shellexecute solution? It would probably be the cheapest method...
Mef
A: 

Here are a bunch of routines I have written in my libary. If you pass a pdf file as parameter to PrintUsingShell it should print if a Acrobat reader program has been installed (might work with other pdf-software too if they registered themselfs in the registry).

  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;

Note: please try these out on your Delphi version and Operating system (I have developed them under Delphi 7 and used them under Windows XP).

If you want native printing (without Acrobat reader installed - but who hasn't installed Acrobat Reader these days?) you might consider the following component set: Pdft print components from WpCubed.

Edelcom