LocalReport Print with C# C#打印RDLC
生活随笔
收集整理的這篇文章主要介紹了
LocalReport Print with C# C#打印RDLC
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?1?代碼
?2?#region?public?static
?3?????????///?<summary>
?4?????????///?獲取指定ReportViewer總物理頁(yè)數(shù)
?5????????///?guog2009-05-06新增
?6?????????///?</summary>
?7?????????///?<param?name="reportViewer">ReportViewer控件</param>
?8?????????///?<returns>總物理頁(yè)數(shù),默認(rèn)值為0</returns>
?9??????????public?static?int?GetTotalPage(ReportViewer?reportViewer)
10?????????{
11?????????????int?result?=?0;
12?????????????//設(shè)置為打印預(yù)覽模式
13??????????????reportViewer.SetDisplayMode(DisplayMode.PrintLayout);
14?????????????
15?????????????//記錄當(dāng)前頁(yè)
16??????????????int?currentPage?=?reportViewer.CurrentPage;
17?????????????if?(reportViewer.LocalReport.GetTotalPages()?>?0)
18?????????????{//自帶的GetTotalPages()方法返回的是邏輯頁(yè)面總數(shù),所以這里重新處理
19??
20?????????????????while?(true)
21?????????????????{
22?????????????????????try
23?????????????????????{
24?????????????????????????reportViewer.CurrentPage?+=?1;
25?????????????????????}
26?????????????????????catch
27?????????????????????{
28?????????????????????????reportViewer.CurrentPage?-=?1;
29?????????????????????????result?=?reportViewer.CurrentPage;
30?????????????????????????break;
31?????????????????????}
32?????????????????????finally
33?????????????????????{
34?????????????????????????//指定到原始位置
35??????????????????????????reportViewer.CurrentPage?=?currentPage;
36?????????????????????}
37?????????????????}
38?????????????}
39?
40?????????????return?result;
41?????????}
42?
43?
44????????#endregion?public?staticView Code代碼
using?System;
?using?System.Collections.Generic;
?using?System.ComponentModel;
?using?System.Data;
?using?System.Drawing;
?using?System.Text;
?using?System.Windows.Forms;
using?yxsoft.emcms.cs.ReportPrintBaseLib;
using?yxsoft.emcms.common;
using?System.Reflection;
using?Microsoft.Reporting.WinForms;
using?Guog.Emcms.Win.Business.GeneralDataQuery.VerifyPrintView;
using?System.Drawing.Printing;
using?System.IO;
using?System.Drawing.Imaging;
using?System.Xml;
namespace?Guog.Emcms.Win.YXEmcms.Forms
{
????public?partial?class?FrmMeterVerifyPrintView?:?Form
????{
???????#region?成員變量
???????///?<summary>
???????///?打印的對(duì)象
???????///?</summary>
???????private?PrintDocument?m_printDocument?=?new?PrintDocument();
???????///?<summary>
???????///?頁(yè)面設(shè)置
???????///?</summary>
???????private?PageSettings?m_pageSettings?=?new?PageSettings();
???????///?<summary>
???????///?打印文件流列表
???????///?</summary>
???????private?IList<Stream>?m_streams?=?new?List<Stream>();
???????///?<summary>
???????///?當(dāng)前呈現(xiàn)的頁(yè)
???????///?</summary>
???????private?int?m_currentPageIndex?=?0;
???????///?<summary>
???????///?是否橫向打印,true表示橫向,false表示縱向
???????///?</summary>
???????private?bool?m_isLandSapce?=?false;
???????///?<summary>
???????///?打印輸出的對(duì)象
???????///?</summary>
???????private?ReportPrintBase?m_reportPrintBase?=?null;
????????///?<summary>
????????///?要打印輸出的記錄總數(shù)
????????///?</summary>
????????private?int?m_chkedCount?=?0;
????????///?<summary>
????????///?空白頁(yè)
????????///?</summary>
????????private?bool?m_isBlank?=?true;
????????///?<summary>
????????///?雙面打印
????????///?</summary>
????????private?bool?m_duplex?=?false;
???????#endregion?成員變量
???????#region?窗體事件
???????public?FrmMeterVerifyPrintView(ReportPrintBase?reportPrintBase,int?chkedCount)
????????{
????????????InitializeComponent();
????????????this.m_chkedCount?=?chkedCount;
????????????this.m_reportPrintBase?=?reportPrintBase;
????????????this.m_isLandSapce=this.IsLandSapces(this.m_reportPrintBase.MainReport.ReportFileStream);
????????????this.DoubleBuffered?=?true;
????????}
???????private?void?FrmMeterVerifyPrintView_Load(object?sender,?EventArgs?e)
???????{
??????????BindPrinters();
???????}
???????private?void?FrmMeterVerifyPrintView_Shown(object?sender,?EventArgs?e)
???????{
??????????if?(m_reportPrintBase?!=?null?&&?m_reportPrintBase.MainReport?!=?null?&&
?????????????m_reportPrintBase.MainReport.Validate)
??????????{
?????????????BindReportData(this.m_reportPrintBase);
??????????}
??????????this.rptView.SetDisplayMode(DisplayMode.PrintLayout);
??????????this.rptView.ZoomMode?=?ZoomMode.PageWidth;
??????????//this.rptView.ZoomPercent?=?100;
???????}
???????#region?加載報(bào)表數(shù)據(jù)
???????private?void?BindReportData(ReportPrintBase?reportPrintBase)
???????{
??????????//報(bào)表信息有效
??????????if?(reportPrintBase?!=?null?&&?reportPrintBase.MainReport?!=?null?&&?reportPrintBase.MainReport.Validate)
??????????{//支持的打印類型
?????????????#region?添加子報(bào)表加載事件,向子報(bào)表提供數(shù)據(jù)源支持
?????????????if?(reportPrintBase.subreportProcessingEventHandler?!=?null)
?????????????{
????????????????//加載子報(bào)表指定的事件,每個(gè)子報(bào)表加載時(shí)通過(guò)此事件指向的方法添加對(duì)應(yīng)的數(shù)據(jù)源
????????????????this.rptView.LocalReport.SubreportProcessing?+=?reportPrintBase.subreportProcessingEventHandler;
?????????????}
?????????????#endregion?加載子報(bào)表事件
?????????????#region?添加報(bào)表
?????????????//加載主報(bào)表文件流
?????????????this.rptView.LocalReport.LoadReportDefinition(reportPrintBase.MainReport.ReportFileStream);
?????????????//添加主報(bào)表數(shù)據(jù)源
?????????????this.rptView.LocalReport.DataSources.Add(reportPrintBase.MainReport.DataSource);
?????????????if?(reportPrintBase.SubReports?!=?null?&&?reportPrintBase.SubReports.Count?>?0)
?????????????{
????????????????foreach?(ReportBaseInfo?rpt?in?reportPrintBase.SubReports)
????????????????{
???????????????????//加載子報(bào)表文件流
???????????????????this.rptView.LocalReport.LoadSubreportDefinition(rpt.Name,?rpt.ReportFileStream);
????????????????}
?????????????}
?????????????#endregion?添加報(bào)表
?????????????//重新呈現(xiàn)(刷新)ReportViewer
?????????????this.rptView.RefreshReport();
??????????}//報(bào)表信息有效
???????}
???????#endregion?加載報(bào)表數(shù)據(jù)??????
????????private?void?FrmMeterVerifyPrintView_FormClosing(object?sender,?FormClosingEventArgs?e)
????????{
????????????this.rptView.Reset();
????????????CloseStreams();
????????????Application.DoEvents();
?????????}
?????????///?<summary>
?????????///?關(guān)閉當(dāng)前所有文件流
?????????///?</summary>
?????????private?void?CloseStreams()
?????????{
?????????????if?(this.m_streams?!=?null?&&?this.m_streams.Count?>?0)
?????????????{
?????????????????foreach?(Stream?s?in?this.m_streams)
?????????????????{
?????????????????????s.SetLength(0);
?????????????????????s.Flush();
?????????????????????s.Close();
?????????????????}
?????????????????this.m_streams.Clear();
?????????????}
?????????}
???????#endregion
???????#region?獲取所有打印機(jī)
?????????///?<summary>
?????????///?獲取當(dāng)前安裝在計(jì)算機(jī)上的所有打印機(jī)名稱
?????????///?</summary>
?????????///?<returns></returns>
?????????public?static?PrinterSettings.StringCollection?GetAllPrintNames()
?????????{??
????????????return?System.Drawing.Printing.PrinterSettings.InstalledPrinters;
?????????}
???????#endregion?獲取所有打印機(jī)
???????#region?綁定打印機(jī)列表
???????private?void?BindPrinters()
???????{
??????????cmbSelectPrinter.Items.Clear();
??????????cmbSelectPrinter.DropDownStyle?=?ComboBoxStyle.DropDownList;
??????????string?printerName;
??????????//獲取默認(rèn)打印機(jī)
??????????string?defaultPrinterName?=?this.m_printDocument.PrinterSettings.PrinterName;
??????????for?(int?i?=?0;?i?<?PrinterSettings.InstalledPrinters.Count;?i++)
??????????{
?????????????printerName?=?PrinterSettings.InstalledPrinters[i];
?????????????cmbSelectPrinter.Items.Add(printerName);
?????????????if?(printerName?==?defaultPrinterName)
?????????????{
????????????????cmbSelectPrinter.SelectedIndex?=?i;
?????????????}
??????????}
???????}
??????#endregion?綁定打印機(jī)列表View Code代碼
??#region?打印
???????#region?判斷是否為橫向
???????///?<summary>
???????///?判斷是否為橫向
???????///?</summary>
???????///?<param?name="stm">Rdlc文件流</param>
???????///?<returns></returns>
???????private?bool?IsLandSapces(Stream?stm)
???????{
??????????string?strPageWidth?=?"";
??????????string?strPageHeight?=?"";
??????????XmlReader?xmlReader?=?XmlReader.Create(stm);
??????????if?(xmlReader.ReadToFollowing("PageWidth"))
??????????{
?????????????strPageWidth?=?xmlReader.ReadElementString("PageWidth");
??????????}
??????????xmlReader.Close();
??????????return?strPageWidth?==?"29.7cm";
???????}
???????#endregion?讀取XML文件
???????///?<summary>
???????///?提供?Stream?對(duì)象以進(jìn)行呈現(xiàn)的?CreateStreamCallback?委托指向的方法
???????///?這里為將報(bào)表的每一個(gè)頁(yè)面作為一個(gè)EMF圖片存放,通常用于報(bào)表呈現(xiàn)
???????///?</summary>
???????///?<param?name="name">流的名稱</param>
???????///?<param?name="fileNameExtension">創(chuàng)建文件流時(shí)要使用的文件擴(kuò)展名</param>
???????///?<param?name="encoding">指定流的字符編碼的?Encoding?枚舉器值。如果流不包含字符,則此值可能為?null。</param>
???????///?<param?name="mimeType">一個(gè)包含流的?MIME?類型的?string</param>
???????///?<param?name="willSeek">指示流是否需要支持查找的?Boolean?值。如果值為?false,則流將為只前推,并將按其創(chuàng)建順序發(fā)送到塊區(qū)中的客戶端。如果值為?true,則流可以任何順序?qū)懭搿?/span></param>
???????///?<returns>ReportViewer?控件可以寫入數(shù)據(jù)的?Stream?對(duì)象</returns>
??????private?Stream?CreateStream(string?name,string?fileNameExtension,?Encoding?encoding,
?????????string?mimeType,?bool?willSeek)
??????{
?????????Stream?stream?=?new?FileStream(Path.GetTempPath()?+?name?+"."?+?fileNameExtension,?FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite);
?????????//Stream?stream?=?new?FileStream(Path.GetTempFileName(),?FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite,8000,true);
?????????m_streams.Add(stream);
?????????return?stream;
??????}
???????///?<summary>
???????///?導(dǎo)出報(bào)表的每一個(gè)頁(yè)面到一個(gè)EMF文件
???????///?</summary>
???????///?<param?name="report">ReportViewer.LocalReport</param>
???????///?<param?name="pageSettings">頁(yè)面設(shè)置</param>
???????private?void?Export(LocalReport?report,PageSettings?pageSettings)
???????{
??????????StringBuilder?sbDeviceInfo?=?new?StringBuilder();
??????????if?(pageSettings?!=?null)
??????????{
?????????????sbDeviceInfo.AppendLine("<DeviceInfo>");
?????????????sbDeviceInfo.AppendLine("??<OutputFormat>EMF</OutputFormat>");
?????????????sbDeviceInfo.AppendLine(String.Format("??<PageWidth>{0}cm</PageWidth>",?FromInchToCM(pageSettings.PaperSize.Width)));
?????????????sbDeviceInfo.AppendLine(String.Format("??<PageHeight>{0}cm</PageHeight>",?FromInchToCM(pageSettings.PaperSize.Height)));
?????????????sbDeviceInfo.AppendLine(String.Format("??<MarginTop>{0}cm</MarginTop>",?FromInchToCM(pageSettings.Margins.Top)));
?????????????sbDeviceInfo.AppendLine(String.Format("??<MarginLeft>{0}cm</MarginLeft>",?FromInchToCM(pageSettings.Margins.Left)));
?????????????sbDeviceInfo.AppendLine(String.Format("??<MarginRight>{0}cm</MarginRight>",?FromInchToCM(pageSettings.Margins.Right)));
?????????????sbDeviceInfo.AppendLine(String.Format("??<MarginBottom>{0}cm</MarginBottom>",?FromInchToCM(pageSettings.Margins.Bottom)));
?????????????sbDeviceInfo.AppendLine("</DeviceInfo>");
??????????}
??????????else
??????????{
?????????????string?deviceInfo?=
?????????????"<DeviceInfo>"?+
?????????????"??<OutputFormat>EMF</OutputFormat>"?+
?????????????"??<PageWidth>21cm</PageWidth>"?+
?????????????"??<PageHeight>29.7cm</PageHeight>"?+
?????????????"??<MarginTop>2.5cm</MarginTop>"?+
?????????????"??<MarginLeft>2.5cm</MarginLeft>"?+
?????????????"??<MarginRight>2.5cm</MarginRight>"?+
?????????????"??<MarginBottom>2.5cm</MarginBottom>"?+
?????????????"</DeviceInfo>";
?????????????sbDeviceInfo.AppendLine(deviceInfo);
??????????}
??????????Warning[]?warnings;
??????????m_streams?=?new?List<Stream>();
??????????report.Render("Image",?sbDeviceInfo.ToString(),?new?CreateStreamCallback(CreateStream),?out?warnings);
??????????foreach?(Stream?stream?in?m_streams)
?????????????stream.Position?=?0;
???????}
???????///?<summary>
???????///?當(dāng)前頁(yè)打印的輸出
???????///?</summary>
???????///?<param?name="sender"></param>
???????///?<param?name="ev"></param>
???????private?void?PrintPage(object?sender,?PrintPageEventArgs?ev)
???????{
???????????if?(this.m_duplex)
???????????{//雙面打印
???????????????#region?雙面打印
???????????????//獲取每份頁(yè)數(shù)
???????????????int?pagePerCopy?=?GetPageCountPerCopy(this.m_streams.Count,?this.m_chkedCount);
???????????????if?(this.m_currentPageIndex?>?0?&&?(this.m_currentPageIndex?+?1)?%?pagePerCopy?==?1?&&?this.m_isBlank?&&?pagePerCopy?%?2?!=?0)
???????????????{//輸出空白頁(yè)
???????????????????//當(dāng)前頁(yè)面不是第一頁(yè),且當(dāng)前頁(yè)面為此份報(bào)表第一頁(yè),且每份報(bào)表頁(yè)數(shù)為奇數(shù)
???????????????????Bitmap?emptyImage?=?new?Bitmap(10,?10);
???????????????????if?(ev.PageSettings.Landscape)
???????????????????{
???????????????????????ev.Graphics.DrawImage(emptyImage,?new?Rectangle(0,?0,?ev.PageBounds.Height,?ev.PageBounds.Width));
???????????????????}
???????????????????else
???????????????????{
???????????????????????ev.Graphics.DrawImage(emptyImage,?ev.PageBounds);
???????????????????}
???????????????????//標(biāo)記已經(jīng)打印空白頁(yè)
???????????????????this.m_isBlank?=?false;
???????????????}
???????????????else
???????????????{
???????????????????Metafile?pageImage?=?new?Metafile(m_streams[m_currentPageIndex]);
???????????????????if?(ev.PageSettings.Landscape)
???????????????????{
???????????????????????ev.Graphics.DrawImage(pageImage,?new?Rectangle(0,?0,?ev.PageBounds.Height,?ev.PageBounds.Width));
???????????????????}
???????????????????else
???????????????????{
???????????????????????ev.Graphics.DrawImage(pageImage,?ev.PageBounds);
???????????????????}
???????????????????m_currentPageIndex++;
???????????????????//標(biāo)記等待打印下一個(gè)空白頁(yè)
???????????????????this.m_isBlank?=?true;
???????????????}
???????????????#endregion?雙面打印
???????????}
???????????else
???????????{//單面打印
???????????????#region?單面打印
???????????????Metafile?pageImage?=?new?Metafile(m_streams[m_currentPageIndex]);
???????????????if?(ev.PageSettings.Landscape)
???????????????{
???????????????????ev.Graphics.DrawImage(pageImage,?new?Rectangle(0,?0,?ev.PageBounds.Height,?ev.PageBounds.Width));
???????????????}
???????????????else
???????????????{
???????????????????ev.Graphics.DrawImage(pageImage,?ev.PageBounds);
???????????????}
???????????????m_currentPageIndex++;
???????????????#endregion?單面打印
???????????}
??????????ev.HasMorePages?=?(m_currentPageIndex?<?m_streams.Count);
???????}
???????///?<summary>
???????///?打印開始
???????///?</summary>
???????///?<param?name="sender"></param>
???????///?<param?name="e"></param>
???????private?void?BeginPrint(object?sender,?PrintEventArgs?e)
???????{
??????????this.btnPrint.Enabled?=?false;
???????}
???????///?<summary>
???????///?打印結(jié)束
???????///?</summary>
???????///?<param?name="sender"></param>
???????///?<param?name="e"></param>
???????private?void?EndPrint(object?sender,?PrintEventArgs?e)
???????{
??????????this.btnPrint.Enabled?=?true;
???????}View Code
?2?#region?public?static
?3?????????///?<summary>
?4?????????///?獲取指定ReportViewer總物理頁(yè)數(shù)
?5????????///?guog2009-05-06新增
?6?????????///?</summary>
?7?????????///?<param?name="reportViewer">ReportViewer控件</param>
?8?????????///?<returns>總物理頁(yè)數(shù),默認(rèn)值為0</returns>
?9??????????public?static?int?GetTotalPage(ReportViewer?reportViewer)
10?????????{
11?????????????int?result?=?0;
12?????????????//設(shè)置為打印預(yù)覽模式
13??????????????reportViewer.SetDisplayMode(DisplayMode.PrintLayout);
14?????????????
15?????????????//記錄當(dāng)前頁(yè)
16??????????????int?currentPage?=?reportViewer.CurrentPage;
17?????????????if?(reportViewer.LocalReport.GetTotalPages()?>?0)
18?????????????{//自帶的GetTotalPages()方法返回的是邏輯頁(yè)面總數(shù),所以這里重新處理
19??
20?????????????????while?(true)
21?????????????????{
22?????????????????????try
23?????????????????????{
24?????????????????????????reportViewer.CurrentPage?+=?1;
25?????????????????????}
26?????????????????????catch
27?????????????????????{
28?????????????????????????reportViewer.CurrentPage?-=?1;
29?????????????????????????result?=?reportViewer.CurrentPage;
30?????????????????????????break;
31?????????????????????}
32?????????????????????finally
33?????????????????????{
34?????????????????????????//指定到原始位置
35??????????????????????????reportViewer.CurrentPage?=?currentPage;
36?????????????????????}
37?????????????????}
38?????????????}
39?
40?????????????return?result;
41?????????}
42?
43?
44????????#endregion?public?staticView Code代碼
using?System;
?using?System.Collections.Generic;
?using?System.ComponentModel;
?using?System.Data;
?using?System.Drawing;
?using?System.Text;
?using?System.Windows.Forms;
using?yxsoft.emcms.cs.ReportPrintBaseLib;
using?yxsoft.emcms.common;
using?System.Reflection;
using?Microsoft.Reporting.WinForms;
using?Guog.Emcms.Win.Business.GeneralDataQuery.VerifyPrintView;
using?System.Drawing.Printing;
using?System.IO;
using?System.Drawing.Imaging;
using?System.Xml;
namespace?Guog.Emcms.Win.YXEmcms.Forms
{
????public?partial?class?FrmMeterVerifyPrintView?:?Form
????{
???????#region?成員變量
???????///?<summary>
???????///?打印的對(duì)象
???????///?</summary>
???????private?PrintDocument?m_printDocument?=?new?PrintDocument();
???????///?<summary>
???????///?頁(yè)面設(shè)置
???????///?</summary>
???????private?PageSettings?m_pageSettings?=?new?PageSettings();
???????///?<summary>
???????///?打印文件流列表
???????///?</summary>
???????private?IList<Stream>?m_streams?=?new?List<Stream>();
???????///?<summary>
???????///?當(dāng)前呈現(xiàn)的頁(yè)
???????///?</summary>
???????private?int?m_currentPageIndex?=?0;
???????///?<summary>
???????///?是否橫向打印,true表示橫向,false表示縱向
???????///?</summary>
???????private?bool?m_isLandSapce?=?false;
???????///?<summary>
???????///?打印輸出的對(duì)象
???????///?</summary>
???????private?ReportPrintBase?m_reportPrintBase?=?null;
????????///?<summary>
????????///?要打印輸出的記錄總數(shù)
????????///?</summary>
????????private?int?m_chkedCount?=?0;
????????///?<summary>
????????///?空白頁(yè)
????????///?</summary>
????????private?bool?m_isBlank?=?true;
????????///?<summary>
????????///?雙面打印
????????///?</summary>
????????private?bool?m_duplex?=?false;
???????#endregion?成員變量
???????#region?窗體事件
???????public?FrmMeterVerifyPrintView(ReportPrintBase?reportPrintBase,int?chkedCount)
????????{
????????????InitializeComponent();
????????????this.m_chkedCount?=?chkedCount;
????????????this.m_reportPrintBase?=?reportPrintBase;
????????????this.m_isLandSapce=this.IsLandSapces(this.m_reportPrintBase.MainReport.ReportFileStream);
????????????this.DoubleBuffered?=?true;
????????}
???????private?void?FrmMeterVerifyPrintView_Load(object?sender,?EventArgs?e)
???????{
??????????BindPrinters();
???????}
???????private?void?FrmMeterVerifyPrintView_Shown(object?sender,?EventArgs?e)
???????{
??????????if?(m_reportPrintBase?!=?null?&&?m_reportPrintBase.MainReport?!=?null?&&
?????????????m_reportPrintBase.MainReport.Validate)
??????????{
?????????????BindReportData(this.m_reportPrintBase);
??????????}
??????????this.rptView.SetDisplayMode(DisplayMode.PrintLayout);
??????????this.rptView.ZoomMode?=?ZoomMode.PageWidth;
??????????//this.rptView.ZoomPercent?=?100;
???????}
???????#region?加載報(bào)表數(shù)據(jù)
???????private?void?BindReportData(ReportPrintBase?reportPrintBase)
???????{
??????????//報(bào)表信息有效
??????????if?(reportPrintBase?!=?null?&&?reportPrintBase.MainReport?!=?null?&&?reportPrintBase.MainReport.Validate)
??????????{//支持的打印類型
?????????????#region?添加子報(bào)表加載事件,向子報(bào)表提供數(shù)據(jù)源支持
?????????????if?(reportPrintBase.subreportProcessingEventHandler?!=?null)
?????????????{
????????????????//加載子報(bào)表指定的事件,每個(gè)子報(bào)表加載時(shí)通過(guò)此事件指向的方法添加對(duì)應(yīng)的數(shù)據(jù)源
????????????????this.rptView.LocalReport.SubreportProcessing?+=?reportPrintBase.subreportProcessingEventHandler;
?????????????}
?????????????#endregion?加載子報(bào)表事件
?????????????#region?添加報(bào)表
?????????????//加載主報(bào)表文件流
?????????????this.rptView.LocalReport.LoadReportDefinition(reportPrintBase.MainReport.ReportFileStream);
?????????????//添加主報(bào)表數(shù)據(jù)源
?????????????this.rptView.LocalReport.DataSources.Add(reportPrintBase.MainReport.DataSource);
?????????????if?(reportPrintBase.SubReports?!=?null?&&?reportPrintBase.SubReports.Count?>?0)
?????????????{
????????????????foreach?(ReportBaseInfo?rpt?in?reportPrintBase.SubReports)
????????????????{
???????????????????//加載子報(bào)表文件流
???????????????????this.rptView.LocalReport.LoadSubreportDefinition(rpt.Name,?rpt.ReportFileStream);
????????????????}
?????????????}
?????????????#endregion?添加報(bào)表
?????????????//重新呈現(xiàn)(刷新)ReportViewer
?????????????this.rptView.RefreshReport();
??????????}//報(bào)表信息有效
???????}
???????#endregion?加載報(bào)表數(shù)據(jù)??????
????????private?void?FrmMeterVerifyPrintView_FormClosing(object?sender,?FormClosingEventArgs?e)
????????{
????????????this.rptView.Reset();
????????????CloseStreams();
????????????Application.DoEvents();
?????????}
?????????///?<summary>
?????????///?關(guān)閉當(dāng)前所有文件流
?????????///?</summary>
?????????private?void?CloseStreams()
?????????{
?????????????if?(this.m_streams?!=?null?&&?this.m_streams.Count?>?0)
?????????????{
?????????????????foreach?(Stream?s?in?this.m_streams)
?????????????????{
?????????????????????s.SetLength(0);
?????????????????????s.Flush();
?????????????????????s.Close();
?????????????????}
?????????????????this.m_streams.Clear();
?????????????}
?????????}
???????#endregion
???????#region?獲取所有打印機(jī)
?????????///?<summary>
?????????///?獲取當(dāng)前安裝在計(jì)算機(jī)上的所有打印機(jī)名稱
?????????///?</summary>
?????????///?<returns></returns>
?????????public?static?PrinterSettings.StringCollection?GetAllPrintNames()
?????????{??
????????????return?System.Drawing.Printing.PrinterSettings.InstalledPrinters;
?????????}
???????#endregion?獲取所有打印機(jī)
???????#region?綁定打印機(jī)列表
???????private?void?BindPrinters()
???????{
??????????cmbSelectPrinter.Items.Clear();
??????????cmbSelectPrinter.DropDownStyle?=?ComboBoxStyle.DropDownList;
??????????string?printerName;
??????????//獲取默認(rèn)打印機(jī)
??????????string?defaultPrinterName?=?this.m_printDocument.PrinterSettings.PrinterName;
??????????for?(int?i?=?0;?i?<?PrinterSettings.InstalledPrinters.Count;?i++)
??????????{
?????????????printerName?=?PrinterSettings.InstalledPrinters[i];
?????????????cmbSelectPrinter.Items.Add(printerName);
?????????????if?(printerName?==?defaultPrinterName)
?????????????{
????????????????cmbSelectPrinter.SelectedIndex?=?i;
?????????????}
??????????}
???????}
??????#endregion?綁定打印機(jī)列表View Code代碼
??#region?打印
???????#region?判斷是否為橫向
???????///?<summary>
???????///?判斷是否為橫向
???????///?</summary>
???????///?<param?name="stm">Rdlc文件流</param>
???????///?<returns></returns>
???????private?bool?IsLandSapces(Stream?stm)
???????{
??????????string?strPageWidth?=?"";
??????????string?strPageHeight?=?"";
??????????XmlReader?xmlReader?=?XmlReader.Create(stm);
??????????if?(xmlReader.ReadToFollowing("PageWidth"))
??????????{
?????????????strPageWidth?=?xmlReader.ReadElementString("PageWidth");
??????????}
??????????xmlReader.Close();
??????????return?strPageWidth?==?"29.7cm";
???????}
???????#endregion?讀取XML文件
???????///?<summary>
???????///?提供?Stream?對(duì)象以進(jìn)行呈現(xiàn)的?CreateStreamCallback?委托指向的方法
???????///?這里為將報(bào)表的每一個(gè)頁(yè)面作為一個(gè)EMF圖片存放,通常用于報(bào)表呈現(xiàn)
???????///?</summary>
???????///?<param?name="name">流的名稱</param>
???????///?<param?name="fileNameExtension">創(chuàng)建文件流時(shí)要使用的文件擴(kuò)展名</param>
???????///?<param?name="encoding">指定流的字符編碼的?Encoding?枚舉器值。如果流不包含字符,則此值可能為?null。</param>
???????///?<param?name="mimeType">一個(gè)包含流的?MIME?類型的?string</param>
???????///?<param?name="willSeek">指示流是否需要支持查找的?Boolean?值。如果值為?false,則流將為只前推,并將按其創(chuàng)建順序發(fā)送到塊區(qū)中的客戶端。如果值為?true,則流可以任何順序?qū)懭搿?/span></param>
???????///?<returns>ReportViewer?控件可以寫入數(shù)據(jù)的?Stream?對(duì)象</returns>
??????private?Stream?CreateStream(string?name,string?fileNameExtension,?Encoding?encoding,
?????????string?mimeType,?bool?willSeek)
??????{
?????????Stream?stream?=?new?FileStream(Path.GetTempPath()?+?name?+"."?+?fileNameExtension,?FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite);
?????????//Stream?stream?=?new?FileStream(Path.GetTempFileName(),?FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite,8000,true);
?????????m_streams.Add(stream);
?????????return?stream;
??????}
???????///?<summary>
???????///?導(dǎo)出報(bào)表的每一個(gè)頁(yè)面到一個(gè)EMF文件
???????///?</summary>
???????///?<param?name="report">ReportViewer.LocalReport</param>
???????///?<param?name="pageSettings">頁(yè)面設(shè)置</param>
???????private?void?Export(LocalReport?report,PageSettings?pageSettings)
???????{
??????????StringBuilder?sbDeviceInfo?=?new?StringBuilder();
??????????if?(pageSettings?!=?null)
??????????{
?????????????sbDeviceInfo.AppendLine("<DeviceInfo>");
?????????????sbDeviceInfo.AppendLine("??<OutputFormat>EMF</OutputFormat>");
?????????????sbDeviceInfo.AppendLine(String.Format("??<PageWidth>{0}cm</PageWidth>",?FromInchToCM(pageSettings.PaperSize.Width)));
?????????????sbDeviceInfo.AppendLine(String.Format("??<PageHeight>{0}cm</PageHeight>",?FromInchToCM(pageSettings.PaperSize.Height)));
?????????????sbDeviceInfo.AppendLine(String.Format("??<MarginTop>{0}cm</MarginTop>",?FromInchToCM(pageSettings.Margins.Top)));
?????????????sbDeviceInfo.AppendLine(String.Format("??<MarginLeft>{0}cm</MarginLeft>",?FromInchToCM(pageSettings.Margins.Left)));
?????????????sbDeviceInfo.AppendLine(String.Format("??<MarginRight>{0}cm</MarginRight>",?FromInchToCM(pageSettings.Margins.Right)));
?????????????sbDeviceInfo.AppendLine(String.Format("??<MarginBottom>{0}cm</MarginBottom>",?FromInchToCM(pageSettings.Margins.Bottom)));
?????????????sbDeviceInfo.AppendLine("</DeviceInfo>");
??????????}
??????????else
??????????{
?????????????string?deviceInfo?=
?????????????"<DeviceInfo>"?+
?????????????"??<OutputFormat>EMF</OutputFormat>"?+
?????????????"??<PageWidth>21cm</PageWidth>"?+
?????????????"??<PageHeight>29.7cm</PageHeight>"?+
?????????????"??<MarginTop>2.5cm</MarginTop>"?+
?????????????"??<MarginLeft>2.5cm</MarginLeft>"?+
?????????????"??<MarginRight>2.5cm</MarginRight>"?+
?????????????"??<MarginBottom>2.5cm</MarginBottom>"?+
?????????????"</DeviceInfo>";
?????????????sbDeviceInfo.AppendLine(deviceInfo);
??????????}
??????????Warning[]?warnings;
??????????m_streams?=?new?List<Stream>();
??????????report.Render("Image",?sbDeviceInfo.ToString(),?new?CreateStreamCallback(CreateStream),?out?warnings);
??????????foreach?(Stream?stream?in?m_streams)
?????????????stream.Position?=?0;
???????}
???????///?<summary>
???????///?當(dāng)前頁(yè)打印的輸出
???????///?</summary>
???????///?<param?name="sender"></param>
???????///?<param?name="ev"></param>
???????private?void?PrintPage(object?sender,?PrintPageEventArgs?ev)
???????{
???????????if?(this.m_duplex)
???????????{//雙面打印
???????????????#region?雙面打印
???????????????//獲取每份頁(yè)數(shù)
???????????????int?pagePerCopy?=?GetPageCountPerCopy(this.m_streams.Count,?this.m_chkedCount);
???????????????if?(this.m_currentPageIndex?>?0?&&?(this.m_currentPageIndex?+?1)?%?pagePerCopy?==?1?&&?this.m_isBlank?&&?pagePerCopy?%?2?!=?0)
???????????????{//輸出空白頁(yè)
???????????????????//當(dāng)前頁(yè)面不是第一頁(yè),且當(dāng)前頁(yè)面為此份報(bào)表第一頁(yè),且每份報(bào)表頁(yè)數(shù)為奇數(shù)
???????????????????Bitmap?emptyImage?=?new?Bitmap(10,?10);
???????????????????if?(ev.PageSettings.Landscape)
???????????????????{
???????????????????????ev.Graphics.DrawImage(emptyImage,?new?Rectangle(0,?0,?ev.PageBounds.Height,?ev.PageBounds.Width));
???????????????????}
???????????????????else
???????????????????{
???????????????????????ev.Graphics.DrawImage(emptyImage,?ev.PageBounds);
???????????????????}
???????????????????//標(biāo)記已經(jīng)打印空白頁(yè)
???????????????????this.m_isBlank?=?false;
???????????????}
???????????????else
???????????????{
???????????????????Metafile?pageImage?=?new?Metafile(m_streams[m_currentPageIndex]);
???????????????????if?(ev.PageSettings.Landscape)
???????????????????{
???????????????????????ev.Graphics.DrawImage(pageImage,?new?Rectangle(0,?0,?ev.PageBounds.Height,?ev.PageBounds.Width));
???????????????????}
???????????????????else
???????????????????{
???????????????????????ev.Graphics.DrawImage(pageImage,?ev.PageBounds);
???????????????????}
???????????????????m_currentPageIndex++;
???????????????????//標(biāo)記等待打印下一個(gè)空白頁(yè)
???????????????????this.m_isBlank?=?true;
???????????????}
???????????????#endregion?雙面打印
???????????}
???????????else
???????????{//單面打印
???????????????#region?單面打印
???????????????Metafile?pageImage?=?new?Metafile(m_streams[m_currentPageIndex]);
???????????????if?(ev.PageSettings.Landscape)
???????????????{
???????????????????ev.Graphics.DrawImage(pageImage,?new?Rectangle(0,?0,?ev.PageBounds.Height,?ev.PageBounds.Width));
???????????????}
???????????????else
???????????????{
???????????????????ev.Graphics.DrawImage(pageImage,?ev.PageBounds);
???????????????}
???????????????m_currentPageIndex++;
???????????????#endregion?單面打印
???????????}
??????????ev.HasMorePages?=?(m_currentPageIndex?<?m_streams.Count);
???????}
???????///?<summary>
???????///?打印開始
???????///?</summary>
???????///?<param?name="sender"></param>
???????///?<param?name="e"></param>
???????private?void?BeginPrint(object?sender,?PrintEventArgs?e)
???????{
??????????this.btnPrint.Enabled?=?false;
???????}
???????///?<summary>
???????///?打印結(jié)束
???????///?</summary>
???????///?<param?name="sender"></param>
???????///?<param?name="e"></param>
???????private?void?EndPrint(object?sender,?PrintEventArgs?e)
???????{
??????????this.btnPrint.Enabled?=?true;
???????}View Code
from:http://www.cnblogs.com/rwecho/archive/2010/04/08/1707507.html?
總結(jié)
以上是生活随笔為你收集整理的LocalReport Print with C# C#打印RDLC的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 第七节 VMware View 6.0
- 下一篇: Hadoop阅读笔记(四)——一幅图看透