Delphi:如何在不显示PDF的情况下打印PDF?

时间:2010-01-13 08:18:17

标签: delphi pdf printing

我一直在网上看了一段时间,但我还是没有弄清楚如何在Delphi中打印PDF文件而不显示文档本身或打印对话框。我只想在不显示文件的情况下打开文件,然后将其打印到默认打印机。

我正在尝试打印一批PDF文档,不需要用户干预。

4 个答案:

答案 0 :(得分:17)

打印PDF有一些不同的可能性......这取决于您是否可以安装Adobe Reader(我不知道您是想分发您的工具还是只是自己使用它)。

1)可以加载Adobe Reader的ActiveX控件并将其用于打印

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

2)您可以使用Adobe Reader本身打印PDF(也可以使用FoxIt完成)

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

3)您也可以使用Ghostview和Ghostprint

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

4)或者你可以使用第三方图书馆...有一些可用,但不是所有都是免费的

答案 1 :(得分:7)

以下是我在自己的图书馆中编写的一系列例程。如果您将pdf文件作为参数传递给 PrintUsingShell ,则应该在安装了Acrobat reader程序时打印(如果他们在注册表中注册了自己的pdf软件,也可以使用其他pdf软件。)

  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;

注意:请在您的Delphi版本和操作系统上尝试这些(我在Delphi 7下开发它们并在Windows XP下使用它们。)

如果您想要原生打印(未安装Acrobat Reader,但最近没有安装过Acrobat Reader?),您可以考虑使用以下组件:Pdft print components from WpCubed

<强>更新

根据要求,我从我的库中添加了执行功能......

答案 2 :(得分:1)

有一个名为'AutoPrint'的shareware-prog将文件夹中的所有文件发送到打印机, 花费35美元。 (如果你没有很多顾客)。

否则,如果有人可以修复一些同样的代码,那将会很酷。

答案 3 :(得分:1)

将PDF打印到打印机而不尝试使用Delphi的Adobe Reader可以使用Debenu Quick PDF Library完成,它支持从4到XE8的所有版本的Delphi。以编程方式打印PDF而不先预览PDF的示例代码:

build.gradle

使用customer printer functions也可以使用更高级的打印选项。它不是免费的SDK,但它可以完全按照您的意愿完成。