locked
Stream Specific pages from RDLC report RRS feed

  • Question

  • User-1305530094 posted

    Dear All,

    I am using the following code to stream RDLC report pages into image files and then print them,

    My question is there a way to modify the code to stream only certain page number or a range of pages? 

    Thanks

    Public Class RepVuer
        Inherits System.Web.UI.Page
        Implements IDisposable
        Private m_currentPageIndex As Integer
        Private m_streams As IList(Of Stream)
    
     Private Function CreateStream(ByVal name As String, ByVal fileNameExtension As String, ByVal encoding As Encoding, ByVal mimeType As String, ByVal willSeek As Boolean) As Stream
            Dim stream As Stream = New MemoryStream()
            m_streams.Add(stream)
            Return stream
        End Function
        Public Function PageSizeParam(ByVal Format As String)
            Dim DW As String = Nothing
            Dim DH As String = Nothing
            Dim Mrg_top As String = Nothing
            Dim Mrg_Right As String = Nothing
            Dim Mrg_Left As String = Nothing
            Dim Mrg_Bottom As String = Nothing
            Dim RawData As String = PgeSize.SelectedValue
            Dim Arg = RawData.Split("/")
            Dim ParamDim = Arg(0).Split(",")
            Dim W As String = ParamDim(0)
            Dim H As String = ParamDim(1)
            Dim ParamMarg = Arg(1).Split(",")
            Mrg_top = ParamMarg(0)
            Mrg_Right = ParamMarg(1)
            Mrg_Left = ParamMarg(2)
            Mrg_Bottom = ParamMarg(3)
            Select Case PgeOri.SelectedValue
                Case Is = 1
                    DW = W
                    DH = H
                Case Is = 2
                    DW = H
                    DH = W
            End Select
            Dim deviceInfo As String = "<DeviceInfo>" & _
              "<OutputFormat>" & Format & "</OutputFormat>" & _
              "<PageWidth>" & DW & "in" & "</PageWidth>" & _
              "<PageHeight>" & DH & "in" & "</PageHeight>" & _
              "<MarginTop>" & Mrg_top & "in" & "</MarginTop>" & _
              "<MarginLeft>" & Mrg_Left & "in" & "</MarginLeft>" & _
              "<MarginRight>" & Mrg_Right & "in" & "</MarginRight>" & _
              "<MarginBottom>" & Mrg_Bottom & "in" & "</MarginBottom>" & _
              "</DeviceInfo>"
            Return deviceInfo
        End Function
    
        Private Sub Print()
            Dim warnings As Warning()
            m_streams = New List(Of Stream)()
            RepVuerCtl.LocalReport.Render("Image", PageSizeParam("EMF"), AddressOf CreateStream, warnings)
            For Each stream As Stream In m_streams
                stream.Position = 0
            Next
            If m_streams Is Nothing OrElse m_streams.Count = 0 Then
                Throw New Exception("Error: no stream to print.")
            End If
            Dim printDoc As New PrintDocument()
            If Not printDoc.PrinterSettings.IsValid Then
                Throw New Exception("Error: cannot find the default printer.")
            Else
                AddHandler printDoc.PrintPage, AddressOf PrintPage
    
                m_currentPageIndex = 0
                printDoc.Print()
                DisposeStrm()
            End If
        End Sub
    
       Private Sub PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
            Dim pageImage As New Metafile(m_streams(m_currentPageIndex))
    
            ' Adjust rectangular area with printer margins.
            Dim adjustedRect As New Rectangle(ev.PageBounds.Left - CInt(ev.PageSettings.HardMarginX), _
                                              ev.PageBounds.Top - CInt(ev.PageSettings.HardMarginY), _
                                              ev.PageBounds.Width, _
                                              ev.PageBounds.Height)
    
            ' Draw a white background for the report
            ev.Graphics.FillRectangle(Brushes.White, adjustedRect)
    
            ' Draw the report content
            ev.Graphics.DrawImage(pageImage, adjustedRect)
    
            ' Prepare for the next page. Make sure we haven't hit the end.
            m_currentPageIndex += 1
            ev.HasMorePages = (m_currentPageIndex < m_streams.Count)
        End Sub
        Public Sub DisposeStrm() Implements IDisposable.Dispose
            If m_streams IsNot Nothing Then
                For Each stream As Stream In m_streams
                    stream.Close()
                Next
                m_streams = Nothing
            End If
        End Sub
    Protected Sub PrntPnlBtn_Click(sender As Object, e As ImageClickEventArgs) Handles PrntPnlBtn.Click
    Print()
    End Sub
    End Class

    Sunday, October 18, 2015 2:06 AM

Answers

  • User1644755831 posted

    Hello Embryologist,

    Private m_streams As IList(Of Stream)

    I am not sure if it can be done directly. but could you not take required stream from the list of stream. I mean you can take the stream for the specific page and then print it?

    Dim pageImage As New Metafile(m_streams(m_currentPageIndex))

    You just need to use the specific stream here.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 20, 2015 10:05 PM

All replies

  • User1644755831 posted

    Hello Embryologist,

    Private m_streams As IList(Of Stream)

    I am not sure if it can be done directly. but could you not take required stream from the list of stream. I mean you can take the stream for the specific page and then print it?

    Dim pageImage As New Metafile(m_streams(m_currentPageIndex))

    You just need to use the specific stream here.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 20, 2015 10:05 PM
  • User-1305530094 posted

    Daer krunal,

    could you please elaborate more,

    Thanks!

    Thursday, October 22, 2015 4:01 AM
  • User1644755831 posted

    Hello,

    could you please elaborate more,

    What I mean is that the method will return streams for each page. you can just choose stream for specific page and then print it.

    With Regards,

    Krunal Parekh

    Sunday, October 25, 2015 10:08 PM