【问题标题】:Eliminating local report extra blank pages消除本地报告额外的空白页
【发布时间】:2017-04-18 21:34:54
【问题描述】:

我正在使用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

它并没有解决我的问题

调试时我发现空白页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 打印报告

【问题讨论】:

  • 异国代码,很难看出它试图解决的问题。唯一跳出来的是您使用的 LocalReport.Render() 重载。现在它是“简单”的,它没有指定其他重载采用的 PageCountMode 参数。它使用 PageCountMode.Estimate。显然,您有充分的理由使用 PageCountMode.Actual。
  • @HansPassant 此代码来自 MSDN。我提供了它的链接。如果您知道其他方式,请提供。

标签: c# vb.net reporting-services printing rdlc


【解决方案1】:

你能不能试着改变报告的宽度,很多时候宽度会产生额外的白页。

【讨论】:

  • 报表尺寸为A4纸的尺寸。这就是我需要的
  • @Hadi 报告尺寸不是 A4:A4 为 8.27 x 11.69 英寸。
  • @AndrewMorton 我用您提供的尺寸对其进行了测试,但结果相同。
【解决方案2】:

当您创建 .RDLC 报告时,是否有多余的空白?有关示例,请参见图像。空白区域以黄色突出显示。此区域将始终打印,并且显示为空白。

如果您这样做,请尝试缩短空白以仅围绕您的表格/矩阵。将鼠标悬停在黑色边框上并向上拖动边框,直到它仅围绕您的桌子。这应该会消除多余的页面。

【讨论】:

  • 在报表设计器中我没有空格。打印时显示
  • 好吧,这让我的多余空间减掉了一半……现在是另一半……
【解决方案3】:

经过多次尝试,看起来空白页内存流大小为408,因此我们可以使用提供的代码来消除空白页

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    相关资源
    最近更新 更多