2014-03-05 36 views
0

我正在旋轉我的輪子。我有一個使用vb.net的asp.net網站。我通過Intranet SharePoint頁面鏈接到此ASP.net站點。我有一些代碼可以從SSRS報告中生成PDF文件。現在在代碼的相同行中,我想打印PDF文件。我發現這個做,但沒有運氣得到它的工作:On Button點擊從服務器打印PDF

''''PRINT REPORT 
    ''get default printer 
    Dim oPS As New System.Drawing.Printing.PrinterSettings 
    Dim defaultprintername As String = "" 
    Try 
     'Set my print ---- defaultprintername = "Dell 3330dn Laser Printer XL" 
     defaultprintername = oPS.PrinterName 
    Catch ex As System.Exception 
     defaultprintername = "" 
     SendAllLabel.Text = "ERROR - Could not set printer" 
    Finally 
     oPS = Nothing 
    End Try 

    Dim pathToExecutable As String = "AcroRd32.exe" 
    Dim starter As New ProcessStartInfo(pathToExecutable, "/t " + strReportOutput + " " + defaultprintername + "") 
    Dim Process As New Process() 
    Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden 
    Process.StartInfo.UseShellExecute = True 
    Process.StartInfo.Verb = "print" 
    Process.StartInfo.CreateNoWindow = True 
    Process.StartInfo = starter 
    ''WILL NOT START BUT NO EXCEPTION 
    Try 
     Process.Start() 
     Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden 
    Catch ex As System.Exception 
     SendAllLabel.Text = "ERROR - Could not set print job." 
    End Try 

    System.Threading.Thread.Sleep(30000) 

    Process.CloseMainWindow() 
    Dim iLoop As Int16 = 0 
    'check the process has exited or not 
    If Process.HasExited = False Then 
     Try 
      Process.Kill() 
     Catch ex As Exception 
      SendAllLabel.Text = "ERROR - Could not stop process." 
     End Try 
     'if not then loop for 100 time to try and close the process'with 10 seconds delay 
     While Not Process.HasExited 
      System.Threading.Thread.Sleep(10000) 
      Process.CloseMainWindow() 
      iLoop = CShort(iLoop + 1) 

      If iLoop >= 100 Then 


       Try 
        Process.Kill() 
       Catch ex As Exception 
        SendAllLabel.Text = "ERROR - Could not stop process." 
       End Try 
       Exit While 
      End If 
     End While 
    End If 

    Process.Close() 
    Process.Dispose() 
    Process = Nothing 
    starter = Nothing 

需要採取哪些措施來得到這個工作?我看到很多引用此代碼的網站,但沒有超出它的範圍。我需要授予權限嗎?如果是這樣,我該怎麼做?該服務器正在使用Adobe Reader 10.0,大多數用戶正在使用Reader 11.0。

請和謝謝你

+0

你一定要明白,如果你告訴它在ASP代碼打印,你問它來打印你的服務器上,而不是客戶端計算機,對吧? – Steve

+0

您可以在新的網頁瀏覽器中打開PDF,這會給您打印選項。這足夠嗎? – voddy

+0

服務器上的默認打印機是打印機,它需要去客戶端。 – Schwimms

回答

0

你需要生成PDF,然後流下來的瀏覽器,這裏是我很久以前寫的。

Response.Clear() 
    Response.ClearHeaders() 
    Response.ClearContent() 

    Response.ContentType = "application/pdf" 

    ' IE & Acrobat seam to require "content-disposition" header being in the response. If you don't add it, the doc still works most of the time, but not always. 
    ' this makes a new window appear: 
    ' you can replace the filename with the actual filename 
    Response.AddHeader("content-disposition", "attachment; filename=something.PDF") 

    ' Create a new memory stream that will hold the pdf output 
    Dim memStream As New System.IO.MemoryStream() 

    ' Export the report to PDF: 
    ' put the pdf file into the memStream 
    ' the strpath variable should hold the full path and filename of the pdf file 
    ' for example dim strPath = "\\hw.net\files\2014dt140305.105459.pdf" 
    Dim bData As Byte() 
    Dim br As BinaryReader = New BinaryReader(System.IO.File.OpenRead(strPath)) 
    bData = br.ReadBytes(br.BaseStream.Length) 
    Dim memStream As MemoryStream = New MemoryStream(bData, 0, bData.Length) 

    ' Write the PDF stream out 
    Response.BinaryWrite(memStream.ToArray()) 

    ' Send all buffered content to the client 
    Response.End() 
+0

該文件位於公司股票驅動器上。如果是這樣的文件,我將如何打開該文件:\\ Hw \ files \ 2014dt140305.105459.pdf – Schwimms

+0

對不起,這不是完整的鏈接... \\ hw.net \ files \ 2014dt140305.105459.pdf – Schwimms

+0

I已經編輯了我的答案,包括將pdf讀入內存流對象的代碼 –