2016-12-04 26 views
3

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記事からです

  • は、私がページ幅を最小化しようとしたが、それでも、何度も幅が余分なホワイトページを作る
  • レポートは、レポートの幅を変更しようとすることはでき
+0

を提供問題を解決しようとする。飛び出す唯一の問題は、使用するLocalReport.Render()オーバーロードです。今は、他のオーバーロードにかかるPageCountMode引数を指定しない「簡単な」ものです。PageCountMode.Estimateを使用します。明らかに、PageCountMode.Actualを使用する正当な理由があります。 –

+1

@HansPassantこのコードはMSDNのものです。私はリンクを提供した。あなたが別の方法を知っているなら、それを提供してください。 – Hadi

答えて

0

多くの試行を与えた後、空白ページメモリストリームのサイズが408であるように、それはそう空白のページを排除するために、我々は、コードを使用することができ、見え見えにくい、エキゾチックコード

Dim var = m_streams.Where(Function(X) X.Length <= 408).ToList 

For Each ms As MemoryStream In var 
    m_streams.Remove(ms) 
Next 
0

ReportViewerを使用せずに印刷されているのと同じ問題を抱えています。

+1

レポートの寸法は、A4用紙の寸法です。これは私が必要なものです – Hadi

+1

@ハディレポートサイズはA4ではありません:A4は8.27 x 11.69インチです。 –

+0

@AndrewMorton私はそれを試してみます。 Thx – Hadi

0

.RDLCレポートを作成するときに余分な空白がありますか?例については、画像を参照してください。空白は黄色で強調表示されます。この領域は常にとなり、空白に見えます。

この場合、テーブル/マトリックスのみを囲むように空白を短くしてみてください。黒い枠線の上にカーソルを置き、枠線のみを囲むまで枠線をドラッグします。それは余分なページをなくすはずです。

enter image description here

+1

レポートデザイナーでは、空白がありません。印刷中に表示されます – Hadi

関連する問題