打印rdlc报告而不查看打印对话框

时间:2014-07-29 08:57:29

标签: vb.net visual-studio reporting-services rdlc

我正在写一个POS应用程序,它需要经常打印发票。 我需要将其直接发送到打印机而不是查看打印对话框。使用Reportviewer_renderingcomplete,我可以避免看到报告,但我不知道如何避免在没有用户干预的情况下看到打印对话框和打印报告?

非常感谢。

3 个答案:

答案 0 :(得分:1)

以下是如何做到这一点:

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

答案 1 :(得分:0)

这是微软有趣的演练:Printing a Local Report without Preview。 它与您的方法不同,因为它直接打印报告而不使用ReportViewerRenderingComplete事件。

为了不显示PrintDialog框,您必须使用默认的打印机名称设置printDoc.PrinterSettings.PrinterName

也许您可以将此值存储在用户配置文件中。

答案 2 :(得分:0)

它实际上比你想象的要简单得多。

在表单中,包含工具箱中的“PrintDocument”组件。

在您的代码中,您需要在新添加的组件上调用以下方法。

PrintDoc.Print()

文档说明Print()“开始文档的打印过程”。它将自动开始打印默认设置的打印机。

正如tezzo所提到的,要手动设置打印机,您可以使用以下代码段:

PrintDoc.PrinterSettings.PrinterName = "YourPrinterNameHere"

PrintDoc.PrinterSettings.PrinterName“根据文档获取或设置要使用的打印机的名称”。如果您需要任何进一步的帮助,请查看此isCapsLockOn

但请注意,该视频未提及如何“静默”打印。对于初学者来说,它只是一个很好的参考,可以看到打印组件如何协同工作。

相关问题