2016-12-04 60 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的長度爲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
+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

你可以嘗試更改報表寬度相同的問題。

+1

報告尺寸是A4紙的尺寸。這是我需要的 – Hadi

+1

@Hadi報告大小不是A4:A4是8.27乘11.69英寸。 –

+0

@AndrewMorton我會試試看。 Thx – Hadi

0

當您創建.RDLC報告時,是否有多餘的空白空間?看到圖像的例子。白色空間以黃色突出顯示。此區域將始終打印,並顯示爲空白。

如果你這樣做,嘗試縮短空白只包圍你的表/矩陣。將鼠標懸停在黑色邊框上並將邊框向上拖動,直到它僅包圍您的桌子。這應該消除多餘的頁面。

enter image description here

+1

在報告設計師我沒有空格。它在打印時顯示 – Hadi