Print rdlc report without viewing print dialogue box

a 夏天 提交于 2019-12-08 04:32:14

问题


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?

Thanks a lot.


回答1:


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:

' 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



回答2:


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 ReportViewer and RenderingComplete event.

In order to not display PrintDialog box you must set printDoc.PrinterSettings.PrinterName with your default printer name.

Maybe you can store this value in a user configuration file.




回答3:


It is actually far more simple than you would have imagined.

Within your form, include a "PrintDocument" component from the Toolbox.

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.

As tezzo mentioned, to set the Printer manually you can use the following snippet:

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.

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.



来源:https://stackoverflow.com/questions/25011576/print-rdlc-report-without-viewing-print-dialogue-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!