如何从vb.net打印SSRS ServerReport

时间:2011-10-24 03:07:35

标签: vb.net reporting-services

我一直在寻找...我只能找到如何打印LocalReport,而不是ServerReport。

我不想要打印对话或任何东西,我只是想要打印。

我已经设法打印第一页,但我无法弄清楚如何获取额外的页面?

1 个答案:

答案 0 :(得分:1)

此VB.Net(2005)课程将打印SSRS 2003报告。我认为 MAY 能够处理任何SSRS版本,因为报告作为字节数组的数组传递,但遗憾的是我还无法连接到(不管渲染!)SSRS 2005或SSRS 2008服务来测试它

您应该调用PrintReport函数向其传递Report bytes(呈现为IMAGE)和目标打印机名称。

好的,这是代码(请注意,这是我的第一次尝试之一,并根据示例C代码改编,不记得'Net'中的位置):

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