RDLC:-渲染报告查看器后打印而无需对话

时间:2018-10-17 20:47:08

标签: c# vb.net rdlc reportviewer

我正在为我的朋友创建一个简单的发票管理系统。用户可以创建发票并在需要时打印。我目前只能打印发票部分。这是一种B2B商业模式,需要一次打印100张发票。我使用MS RDLC创建了报告。将其链接到报表查看器。下面是我的应用程序的用户界面

Application UI

我要做什么 我想自动执行报表查看器的内置打印功能(红色箭头),但是在按“打印”按钮(黄色箭头)时没有选择打印机对话框。

到处都有很多代码示例。我已经阅读过MS的演练,但无法完全理解。

我尝试过的事情 试图使用PrintDocument()打印reportviewer,但呈现空白页。无法理解如何将reportviewer分配给PrintDocument对象。

    Dim printDoc As New PrintDocument()
    Dim ps As New PrinterSettings()
    printDoc.PrinterSettings.PrinterName = ps.PrinterName 'Default Printer Name
    printDoc.Print() 'How to assign the Reportviewer1 to printdoc?

============更新============

我尝试了搜索网络时发现的以下类。

Custom Class

然后我这样称呼它。

 Dim autoprintme As AutoPrintCls = New AutoPrintCls(ReportViewer1.LocalReport)
 autoprintme.Print()

它可以通过我的默认打印机进行工作和打印,但是页面设置混乱了。谁能指出我的写作方向?

我的页面设置

Hight:14.8cm  Width:21cm 

============更新2 ============

由于页边距设置错误,我搞砸了。

我已经在课堂上对此进行了更改

"<DeviceInfo><OutputFormat>emf</OutputFormat>
<StartPage>0</StartPage>
<EndPage>0</EndPage>
<MarginTop>{0}</MarginTop>
<MarginLeft>{1}</MarginLeft>
<MarginRight>{2}</MarginRight>
<MarginBottom>{3}</MarginBottom>
<PageHeight>{4}</PageHeight>
<PageWidth>{5}</PageWidth>
</DeviceInfo>", "0in","0in","0in","0in", ToInches(585),
ToInches(827)) 

但是所有内容都是硬编码的,如何将按钮单击事件中的高度,宽度和边距值传递给类?

1 个答案:

答案 0 :(得分:0)

将此类添加到您的项目中:

Imports System.Drawing.Imaging
Imports System.Drawing.Printing
Imports System.Globalization
Imports System.IO
Imports System.Text
Imports Microsoft.Reporting.WinForms

Public Class ReportPrinter
    Public Sub Print(report As LocalReport)
        ' print
        export(report)
        printReport(report)
    End Sub

    Private _currentPageIndex As Integer
    Private _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()
        _streams.Add(stream)
        Return stream
    End Function

    Private Sub export(ByVal report As LocalReport)
        Dim settings As ReportPageSettings = report.GetDefaultPageSettings()
        Dim pageInfo As String
        If Not settings.IsLandscape Then
            pageInfo = _
                String.Format("<PageWidth>{0}</PageWidth>" & _
                              "<PageHeight>{1}</PageHeight>", _
                              toInches(settings.PaperSize.Width), _
                              toInches(settings.PaperSize.Height))
        Else
            pageInfo = _
                String.Format("<PageHeight>{0}</PageHeight>" & _
                              "<PageWidth>{1}</PageWidth>", _
                              toInches(settings.PaperSize.Width), _
                              toInches(settings.PaperSize.Height))
        End If
        Dim deviceInfo As String = _
            String.Format("<DeviceInfo>" & _
                            "<OutputFormat>EMF</OutputFormat>" & _
                            "{0}" & _
                            "<MarginTop>{1}</MarginTop>" & _
                            "<MarginLeft>{2}</MarginLeft>" & _
                            "<MarginRight>{3}</MarginRight>" & _
                            "<MarginBottom>{4}</MarginBottom>" & _
                            "</DeviceInfo>", _
                            pageInfo, _
                            toInches(settings.Margins.Top), _
                            toInches(settings.Margins.Left), _
                            toInches(settings.Margins.Right), _
                            toInches(settings.Margins.Bottom))
        Dim warnings As Warning() = Nothing
        _streams = New List(Of Stream)()
        report.Render("Image", deviceInfo, AddressOf createStream, warnings)
        For Each stream As Stream In _streams
            stream.Position = 0
        Next
    End Sub

    Private Function toInches(hundrethsOfInch As Integer) As String
        Dim inches As Double = hundrethsOfInch / 100.0
        Return inches.ToString(CultureInfo.InvariantCulture) + "in"
    End Function

    Private Sub printPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim pageImage As Metafile = New Metafile(_streams(_currentPageIndex))

        ' Adjust rectangular area with printer margins.
        Dim adjustedRect As System.Drawing.Rectangle = _
            New System.Drawing.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(System.Drawing.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.
        _currentPageIndex += 1
        ev.HasMorePages = (_currentPageIndex < _streams.Count)
    End Sub

    Private Sub printReport(report As LocalReport)
        If _streams Is Nothing OrElse _streams.Count = 0 Then
            Throw New Exception("No stream to print.")
        End If

        Dim printDoc As New PrintDocument()
        printDoc.DocumentName = report.DisplayName
        printDoc.DefaultPageSettings.Landscape = report.GetDefaultPageSettings().IsLandscape
        printDoc.PrintController = New StandardPrintController()
        If Not printDoc.PrinterSettings.IsValid Then
            Throw New Exception("Cannot find the default printer.")
        Else
            AddHandler printDoc.PrintPage, AddressOf printPage
            _currentPageIndex = 0
            printDoc.Print()
        End If
    End Sub
End Class

然后像这样使用它:

Dim printer As ReportPrinter = New ReportPrinter()
printer.Print(reportViewer1.LocalReport)