vb.net 不查看打印对话框打印rdlc报告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25011576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print rdlc report without viewing print dialogue box
提问by Furqan Sehgal
I have am writing a POS application, which requires to print invoice very often. I need to send it directly to printer instead of viewing the print dialogue. Using Reportviewer_renderingcomplete, I can avoid seeing the report but I do not know how to avoid seeing the print dialogue box and print report without user intervention?
我正在编写一个 POS 应用程序,它需要经常打印发票。我需要将它直接发送到打印机而不是查看打印对话框。使用Reportviewer_renderingcomplete,我可以避免看到报告,但我不知道如何在没有用户干预的情况下避免看到打印对话框和打印报告?
Thanks a lot.
非常感谢。
采纳答案by InitK
Here is how you can do it:
您可以这样做:
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()
Where PrintPage defined as follows:
其中 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
回答by tezzo
This is an interesting walkthrough by Microsoft: Printing a Local Report without Preview.
It's a different approach from yours because it prints directly a report without using ReportViewerand RenderingCompleteevent.
这是 Microsoft 的一个有趣的演练:Printing a Local Report without Preview。这是一种与您不同的方法,因为它直接打印报告而不使用ReportViewer和RenderingComplete事件。
In order to not display PrintDialog box you must set printDoc.PrinterSettings.PrinterNamewith your default printer name.
为了不显示printDoc.PrinterSettings.PrinterName打印对话框,您必须使用默认打印机名称进行设置。
Maybe you can store this value in a user configuration file.
也许您可以将此值存储在用户配置文件中。
回答by ChallengeAccepted
It is actually far more simple than you would have imagined.
它实际上比您想象的要简单得多。
Within your form, include a "PrintDocument" component from the Toolbox.
在您的表单中,包括工具箱中的“PrintDocument”组件。
Within your code behind, you will want to call the following method on your newly added component.
在后面的代码中,您需要在新添加的组件上调用以下方法。
PrintDoc.Print()
The documentations state that the Print() "Starts the document's printing process". It will automatically begin printing the default set printer.
文档说明 Print() “启动文档的打印过程”。它将自动开始打印默认设置的打印机。
As tezzo mentioned, to set the Printer manually you can use the following snippet:
正如 tezzo 所提到的,要手动设置打印机,您可以使用以下代码段:
PrintDoc.PrinterSettings.PrinterName = "YourPrinterNameHere"
PrintDoc.PrinterSettings.PrinterName "gets or sets the name of the printer to use" as according to documentation. And if you need any further help, check out this video.
PrintDoc.PrinterSettings.PrinterName 根据文档“获取或设置要使用的打印机的名称”。如果您需要任何进一步的帮助,请查看此视频。
Please note however that video does not mention how to print "silently". It is just a good reference for beginners to see how the Print Components work together.
但请注意,该视频并未提及如何“静默”打印。它只是初学者了解打印组件如何协同工作的一个很好的参考。

