首页 > 代码库 > 解决RDLC报表自带打印,弹出打印界面后,经常须按两次打印才执行
解决RDLC报表自带打印,弹出打印界面后,经常须按两次打印才执行
这段时间做报表,用RDLC客户老是反应,打印要按几次才可以打印,自己测试了一下,发现果真是存在这个问题,貌似自带的打印是在弹出打印窗口后,第一次点击是先将当前窗体激活,第二次点击才会真正的触发打印事件,查询:http://msdn.microsoft.com/zh-cn/library/ms252091(v=vs.100).aspx
修改了里面几个地方,如下:
using System; using System.IO; using System.Data; using System.Text; using System.Drawing.Imaging; using System.Drawing.Printing; using System.Collections.Generic; using System.Windows.Forms; using Microsoft.Reporting.WinForms; using System.Drawing; namespace YMParking.ParkingMg { public class Print { int m_currentPageIndex; IList<Stream> m_streams; // 摘要: // 为 ReportViewer 控件提供流以进行呈现。 // // 返回结果: // ReportViewer 控件可以写入数据的 Stream 对象。 private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek) { Stream stream = new MemoryStream(); m_streams.Add(stream); return stream; } /// <summary> /// 将报表文件按指定的格式导出 /// </summary> /// <param name="report">报表文件</param> private void Export(LocalReport report) { string deviceInfo = @"<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>"; Warning[] warnings; m_streams = new List<Stream>(); report.Render("Image", deviceInfo, CreateStream, out warnings); foreach (Stream stream in m_streams) stream.Position = 0; } // Handler for PrintPageEvents private void PrintPage(object sender, PrintPageEventArgs ev) { Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]); Rectangle adjustedRect = new Rectangle( ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX, ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY, ev.PageBounds.Width, ev.PageBounds.Height); ev.Graphics.FillRectangle(Brushes.White, adjustedRect); ev.Graphics.DrawImage(pageImage, adjustedRect); m_currentPageIndex++; ev.HasMorePages = (m_currentPageIndex < m_streams.Count); } /// <summary> /// 打印 /// </summary> /// <param name="IsShowPrint">是否弹出打印窗口</param> private void PrintFiel(bool IsShowPrint) { if (m_streams == null || m_streams.Count == 0) throw new Exception("指定的打印的文件错误"); PrintDocument document; if (IsShowPrint) { PrintDialog pd = new PrintDialog(); if (pd.ShowDialog() == DialogResult.OK) { document = new PrintDocument(); pd.Document = document; } else { return; } } else { document = new PrintDocument(); } if (!document.PrinterSettings.IsValid) { throw new Exception("连接打印机出错"); } else { document.PrintPage += new PrintPageEventHandler(PrintPage); m_currentPageIndex = 0; document.Print(); } } public void Run(LocalReport report, bool IsShowPrint) { Export(report); PrintFiel(IsShowPrint); } public void Dispose() { if (m_streams != null) { foreach (Stream stream in m_streams) stream.Close(); m_streams = null; } } } }
写了这个共用的类,重写reportViewer打印事件,
private void reportViewer1_Print(object sender, CancelEventArgs e)
{
e.Cancel = true;
Print pt = new Print();
pt.Run(reportViewer1.LocalReport, true);
}
到此,问题解决了。
本文出自 “温馨梦痕” 博客,请务必保留此出处http://317057112.blog.51cto.com/1361376/1604758
解决RDLC报表自带打印,弹出打印界面后,经常须按两次打印才执行
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。