gpt4 book ai didi

c# - 消除本地报告额外的空白页

转载 作者:行者123 更新时间:2023-11-30 18:18:39 26 4
gpt4 key购买 nike

我正在使用用 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 中使用以下代码过滤内存流子

    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 打印报告

最佳答案

您可以尝试更改报告宽度吗,多次宽度会产生额外的白页。

关于c# - 消除本地报告额外的空白页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40958213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com