[英]VB.NET - Print RDLC Report without showing ReportViewer control
在 Reza Aghaei 的幫助下,我設法創建了一個 DLL
這是 C# 代碼(我從Print RDLC Report 中得到,但沒有顯示 ReportViewer Control ):
using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
public static class LocalReportExtensions
{
public static void Print(this LocalReport report)
{
var pageSettings = new PageSettings();
pageSettings.PaperSize = report.GetDefaultPageSettings().PaperSize;
pageSettings.Landscape = report.GetDefaultPageSettings().IsLandscape;
pageSettings.Margins = report.GetDefaultPageSettings().Margins;
Print(report, pageSettings);
}
public static void Print(this LocalReport report, PageSettings pageSettings)
{
string deviceInfo =
$@"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>{pageSettings.PaperSize.Width * 100}in</PageWidth>
<PageHeight>{pageSettings.PaperSize.Height * 100}in</PageHeight>
<MarginTop>{pageSettings.Margins.Top * 100}in</MarginTop>
<MarginLeft>{pageSettings.Margins.Left * 100}in</MarginLeft>
<MarginRight>{pageSettings.Margins.Right * 100}in</MarginRight>
<MarginBottom>{pageSettings.Margins.Bottom * 100}in</MarginBottom>
</DeviceInfo>";
Warning[] warnings;
var streams = new List<Stream>();
var currentPageIndex = 0;
report.Render("Image", deviceInfo,
(name, fileNameExtension, encoding, mimeType, willSeek) =>
{
var stream = new MemoryStream();
streams.Add(stream);
return stream;
}, out warnings);
foreach (Stream stream in streams)
stream.Position = 0;
if (streams == null || streams.Count == 0)
throw new Exception("Error: no stream to print.");
var printDocument = new PrintDocument();
printDocument.DefaultPageSettings = pageSettings;
if (!printDocument.PrinterSettings.IsValid)
throw new Exception("Error: cannot find the default printer.");
else
{
printDocument.PrintPage += (sender, e) =>
{
Metafile pageImage = new Metafile(streams[currentPageIndex]);
Rectangle adjustedRect = new Rectangle(
e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
e.PageBounds.Width,
e.PageBounds.Height);
e.Graphics.FillRectangle(Brushes.White, adjustedRect);
e.Graphics.DrawImage(pageImage, adjustedRect);
currentPageIndex++;
e.HasMorePages = (currentPageIndex < streams.Count);
e.Graphics.DrawRectangle(Pens.Red, adjustedRect);
};
printDocument.EndPrint += (Sender, e) =>
{
if (streams != null)
{
foreach (Stream stream in streams)
stream.Close();
streams = null;
}
};
printDocument.Print();
}
}
}
在 Reza Aghaei 提到的 C# 中, function 被稱為
this.reportViewer1.LocalReport.Print();
我參考並嘗試在 vb.net 中調用它
如果我嘗試
this.reportViewer1.LocalReport.Print()
或者
LocalReport.Print()
它給出了錯誤。
我缺少什么請提供幫助,或者如果專家可以將其轉換為 Vb.net,將不勝感激。
我做的收據是這樣的。
如果任何專家幫助我解決這個問題,我將不勝感激,因為我主要在 VB.net 工作。
注意:我嘗試的所有轉換器都無法將鏈接帖子的 C# 代碼轉換為正確的 VB.NET 版本。 您可以在鏈接的帖子中找到原始 C# 版本:打印 RDLC 報告而不顯示 ReportViewer 控件。
下面是LocalReport的Print擴展方法的VB版本:
Imports Microsoft.Reporting.WinForms
Imports System.Drawing.Imaging
Imports System.Drawing.Printing
Imports System.IO
Imports System.Runtime.CompilerServices
Public Module LocalReportExtensions
<Extension()>
Sub Print(ByVal report As LocalReport)
Dim pageSettings = New PageSettings()
pageSettings.PaperSize = report.GetDefaultPageSettings().PaperSize
pageSettings.Landscape = report.GetDefaultPageSettings().IsLandscape
pageSettings.Margins = report.GetDefaultPageSettings().Margins
Print(report, pageSettings)
End Sub
<Extension()>
Sub Print(ByVal report As LocalReport, ByVal pageSettings As PageSettings)
Dim deviceInfo As String = $"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>{pageSettings.PaperSize.Width * 100}in</PageWidth>
<PageHeight>{pageSettings.PaperSize.Height * 100}in</PageHeight>
<MarginTop>{pageSettings.Margins.Top * 100}in</MarginTop>
<MarginLeft>{pageSettings.Margins.Left * 100}in</MarginLeft>
<MarginRight>{pageSettings.Margins.Right * 100}in</MarginRight>
<MarginBottom>{pageSettings.Margins.Bottom * 100}in</MarginBottom>
</DeviceInfo>"
Dim warnings() As Warning
Dim streams = New List(Of Stream)()
Dim currentPageIndex = 0
report.Render("Image", deviceInfo,
Function(name, fileNameExtension, encoding, mimeType, willSeek)
Dim stream = New MemoryStream()
streams.Add(stream)
Return stream
End Function, warnings)
For Each stream As Stream In streams
stream.Position = 0
Next
If streams Is Nothing OrElse streams.Count = 0 Then
Throw New Exception("Error: no stream to print.")
End If
Dim printDocument = New PrintDocument()
printDocument.DefaultPageSettings = pageSettings
If Not printDocument.PrinterSettings.IsValid Then
Throw New Exception("Error: cannot find the default printer.")
Else
AddHandler printDocument.PrintPage,
Sub(sender, e)
Dim pageImage As Metafile = New Metafile(streams(currentPageIndex))
Dim adjustedRect As Rectangle = New Rectangle(
e.PageBounds.Left - CInt(e.PageSettings.HardMarginX),
e.PageBounds.Top - CInt(e.PageSettings.HardMarginY),
e.PageBounds.Width,
e.PageBounds.Height)
e.Graphics.FillRectangle(Brushes.White, adjustedRect)
e.Graphics.DrawImage(pageImage, adjustedRect)
currentPageIndex += 1
e.HasMorePages = (currentPageIndex < streams.Count)
e.Graphics.DrawRectangle(Pens.Red, adjustedRect)
End Sub
AddHandler printDocument.EndPrint,
Sub(Sender, e)
If streams IsNot Nothing Then
For Each stream As Stream In streams
stream.Close()
Next
streams = Nothing
End If
End Sub
printDocument.Print()
End If
End Sub
End Module
要使用上面的代碼,在LocalReport
上調用Print
擴展方法就足夠了。 例如,如果您有一個(可見或不可見的 ReportViewer):
Me.ReportViewer1.LocalReport.Print()
如果要傳遞打印機設置:
Me.ReportViewer1.LocalReport.Print(ReportViewer1.GetPageSettings())
此代碼失敗,您必須更改以下行:
暗淡警告()作為警告=沒有
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.