Delphi :如何打印PDF而不显示它?

roejwanj  于 2022-11-04  发布在  其他
关注(0)|答案(5)|浏览(487)

我已经在网上找了一段时间了,但是我仍然没有弄清楚如何在 Delphi 中打印一个PDF文件而不显示文档本身,或者打印对话框。我只想打开一个文件而不显示它,然后打印到默认的打印机。
我正在尝试打印一批PDF文档,不需要用户干预。

gjmwrych

gjmwrych1#

有一些不同的可能性打印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)或者您可以使用第三方库...有一些可用的,但不是所有的都是免费的

avwztpqn

avwztpqn2#

下面是我在我的库中编写的一系列例程。如果你将一个pdf文件作为参数传递给PrintUsingShell,如果安装了Acrobat阅读器程序,它应该会打印出来(如果其他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

更新

根据请求,我从我的库中添加了Execute函数...

5cg8jx4n

5cg8jx4n3#

有一个共享软件程序叫'自动打印',发送所有文件夹中的文件到打印机,成本35美元。(如果你没有很多客户)。
否则,如果有人能修复一些做同样事情的代码,那就太酷了。

lp0sw83n

lp0sw83n4#

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

procedure TForm6.PrintDocumentClick(Sender: TObject);
var
iPrintOptions: Integer;
begin
  DPL := TDebenuPDFLibrary1115.Create;
  try
    UnlockResult := DPL.UnlockKey('...'); // Add trial license key here
    if UnlockResult = 1 then
      begin
          // Load your file
          DPL.LoadFromFile('test.pdf', '');

          // Configure print options
          iPrintOptions := DPL.PrintOptions(0, 0, 'Printing Sample');

          // Print the current document to the default printing
          // using the options as configured above
          DPL.PrintDocument(DPL.GetDefaultPrinterName(), 1, 1, iPrintOptions);
      end;
    finally
    DPL.Free;
  end;
end;

使用customer printer functions还可以获得更高级的打印选项。它不是一个免费的SDK,但它可以完全满足您的需要。

nxowjjhe

nxowjjhe5#

我来这里最初是因为我想做OP想要的,从 Delphi 应用程序打印。其他人建议了一个替代的解决方案,一个廉价的商业应用程序,称为自动打印。但前一个人给了很少的信息。所以,我谷歌了一下,找到了它。我本来想在另一个答案上添加一个评论,但我没有足够的分数来评论另一个答案。所以我在加上我自己的答案。
该产品被4-Tech-Engineering称为自动打印:
https://4-tech-engineering.com/
我和这家公司没有任何联系,我只是发现他们的解决方案比重写轮子更容易。它是免费的,标准版35美元,专业版85美元。它可以打印很多不同类型的文件,但我主要想PDF打印,当然它也能。

相关问题