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
の長さはので、私は、次のコードを使用してmemorystreamsをフィルタ処理しようとした408
であることがわかりましたPrint
サブ
Dim var = m_streams.Where(Function(X) X.Length <= 408).ToList
For Each ms As MemoryStream In var
m_streams.Remove(ms)
Next
そして空白ページを排除する。しかし、私はこのクエリを他の場合に使用することができると仮定することはできません?どんな提案
注:
-
:
- 私が提供
C#
- コードで答えを受け入れるには、
- は、私がページ幅を最小化しようとしたが、それでも、何度も幅が余分なホワイトページを作る
- レポートは、レポートの幅を変更しようとすることはでき
Update 1の上にリンクされMSDN記事からです
を提供問題を解決しようとする。飛び出す唯一の問題は、使用するLocalReport.Render()オーバーロードです。今は、他のオーバーロードにかかるPageCountMode引数を指定しない「簡単な」ものです。PageCountMode.Estimateを使用します。明らかに、PageCountMode.Actualを使用する正当な理由があります。 –
@HansPassantこのコードはMSDNのものです。私はリンクを提供した。あなたが別の方法を知っているなら、それを提供してください。 – Hadi