【问题标题】:RDLC:- Print without dialogue after rendered reportviewerRDLC:- 呈现reportviewer 后无对话打印
【发布时间】:2019-03-22 15:13:20
【问题描述】:

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

Application UI

我想做的事 我想自动化报表查看器的内置打印功能(红色箭头),但在按下打印按钮(黄色箭头)时没有选择打印机对话框。

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

我的尝试 尝试使用 PrintDocument() 打印报告查看器,但呈现空白页。无法理解如何将报表查看器分配给 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)) 

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

【问题讨论】:

  • 我已经用一个半有效的解决方案更新了我的问题
  • 页面设置“混乱”是什么意思?
  • 我已经更新了我的问题,请参阅更新 2
  • 您需要根据可以从 LocalReport.GetDefaultPageSettings() 获得的页面设置来设置页边距。检查我的示例中的“export()”方法。
  • 我明白你想做什么,但我想知道我怎样才能像这样传递我自己的变量.. Dim hight As Double = 585 Dim width As Double = 827 Dim autoprintme As AutoPrintCls = New AutoPrintCls (ReportViewer1.LocalReport, 高度. 宽度)

标签: c# vb.net rdlc reportviewer


【解决方案1】:

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

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)

【讨论】:

  • 点击打印按钮后没有任何反应,没有打印队列。也没有错误。 PrinterSettings 选择了我的默认打印机(Adove PDF)
  • 当您单击打印按钮时,您的报告是否已经加载到报告查看器中?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-30
  • 2019-05-07
  • 1970-01-01
  • 2016-04-16
  • 2021-05-17
  • 2018-08-17
  • 1970-01-01
相关资源
最近更新 更多