2011-10-24 17 views
0

我一直在尋找無處不在......我只能找到如何打印LocalReport,而不是ServerReport。如何從vb.net打印SSRS ServerReport

我不想打印對話或任何東西,我只是想要它打印。

我已經盡力打印第一頁,但我不知道如何獲得額外的頁面?

回答

1

該VB.Net(2005)類將打印SSRS 2003報告。我猜想可能能夠處理任何SSRS版本,因爲報告是作爲字節數組的數組傳遞的,但不幸的是,我仍然無法連接到(單獨渲染!)SSRS 2005或SSRS 2008服務來測試它。

您應該調用PrintReport函數,將Report字節(呈現爲IMAGE)和目標打印機名稱傳遞給它。

好了,下面的代碼(被警告,這是我第一次嘗試,並從樣本C代碼調整,不記得在「淨這裏):

Imports System 
Imports System.Drawing 
Imports System.Drawing.Imaging 
Imports System.Drawing.Printing 
Imports System.IO 
Imports System.Runtime.InteropServices 

Public Class clsReportPrinting 

Private byaPagesToPrint()() As Byte = Nothing, _ 
     m_oMetafile As Metafile = Nothing, _ 
     m_iNumberOfPages As Int32 = 0, _ 
     m_iCurrentPrintingPage As Int32 = 0, _ 
     m_iLastPrintingPage As Int32 = 0, _ 
     m_oCurrentPageStream As MemoryStream = Nothing, _ 
     m_oDelegate As Graphics.EnumerateMetafileProc = Nothing 

Public Function PrinterExists(ByVal PrinterName As String) As Boolean 

    'Returns TRUE if the named printer is amongst installed printers, FALSE otherwise 
    Try 

     PrinterName = PrinterName.Trim.ToUpper 

     For Each sPrinter As String In PrinterSettings.InstalledPrinters 

      'Printers may have UNC names, so we use .EndsWith to deal with \\Server\SharedPrinter 
      'if we pass "SharedPrinter" as PrinterName 
      If sPrinter.Trim.ToUpper.EndsWith(PrinterName) Then 
       Return True 
      End If 

     Next 

     Return False 

    Catch oEx As Exception 

     'Report error 
     glb_oLog.Log(oEx.ToString, clsLogger.enuSeverity.sev_01_Error) 
     Return False 

    End Try 

End Function 

Public Function MetafileCallback(ByVal oRecType As EmfPlusRecordType, _ 
            ByVal iFlags As Int32, _ 
            ByVal iDataSize As Int32, _ 
            ByVal dpData As IntPtr, _ 
            ByVal oCallbackData As PlayRecordCallback _ 
           ) As Boolean 

    Dim byaDataArray() As Byte = Nothing 

    If dpData <> IntPtr.Zero Then 

     'Copy unmanaged data to managed array for PlayRecord call 
     Array.Resize(Of Byte)(byaDataArray, iDataSize) 
     Marshal.Copy(dpData, byaDataArray, 0, iDataSize) 

    End If 

    'Play the record 
    m_oMetafile.PlayRecord(oRecType, iFlags, iDataSize, byaDataArray) 

    'Return value 
    Return True 

End Function 

Private Function MoveToPage(ByVal lPage As Int32) As Boolean 

    'Check current page does exist 
    If Me.byaPagesToPrint(m_iCurrentPrintingPage - 1) Is Nothing Then 
     Return False 
    End If 

    'Set current page stream to desired rendered page 
    m_oCurrentPageStream = New MemoryStream(Me.byaPagesToPrint(m_iCurrentPrintingPage - 1)) 
    'Set curernt stream position to its start 
    m_oCurrentPageStream.Position = 0 

    'Get rid of any former metafile 
    If Not m_oMetafile Is Nothing Then 
     m_oMetafile.Dispose() 
     m_oMetafile = Nothing 
    End If 

    'Set local metafile to page 
    m_oMetafile = New Metafile(m_oCurrentPageStream) 

    'Must always return TRUE 
    Return True 

End Function 

Public Sub pd_PrintPage(ByVal oSender As Object, _ 
         ByVal oEV As PrintPageEventArgs) 

    oEV.HasMorePages = False 

    If (m_iCurrentPrintingPage <= m_iLastPrintingPage) And _ 
     (MoveToPage(m_iCurrentPrintingPage)) Then 

     'Draw the page 
     DrawPage(oEV.Graphics) 

     'Point to next page 
     m_iCurrentPrintingPage += 1 

     'If there are more pages, flag so. 
     oEV.HasMorePages = (m_iCurrentPrintingPage <= m_iLastPrintingPage) 

    End If 

End Sub 

'This draws the current selected stream into a metafile 
Public Sub DrawPage(ByVal oGrx As Graphics) 

    If m_oCurrentPageStream Is Nothing Or _ 
     m_oCurrentPageStream.Length = 0 Or _ 
     m_oMetafile Is Nothing Then 

     Return 

    End If 

    'Critical section follows (no more than one thread a time) 
    SyncLock Me 

     Dim iWidth As Int32 = m_oMetafile.Width, _ 
      iHeight As Int32 = m_oMetafile.Height, _ 
      oDestPoint As Point = Nothing 

     'Prepare metafile delegate 
     m_oDelegate = New Graphics.EnumerateMetafileProc(AddressOf MetafileCallback) 

     'Draw in the rectangle 
     oDestPoint = New Point(0, 0) 
     oGrx.EnumerateMetafile(m_oMetafile, oDestPoint, m_oDelegate) 

     'Clean up 
     m_oDelegate = Nothing 

    End SyncLock 

End Sub 

Public Function PrintReport(ByVal byaReport()() As Byte, _ 
          ByVal sPrinterName As String _ 
          ) As Boolean 

    'Report data is an array of pages. Each page in turn is another byte array. 
    Me.byaPagesToPrint = byaReport 
    m_iNumberOfPages = Me.byaPagesToPrint.Length 

    Try 

     Dim oPS As PrinterSettings = New PrinterSettings 
     oPS.MaximumPage = m_iNumberOfPages 
     oPS.MinimumPage = 1 
     oPS.PrintRange = PrintRange.SomePages 
     oPS.FromPage = 1 
     oPS.ToPage = m_iNumberOfPages 
     oPS.PrinterName = sPrinterName 

     Dim oPD As PrintDocument = New PrintDocument 
     m_iCurrentPrintingPage = 1 
     m_iLastPrintingPage = m_iNumberOfPages 
     oPD.PrinterSettings = oPS 

     'Do print the report 
     AddHandler oPD.PrintPage, AddressOf Me.pd_PrintPage 
     oPD.Print() 

     Return True 

    Catch oEx As Exception 

     'Report error 
     glb_oLog.Log(oEx.ToString, clsLogger.enuSeverity.sev_01_Error, , True) 

    End Try 

End Function 

End Class