2008-10-24 161 views
2

我正在使用VB.Net WinForms。我想致电Adobe Reader 9 ActiveX控件来打印一些PDF文件。我已经添加了ActiveX控件到VS工具箱(该dll是AcroPDF.dll时,COM名称“的Adobe PDF阅读器”。一些实验后,下面的代码工作。使用.NET Interop以编程方式在Adobe Reader 9中打印

Dim files As String() = Directory.GetFiles(TextBoxPath.Text, "*.pdf", SearchOption.TopDirectoryOnly) 

Using ActiveXPDF As New AxAcroPDFLib.AxAcroPDF 

    Me.Controls.Add(ActiveXPDF) 
    ActiveXPDF.Hide() 

    For Each filename As String In files 

     ActiveXPDF.LoadFile(filename) 
     ActiveXPDF.printAll() 

     'Begin Yukky Hack ' 


     Dim endTime As Date = DateAdd(DateInterval.Second, 20, Now) 
     Do While Now < endTime 
      My.Application.DoEvents() 
     Loop 

     'End Yuk ' 

    Next 

End Using 

没有育位,这将只打印一些PDFs,似乎End Using语句在它完成打印之前调用控件处理

因此,看起来printAll的调用是非阻塞的,但我找不到回调或状态属性我可以查询打印后台打印是否已完成,我缺少一个属性/方法,或者是否有更优雅的(并且响应更快)的工作?

回答

2

使用此方法打印多个文档不会像您发现的那样工作良好。

让它工作是非常棘手的,但这里是解决方案的一般描述。

我使用的System.Diagnostics.Process使用myProcess.StartInfo.Verb =“打印” 打印然后我检查打印机队列的状态和状况的两个步骤,以确保印刷是十分愿意能够打印下一个文件。使用WMI和ManagementObjectSearcher枚举打印机信息使用“选择*从Win32_Printer”。 逻辑是我试图在继续打印下一个之前看看假脱机是否已经开始。

请参阅http://msdn.microsoft.com/en-us/library/aa394363.aspx为Win32_Printer WMI类。

+2

+1但是,这几乎是可怕的黑客是我的“只是等待一个有点”黑客!我不知道为什么它是这样一个皮塔饼。我决定只是不打电话给Dispose,我想这更糟糕...... – 2009-02-01 15:22:18

0

我们最终使用Adobe的PDF验证程序进行自己的测试。为了做到这一点,我们必须实际启动杂技演员并使用SendInput以编程方式操作界面。

我会很感兴趣看看是否可以使用内部API代替。

-2

您可以使用此代码来显示任何具有相应软件的文件。

Sub Show_Document(ByVal FILENAME As String) 
    Dim p As Process = Nothing 
    Try 
     If My.Computer.FileSystem.FileExists(FILENAME) Then 
      p = Process.Start(FILENAME) 
      p.Dispose() 
     End If 

    Catch ex As Exception 

    Finally 

    End Try 

End Sub 
+1

问题是关于如何打印PDF,而不是打开它们。我知道他们可以在新的Adobe Reader实例中点击“打印”,但这是一个可用性的噩梦。另外,吞咽异常也是不好的做法。 – statenjason 2009-12-07 20:01:37

1

我曾在德尔福使用AcroPDF同样的问题..然后我想通了,当我使用一个消息“停止”的processo,AcroPDF开始打印。

所以我只是创建一个模态TForm,几秒钟后关闭自己。

var 
    formModal : TFormModal; 
begin 
    formModal := TFormModal.Create(self); 
    //PrintMethodHere 
    frmPecas.CarregarDocumentoParaImpressao(); 
    formModal.ShowModal; 
end; 

的TFormModal是这样,我只是插入到代表类似“打印”窗体上的加载图标。

unit FModal; 

interface 

uses 
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
    Dialogs, ExtCtrls, Animate, GIFCtrl; 

type 
    TFormModal = class(TForm) 
    Timer: TTimer; 
    imgGif: TRxGIFAnimator; 
    procedure TimerTimer(Sender: TObject); 
    procedure FormShow(Sender: TObject); 
    procedure FormClose(Sender: TObject; var Action: TCloseAction); 
    procedure FormCreate(Sender: TObject); 
    procedure FormKeyDown(Sender: TObject; var Key: Word; 
     Shift: TShiftState); 
    private 
    { Private declarations } 
    public 
    { Public declarations } 
    end; 

var 
    FormModal: TFormModal; 

implementation 

{$R *.dfm} 
// Author: Anderson Mello Date: 09-fev-2012 
// DEscription: Using TTimer after 5 seconds I close this form 
procedure TFormModal.TimerTimer(Sender: TObject); 
begin 
close; 
end; 

// Author: Anderson Mello Date: 09-fev-2012 
// Description: Enable the timer only when the form is shown 
procedure TFormModal.FormShow(Sender: TObject); 
begin 
Timer.Enabled := true; 
end; 

// Description: disable when close 
procedure TFormModal.FormClose(Sender: TObject; var Action: TCloseAction); 
begin 
Timer.Enabled := false; 
end; 

// Author: Anderson Mello Date: 09-fev-2012 
// Description: disable close button "X", so the user can't close 
procedure TFormModal.FormCreate(Sender: TObject); 
var 
    hSysMenu:HMENU; 
begin 
    hSysMenu:=GetSystemMenu(Self.Handle,False); 
    if hSysMenu <> 0 then begin 
    EnableMenuItem(hSysMenu,SC_CLOSE,MF_BYCOMMAND or MF_GRAYED); 
    DrawMenuBar(Self.Handle); 
    end; 
    KeyPreview:=True; 
end; 

// Author: Anderson Mello Date: 09-fev-2012 
// Description: disable shortcuts to close 
procedure TFormModal.FormKeyDown(Sender: TObject; var Key: Word; 
    Shift: TShiftState); 
begin 
    if (Key = VK_F4) and (ssAlt in Shift) then 
    Key:=0; 
end; 
相关问题