打印.rdlc报告,无需预览 [英] Print .rdlc report without preview

查看:119
本文介绍了打印.rdlc报告,无需预览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VB.NET创建了一个 .rdlc 报告,现在我想打印此报告而没有任何预览...请问,有人可以帮助我吗?

解决方案

也许以下内容可能有用:



http://msdn.microsoft.com/en-us/library/ms252091.aspx [ ]


添加解决方案,直接打印到默认打印机



用法

 PrintRDLCReport.PrintToPrinter 







 使用系统; 
使用 System.Collections.Generic;
使用 System.Drawing;
使用 System.Drawing.Imaging;
使用 System.Drawing.Printing;
使用 System.Globalization;
使用 System.IO;
使用 System.Text;
使用 Microsoft.Reporting.WinForms;

命名空间 AHS_EMRReprint.Controllers
{
public 静态 PrintRDLCReport {

private static int m_currentPageIndex;
private static IList< Stream> m_streams;
private static PageSettings m_pageSettings;

public static 流CreateStream( string name,
string fileNameExtension,编码编码,
string mimeType, bool willSeek)
{
Stream stream = new MemoryStream() ;
m_streams.Add(stream);
return stream;
}

public static 无效导出(LocalReport报告, bool print = true
{
PaperSize paperSize = m_pageSettings.PaperSize;
保证金保证金= m_pageSettings.Margins;

// 设备信息字符串定义要打印的页面范围以及页面。
// 0的开始和结束页面表示生成所有页面。
string deviceInfo = string .Format(
CultureInfo.InvariantCulture,
< DeviceInfo> +
< OutputFormat> EMF< / OutputFormat> +
< PageWidth> {5}< / PageWidth> +
< span class =code-string>< PageHeight> {4}< / PageHeight&g t; +
< MarginTop> {0}< / MarginTop> +
< MarginLeft> {1}< / MarginLeft> +
< MarginRight> {2}< / MarginRight> +
< MarginBottom> {3}< / MarginBottom> +
< / DeviceInfo>
ToInches(marginins.Top),
ToInches(marginins.Left),
ToInches(marginins.Right),
ToInches(marginins.Bottom),
ToInches(paperSize.Height),
ToInches(paperSize.Width) ));

警告[]警告;
m_streams = new 列表< Stream>();
report.Render( 图像,deviceInfo,CreateStream,
out 警告);
foreach (流 in m_streams)
stream.Position = 0 ;

if (print)
{
Print();
}
}


// 处理程序PrintPageEvents
public static void PrintPage( object sender,PrintPageEventArgs e)
{
Stream pageToPrint = m_streams [m_currentPageIndex];
pageToPrint.Position = 0 ;

// 将每个页面加载到一个图元文件中以绘制它。
使用(Metafile pageMetaFile = new 元文件(pageToPrint))
{
Rectangle adjustedRect = new 矩形(
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(pageMetaFile,adjustedRect);

// 准备下一页。确保我们没有达到目的。
m_currentPageIndex ++;
e.HasMorePages = m_currentPageIndex < m_streams.Count;
}
}

public static void Print()
{
if (m_streams == null || m_streams.Count == 0
throw new 异常( 错误:无要打印的流。 );
PrintDocument printDoc = new PrintDocument();
if (!printDoc.PrinterSettings.IsValid)
{
throw new 异常( 错误:找不到默认打印机。);
}
其他
{
printDoc.PrintPage + = new PrintPageEventHandler(PrintPage);
m_currentPageIndex = 0 ;
printDoc.Print();
}
}

public static void PrintToPrinter( LocalReport报告)
{
m_pageSettings = new PageSettings();
ReportPageSettings reportPageSettings = report.GetDefaultPageSettings();

m_pageSettings.PaperSize = reportPageSettings.PaperSize;
m_pageSettings.Margins = reportPageSettings.Margins;

出口(报告);
}

public static void DisposePrint()
{
if (m_streams!= null
{
foreach (流 m_streams)
stream.Close();
m_streams = null ;
}
}

私有 静态 string ToInches( int hundrethsOfInch)
{
double inches = hundrethsOfInch / 100 0 ;
return inches.ToString(CultureInfo.InvariantCulture)+ 中的;
}
}
}


  class  ReportPrintDocument:System.Drawing.Printing.PrintDocument 
{
private PageSettings m_pageSettings;
private int m_currentPage;
private 列表< stream> m_pages = new 列表< stream>();

private static 字典< string,> ReportingExtensions;


public ReportPrintDocument(ServerReport serverReport)
this ((Report)serverReport)
{
RenderAllServerReportPages(serverReport);
}

public ReportPrintDocument(LocalReport localReport, string filePath)
((报告)localReport)
{
ReportingExtensions = new Dictionary< string,> ();
ReportingExtensions.Add( 。XLS Excel);
ReportingExtensions.Add( 。XLSX Excel);
ReportingExtensions.Add( 。PDF PDF);
ReportingExtensions.Add( 。TIF 图像);

RenderAllLocalReportPages(localReport,filePath);
}

private ReportPrintDocument(报告报告)
{
// 将页面设置设置为报告中定义的默认值
ReportPageSettings reportPageSettings = report.GetDefaultPageSettings();

// 页面设置对象将使用默认打印机,除非
// PageSettings.PrinterSettings已更改。这假定
// 是默认打印机。
m_pageSettings = new PageSettings();
m_pageSettings.PaperSize = reportPageSettings.PaperSize;
m_pageSettings.Margins = reportPageSettings.Margins;
}

受保护 覆盖 void Dispose( bool 处理)
{
base .Dispose(处置);

if (处置)
{
foreach (流 in m_pages)
{
s.Dispose();
}

m_pages.Clear();
}
}

受保护 覆盖 void OnBeginPrint(PrintEventArgs e)
{
base .OnBeginPrint(e);

m_currentPage = 0 ;
}

受保护 覆盖 void OnPrintPage(PrintPageEventArgs e)
{
base .OnPrintPage(e);

Stream pageToPrint = m_pages [m_currentPage];
pageToPrint.Position = 0 ;

// 将每个页面加载到一个图元文件中以绘制它。
使用(Metafile pageMetaFile = new 元文件(pageToPrint))
{
Rectangle adjustedRect = new 矩形(
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(pageMetaFile,adjustedRect);

// 准备下一页。确保我们没有达到目的。
m_currentPage ++;
e.HasMorePages = m_currentPage < m_pages.Count;
}
}

受保护 覆盖 void OnQueryPageSettings(QueryPageSettingsEventArgs e)
{
e.PageSettings =(PageSettings)m_pageSettings.Clone();
}

private void RenderAllServerReportPages(ServerReport serverReport)
{
string deviceInfo = CreateEMFDeviceInfo();

NameValueCollection firstPageParameters = new NameValueCollection();
firstPageParameters.Add( rs:PersistStreams True);

// GetNextStream返回后台流程序列中的下一页
// 由PersistStreams启动。
NameValueCollection nonFirstPageParameters = new NameValueCollection();
nonFirstPageParameters.Add( rs:GetNextStream True);

string mimeType;
string fileExtension;
Stream pageStream = serverReport.Render( IMAGE,deviceInfo,firstPageParameters, out mimeType, out fileExtension);

// 服务器在超出最后一页时返回空流。
while (pageStream.Length > 0
{
m_pages.Add(pageStream);

pageStream = serverReport.Render( IMAGE,deviceInfo,nonFirstPageParameters, out mimeType, out fileExtension);
}
}

私有 void RenderAllLocalReportPages( LocalReport localReport, string filePath)
{
string extension = Path.GetExtension(文件路径);
string format = ;
if (ReportingExtensions.ContainsKey(extension.ToUpper()))
{
format = ReportingExtensions [extension.ToUpper()] ;
}
else
{
format = ReportingExtensions [ 。TIF];
}
string deviceInfo = CreateEMFDeviceInfo();
string mimeType = null ;
string encoding = null ;
string fileNameExtension = null ;
string [] stream = null ;
警告[]警告;
var list = localReport.ListRenderingExtensions();
// localReport.Render(IMAGE,deviceInfo,LocalReportCreateStreamCallback,out警告);
字节 [] bytes = localReport.Render(格式, null out mimeType, out 编码, out fileNameExtension, out stream, out 警告);
FileStream fs = new FileStream(filePath,FileMode.Create);
fs.Write(bytes, 0 ,bytes.Length);
fs.Close();
}

private Stream LocalReportCreateStreamCallback(
string 名称,
字符串扩展名,
编码编码,
字符串 mimeType ,
bool willSeek)
{
MemoryStream stream = new MemoryStream ();
m_pages.Add(stream);

return stream;
}

private string CreateEMFDeviceInfo()
{
PaperSize paperSize = m_pageSettings.PaperSize;
保证金保证金= m_pageSettings.Margins;

// 设备信息字符串定义要打印的页面范围以及页面。
// 0的开始和结束页面表示生成所有页面。
return string .Format(
CultureInfo.InvariantCulture,
< deviceinfo>< outputformat> emf< / outputformat>< startpage> 0< / startpage>< ;尾页大于0< /尾页>< margintop> {0}< / margintop>< marginleft> {1}< / marginleft>< marginright> {2}< / marginright>< marginbottom> {3- }< / marginbottom>< pageheight> {4}< / pageheight>< pagewidth> {5}< / pagewidth>< / deviceinfo>
ToInches(边距。 Top),
ToInches(marginins.Left),
ToInches(marginins.Right),
ToInches(marginins.Bottom),
ToInches(paperSize.Height),
ToInches(paperSize.Width));
}

private static 字符串 ToInches( int hundrethsOfInch)
{
double inches = hundrethsOfInch / 100 0 ;
return inches.ToString(CultureInfo.InvariantCulture)+ 中的;
}
} < / stream > < / stream >


I created a .rdlc report using VB.NET and now I want to print this report without any preview... please, can anyone help me?

解决方案

Maybe the following might be useful:

http://msdn.microsoft.com/en-us/library/ms252091.aspx[]


Adding on to the solution, to print directly to default printer

Usage

PrintRDLCReport.PrintToPrinter




using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Globalization;
using System.IO;
using System.Text;
using Microsoft.Reporting.WinForms;

namespace AHS_EMRReprint.Controllers
{
    public static class PrintRDLCReport {

        private static int m_currentPageIndex;
        private static IList<Stream> m_streams;
        private static PageSettings m_pageSettings;

        public static Stream CreateStream(string name,
          string fileNameExtension, Encoding encoding,
          string mimeType, bool willSeek)
        {
            Stream stream = new MemoryStream();
            m_streams.Add(stream);
            return stream;
        }

        public static void Export(LocalReport report, bool print = true)
        {
            PaperSize paperSize = m_pageSettings.PaperSize;
            Margins margins = m_pageSettings.Margins;

            // The device info string defines the page range to print as well as the size of the page.
            // A start and end page of 0 means generate all pages.
            string deviceInfo = string.Format(
                CultureInfo.InvariantCulture,
                "<DeviceInfo>" +
                    "<OutputFormat>EMF</OutputFormat>" +
                    "<PageWidth>{5}</PageWidth>" +
                    "<PageHeight>{4}</PageHeight>" +
                    "<MarginTop>{0}</MarginTop>" +
                    "<MarginLeft>{1}</MarginLeft>" +
                    "<MarginRight>{2}</MarginRight>" +
                    "<MarginBottom>{3}</MarginBottom>" +
                "</DeviceInfo>",
                ToInches(margins.Top),
                ToInches(margins.Left),
                ToInches(margins.Right),
                ToInches(margins.Bottom),
                ToInches(paperSize.Height),
                ToInches(paperSize.Width));

            Warning[] warnings;
            m_streams = new List<Stream>();
            report.Render("Image", deviceInfo, CreateStream,
               out warnings);
            foreach (Stream stream in m_streams)
                stream.Position = 0;

            if (print)
            {
                Print();
            }
        }


        // Handler for PrintPageEvents
        public static void PrintPage(object sender, PrintPageEventArgs e)
        {
            Stream pageToPrint = m_streams[m_currentPageIndex];
            pageToPrint.Position = 0;

            // Load each page into a Metafile to draw it.
            using (Metafile pageMetaFile = new Metafile(pageToPrint))
            {
                Rectangle adjustedRect = new Rectangle(
                        e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
                        e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
                        e.PageBounds.Width,
                        e.PageBounds.Height);

                // Draw a white background for the report
                e.Graphics.FillRectangle(Brushes.White, adjustedRect);

                // Draw the report content
                e.Graphics.DrawImage(pageMetaFile, adjustedRect);

                // Prepare for next page.  Make sure we haven't hit the end.
                m_currentPageIndex++;
                e.HasMorePages = m_currentPageIndex < m_streams.Count;
            }
        }

        public static void Print()
        {
            if (m_streams == null || m_streams.Count == 0)
                throw new Exception("Error: no stream to print.");
            PrintDocument printDoc = new PrintDocument();
            if (!printDoc.PrinterSettings.IsValid)
            {
                throw new Exception("Error: cannot find the default printer.");
            }
            else
            {
                printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
                m_currentPageIndex = 0;
                printDoc.Print();
            }
        }

        public static void PrintToPrinter(this LocalReport report)
        {
            m_pageSettings = new PageSettings();
            ReportPageSettings reportPageSettings = report.GetDefaultPageSettings();

            m_pageSettings.PaperSize = reportPageSettings.PaperSize;
            m_pageSettings.Margins = reportPageSettings.Margins;

            Export(report);
        }

        public static void DisposePrint()
        {
            if (m_streams != null)
            {
                foreach (Stream stream in m_streams)
                    stream.Close();
                m_streams = null;
            }
        }

        private static string ToInches(int hundrethsOfInch)
        {
            double inches = hundrethsOfInch / 100.0;
            return inches.ToString(CultureInfo.InvariantCulture) + "in";
        }
    }
}


class ReportPrintDocument : System.Drawing.Printing.PrintDocument
    {
        private PageSettings m_pageSettings;
        private int m_currentPage;
        private List<stream> m_pages = new List<stream>();

        private static Dictionary<string,> ReportingExtensions;


        public ReportPrintDocument(ServerReport serverReport)
            : this((Report)serverReport)
        {
            RenderAllServerReportPages(serverReport);
        }

        public ReportPrintDocument(LocalReport localReport, string filePath)
            : this((Report)localReport)
        {
            ReportingExtensions = new Dictionary<string,>();
            ReportingExtensions.Add(".XLS", "Excel");
            ReportingExtensions.Add(".XLSX", "Excel");
            ReportingExtensions.Add(".PDF", "PDF");
            ReportingExtensions.Add(".TIF", "Image");

            RenderAllLocalReportPages(localReport, filePath);
        }
        
        private ReportPrintDocument(Report report)
        {
            // Set the page settings to the default defined in the report
            ReportPageSettings reportPageSettings = report.GetDefaultPageSettings();

            // The page settings object will use the default printer unless
            // PageSettings.PrinterSettings is changed.  This assumes there
            // is a default printer.
            m_pageSettings = new PageSettings();
            m_pageSettings.PaperSize = reportPageSettings.PaperSize;
            m_pageSettings.Margins = reportPageSettings.Margins;
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (disposing)
            {
                foreach (Stream s in m_pages)
                {
                    s.Dispose();
                }

                m_pages.Clear();
            }
        }

        protected override void OnBeginPrint(PrintEventArgs e)
        {
            base.OnBeginPrint(e);

            m_currentPage = 0;
        }

        protected override void OnPrintPage(PrintPageEventArgs e)
        {
            base.OnPrintPage(e);

            Stream pageToPrint = m_pages[m_currentPage];
            pageToPrint.Position = 0;

            // Load each page into a Metafile to draw it.
            using (Metafile pageMetaFile = new Metafile(pageToPrint))
            {
                Rectangle adjustedRect = new Rectangle(
                        e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
                        e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
                        e.PageBounds.Width,
                        e.PageBounds.Height);

                // Draw a white background for the report
                e.Graphics.FillRectangle(Brushes.White, adjustedRect);

                // Draw the report content
                e.Graphics.DrawImage(pageMetaFile, adjustedRect);

                // Prepare for next page.  Make sure we haven't hit the end.
                m_currentPage++;
                e.HasMorePages = m_currentPage < m_pages.Count;
            }
        }
        
        protected override void OnQueryPageSettings(QueryPageSettingsEventArgs e)
        {
            e.PageSettings = (PageSettings)m_pageSettings.Clone();
        }

        private void RenderAllServerReportPages(ServerReport serverReport)
        {
            string deviceInfo = CreateEMFDeviceInfo();

                       NameValueCollection firstPageParameters = new NameValueCollection();
            firstPageParameters.Add("rs:PersistStreams", "True");

            // GetNextStream returns the next page in the sequence from the background process
            // started by PersistStreams.
            NameValueCollection nonFirstPageParameters = new NameValueCollection();
            nonFirstPageParameters.Add("rs:GetNextStream", "True");

            string mimeType;
            string fileExtension;
            Stream pageStream = serverReport.Render("IMAGE", deviceInfo, firstPageParameters, out mimeType, out fileExtension);

            // The server returns an empty stream when moving beyond the last page.
            while (pageStream.Length > 0)
            {
                m_pages.Add(pageStream);

                pageStream = serverReport.Render("IMAGE", deviceInfo, nonFirstPageParameters, out mimeType, out fileExtension);
            }
        }

        private void RenderAllLocalReportPages(LocalReport localReport, string filePath)
        {
            string extension = Path.GetExtension(filePath);
            string format = "";
            if (ReportingExtensions.ContainsKey(extension.ToUpper()))
            {
                format = ReportingExtensions[extension.ToUpper()];
            }
            else
            {
                format = ReportingExtensions[".TIF"];
            }
            string deviceInfo = CreateEMFDeviceInfo();
            string mimeType = null;
            string encoding = null;
            string fileNameExtension = null;
            string[] stream = null;
            Warning[] warnings;
            var list = localReport.ListRenderingExtensions();
            //localReport.Render("IMAGE", deviceInfo, LocalReportCreateStreamCallback, out warnings);
            Byte[] bytes = localReport.Render(format, null, out mimeType, out encoding, out fileNameExtension, out stream, out warnings);
            FileStream fs = new FileStream(filePath, FileMode.Create);
            fs.Write(bytes, 0, bytes.Length);
            fs.Close();
        }

        private Stream LocalReportCreateStreamCallback(
            string name,
            string extension,
            Encoding encoding,
            string mimeType,
            bool willSeek)
        {
            MemoryStream stream = new MemoryStream();
            m_pages.Add(stream);

            return stream;
        }

        private string CreateEMFDeviceInfo()
        {
            PaperSize paperSize = m_pageSettings.PaperSize;
            Margins margins = m_pageSettings.Margins;

            // The device info string defines the page range to print as well as the size of the page.
            // A start and end page of 0 means generate all pages.
            return string.Format(
                CultureInfo.InvariantCulture,
                "<deviceinfo><outputformat>emf</outputformat><startpage>0</startpage><endpage>0</endpage><margintop>{0}</margintop><marginleft>{1}</marginleft><marginright>{2}</marginright><marginbottom>{3}</marginbottom><pageheight>{4}</pageheight><pagewidth>{5}</pagewidth></deviceinfo>",
                ToInches(margins.Top),
                ToInches(margins.Left),
                ToInches(margins.Right),
                ToInches(margins.Bottom),
                ToInches(paperSize.Height),
                ToInches(paperSize.Width));
        }

        private static string ToInches(int hundrethsOfInch)
        {
            double inches = hundrethsOfInch / 100.0;
            return inches.ToString(CultureInfo.InvariantCulture) + "in";
        }
    }</stream></stream>


这篇关于打印.rdlc报告,无需预览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆