2008-10-24 162 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; 
相關問題