消除本地报告额外的空白页
Eliminating local report extra blank pages
我正在使用 Vb.net
编写的 class 打印一份 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
打印报表
你能不能试试改变报告的宽度,宽度多了会产生额外的白页。
创建.RDLC 报告时,是否有多余的白色space?有关示例,请参见图片。白色 space 以黄色突出显示。此区域将始终打印,并且显示为空白。
如果这样做,请尝试缩短白色 space 以仅包围您的 table / 矩阵。将鼠标悬停在黑色边框上并向上拖动边框,直到它只包围您的 table。这应该会消除多余的页面。
经过多次尝试,似乎空白页内存流大小为408,因此要消除空白页,我们可以使用提供的代码
Dim var = m_streams.Where(Function(X) X.Length <= 408).ToList
For Each ms As MemoryStream In var
m_streams.Remove(ms)
Next
我正在使用 Vb.net
编写的 class 打印一份 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
打印报表
你能不能试试改变报告的宽度,宽度多了会产生额外的白页。
创建.RDLC 报告时,是否有多余的白色space?有关示例,请参见图片。白色 space 以黄色突出显示。此区域将始终打印,并且显示为空白。
如果这样做,请尝试缩短白色 space 以仅包围您的 table / 矩阵。将鼠标悬停在黑色边框上并向上拖动边框,直到它只包围您的 table。这应该会消除多余的页面。
经过多次尝试,似乎空白页内存流大小为408,因此要消除空白页,我们可以使用提供的代码
Dim var = m_streams.Where(Function(X) X.Length <= 408).ToList
For Each ms As MemoryStream In var
m_streams.Remove(ms)
Next