【发布时间】:2017-04-18 21:34:54
【问题描述】:
我正在使用Vb.net 编写的类打印RDLC 报告。
我将RDLC 报告转换为MemoryStream 列表并使用PrintDocument 对象打印它。我用这个MSDN Article 作为参考。
这是我的代码:
Private m_currentPageIndex As Integer
Private m_streams As IList(Of Stream)
Dim m_report As LocalReport
Public Sub New(ByVal v_report As LocalReport)
m_report = v_report
End Sub
' Routine to provide to the report renderer, in order to
' save an image for each page of the report.
Public 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 MemoryStream()
m_streams.Add(stream)
Return stream
End Function
' Export the given report as an EMF (Enhanced Metafile) file.
Public Sub Export(ByVal report As LocalReport)
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, AddressOf CreateStream, warnings)
For Each stream As Stream In m_streams
stream.Position = 0
Next
End Sub
' Handler for PrintPageEvents
Public Sub PrintPage(ByVal sender As Object, ByVal 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
Public Sub Print()
If m_streams Is Nothing OrElse m_streams.Count = 0 Then
Throw New Exception("Error: no stream to print.")
End If
Dim printDoc As New PrintDocument()
If Not printDoc.PrinterSettings.IsValid Then
Throw New Exception("Error: cannot find the default printer.")
Else
AddHandler printDoc.PrintPage, AddressOf PrintPage
m_currentPageIndex = 0
printDoc.Print()
End If
End Sub
' Create a local report for Report.rdlc, load the data,
' export the report to an .emf file, and print it.
Public Sub Run()
Export(m_report)
Print()
End Sub
问题是这段代码打印了 3 个额外的空白页。
我尝试设置ConsumeContainerWhiteSpaces = True
它并没有解决我的问题
调试时我发现空白页MemoryStream 的长度是408 所以我尝试使用Print Sub 中的以下代码过滤内存流
Dim var = m_streams.Where(Function(X) X.Length <= 408).ToList
For Each ms As MemoryStream In var
m_streams.Remove(ms)
Next
它消除了空白页。但我不能假设我可以使用这个查询可以用于其他情况?任何建议
注意:
- 我接受
C#中的答案 - 提供的代码来自上面链接的 MSDN 文章
更新 1:
- 我尝试最小化页面宽度,但仍然遇到同样的问题
- 不使用
ReportViewer打印报告
【问题讨论】:
-
异国代码,很难看出它试图解决的问题。唯一跳出来的是您使用的 LocalReport.Render() 重载。现在它是“简单”的,它没有指定其他重载采用的 PageCountMode 参数。它使用 PageCountMode.Estimate。显然,您有充分的理由使用 PageCountMode.Actual。
-
@HansPassant 此代码来自 MSDN。我提供了它的链接。如果您知道其他方式,请提供。
标签: c# vb.net reporting-services printing rdlc