关于Rdlc文件直接打印浅谈

      rdlc Report是.net提供给我们的一个免费使用的一个文件,拥有强大的功能。下面我来谈谈关于rdlc文档直接打印遇到的一些问题以及解决办法,希望对大家有所帮助,也希望有好的方法留言.

      关于 rdlc 直接打印是一个老生常谈的问题了,现在网络上比较常见的就是"泥人张"所写的那个打印方法。这种方法确实很好用,但是在用的中间也遇到很多问题.

     这段打印分成两个部分,首先生成emf文件,然后通过调用PrintDocument打印这个emf实现直接打印.

 

Public Function Run(ByVal report As LocalReport, ByVal printerName As String, ByVal Copy As Integer) Try Export(report) m_currentPageIndex = 0 Return Print(printerName, Copy) Catch ex As Exception WriteLog(ex.Message, True, LogType.Err) Return False End Try End Function Private Sub Export(ByVal report As LocalReport) Dim warnings As Warning() Dim deviceInfo As String Dim Streamint As Integer = 0 deviceInfo = "" & " EMF" & " 21cm" & " 29.7cm" & " 0.5cm" & " 0.5cm" & " 0.5cm" & " 1cm" & "" m_streams = New List(Of Stream)() report.Render("Image", deviceInfo, AddressOf CreateStream, warnings) For Each stream As Stream In m_streams stream.Position = 0 Next End Sub 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 FileStream((name & ".") + fileNameExtension, FileMode.Create) If Not Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "Img") Then Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "Img") End If Dim FileName As String = AppDomain.CurrentDomain.BaseDirectory + "Img/" + DateTime.Now.ToString("yyyyMMddhhmmssfffffff") Thread.Sleep(10) Dim streampic As Stream = New FileStream(FileName & "." & fileNameExtension, FileMode.Create) m_streams.Add(streampic) Return streampic End Function Private Function Print(ByVal printerName As String, ByVal Copies As Integer) As Boolean Dim Hardx As Single Dim Hardy As Single If m_streams Is Nothing OrElse m_streams.Count = 0 Then Return False Exit Function End If Dim printDoc As New PrintDocument() If printerName <> "" Then printDoc.PrinterSettings.PrinterName = printerName End If If Not printDoc.PrinterSettings.IsValid Then Dim msg As String = [String].Format("Can't find printer ""{0}"".", printerName) WriteLog(msg, True, LogType.Err) Return False Exit Function End If '打印机设置 printDoc.OriginAtMargins = False '该页可打印区域的左上角 With printDoc.PrinterSettings .Copies = Copies '打印份数 With .DefaultPageSettings Hardx = .HardMarginX Hardy = .HardMarginY .PaperSize = printDoc.PrinterSettings.PaperSizes.Item(3) '设置A4纸张 .Landscape = False With .Margins .Left = 23 - Hardx .Right = 0 .Top = 0 .Bottom = 23 - Hardy End With End With End With AddHandler printDoc.PrintPage, AddressOf PrintPage printDoc.Print() Return True End Function Private Sub PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs) Dim pageImage As New Metafile(m_streams(m_currentPageIndex)) ev.Graphics.DrawImage(pageImage, ev.PageBounds) m_currentPageIndex += 1 ev.HasMorePages = (m_currentPageIndex < m_streams.Count) End Sub

 

 .Left = 23 - Hardx
 .Bottom = 23 - Hardy

这两句是根据不同打印机的 hardx 和 hardy 设置 left ,bottom 已适应打印机。这个比较重要,因为我在测试的时候发现,喷墨和激光打印机打印同样的东西差的很远,需要这样进行设置,具体的高度不一定是以23为准,需要大家根据自己的打印机标准为准。

 

还有要注意的就是rdlc的页边距和Export中页边距设置要一致,如果发现右边打印不下来可以尝试调高Export中页宽度.

你可能感兴趣的:(.Net)