VB.NET-打印RDLC报表,不显示ReportViewer控件

VB.NET - Print RDLC Report without showing ReportViewer control(VB.NET-打印RDLC报表,不显示ReportViewer控件)

本文介绍了VB.NET-打印RDLC报表,不显示ReportViewer控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Reza Aghaei的帮助下成功创建了一个DLL

这里是C#代码(我从Print RDLC Report without showing 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#中,使用

调用该函数
this.reportViewer1.LocalReport.Print();

我引用并尝试在vb.net中调用它

如果我尝试

this.reportViewer1.LocalReport.Print()

LocalReport.Print()

出现错误。

我遗漏了什么,请帮助,或者如果专家可以将其转换为Vb.net,我将不胜感激。

我开的收据是这样的。

如果有专家帮我解决这个问题,我将不胜感激,因为我主要在VB.net工作。

VB.NET

注意:我尝试的所有转换器都无法将链接帖子的C#代码转换为正确的推荐答案版本。您可以在链接的帖子中找到原始的C#版本:Print RDLC Report without showing ReportViewer Control.

VB.NET-打印RDLC报表而不显示ReportViewer控件

这里是LocalReport的打印扩展方法的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())

这篇关于VB.NET-打印RDLC报表,不显示ReportViewer控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:VB.NET-打印RDLC报表,不显示ReportViewer控件

基础教程推荐