【发布时间】:2014-07-29 08:57:28
【问题描述】:
我正在编写一个 POS 应用程序,它需要经常打印发票。 我需要将其直接发送到打印机而不是查看打印对话框。使用 Reportviewer_renderingcomplete,我可以避免看到报告,但我不知道如何避免看到打印对话框并在没有用户干预的情况下打印报告?
非常感谢。
【问题讨论】:
标签: vb.net visual-studio reporting-services rdlc
我正在编写一个 POS 应用程序,它需要经常打印发票。 我需要将其直接发送到打印机而不是查看打印对话框。使用 Reportviewer_renderingcomplete,我可以避免看到报告,但我不知道如何避免看到打印对话框并在没有用户干预的情况下打印报告?
非常感谢。
【问题讨论】:
标签: vb.net visual-studio reporting-services rdlc
你可以这样做:
Dim m_currentPageIndex As Integer
Private m_streams As IList(Of Stream)
Dim report As New LocalReport()
report.DataSources.Add(New ReportDataSource("testData", reportData.Tables(0)))
report.ReportEmbeddedResource = "ReportsLibrary.rptTestData.rdlc"
Dim deviceInfo As String = "<DeviceInfo><OutputFormat>EMF</OutputFormat><PageWidth>8.5in</PageWidth><PageHeight>11in</PageHeight><MarginTop>0.25in</MarginTop><MarginLeft>0.25in</MarginLeft><MarginRight>0.25in</MarginRight><MarginBottom>0.25in</MarginBottom></DeviceInfo>"
Dim warnings As Warning()
m_streams = New List(Of Stream)()
report.Render("Image", deviceInfo, CreateStream, warnings)
For Each stream As Stream In m_streams
stream.Position = 0
Next
Dim printDoc As New PrintDocument()
printDoc.PrinterSettings.PrinterName = "<your default printer name>"
Dim ps As New PrinterSettings()
ps.PrinterName = printDoc.PrinterSettings.PrinterName
printDoc.PrinterSettings = ps
printDoc.PrintPage += New PrintPageEventHandler(PrintPage)
m_currentPageIndex = 0
printDoc.Print()
其中PrintPage定义如下:
' Handler for PrintPageEvents
Private Sub PrintPage(sender As Object, 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
【讨论】:
这是 Microsoft 的一个有趣的演练:Printing a Local Report without Preview。
这与您的方法不同,因为它直接打印报告而不使用ReportViewer 和RenderingComplete 事件。
为了不显示打印对话框,您必须将printDoc.PrinterSettings.PrinterName 设置为您的默认打印机名称。
也许您可以将此值存储在用户配置文件中。
【讨论】:
实际上比你想象的要简单得多。
在您的表单中,包含工具箱中的“PrintDocument”组件。
在您的代码中,您需要在新添加的组件上调用以下方法。
PrintDoc.Print()
文档声明 Print()“启动文档的打印过程”。它将自动开始打印默认设置的打印机。
正如 tezzo 所说,要手动设置打印机,您可以使用以下 sn-p:
PrintDoc.PrinterSettings.PrinterName = "YourPrinterNameHere"
PrintDoc.PrinterSettings.PrinterName 根据文档“获取或设置要使用的打印机的名称”。如果您需要任何进一步的帮助,请查看video.
但请注意,该视频并未提及如何“静默”打印。这只是一个很好的参考,供初学者了解打印组件如何协同工作。
【讨论】: