我正在使用用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
並且它不固定我的問題
在調試時,我發現一個blankpage MemoryStream
的長度爲408
所以我嘗試使用以下代碼來篩選memorystreams 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。我提供了它的鏈接。如果你知道另一種方式,請提供它。 – Hadi