代码
/// <summary> /// 打印帮助类 /// </summary> public class PrintHelper { private int m_currentPageIndex; private IList<Stream> m_streams; /// <summary> /// 报表直接打印 /// </summary> /// <param name="reportPath">报表文件路径</param> /// <param name="printerName">打印机名称</param> /// <param name="dt">DataTable</param> /// <param name="sourceName">rdlc的数据集名称</param> /// <param name="paraList">参数列表</param> public void Run(string reportPath, string printerName, DataTable dt, string sourceName, List<ReportParameter> paraList) { LocalReport report = new LocalReport(); report.ReportPath = reportPath; report.DataSources.Add(new ReportDataSource(sourceName,dt)); report.EnableExternalImages = true; report.SetParameters(paraList); Export(report); m_currentPageIndex = 0; Print(printerName); } private void Export(LocalReport report) { string deviceInfo = "<DeviceInfo>" + " <OutputFormat>EMF</OutputFormat>"+ "</DeviceInfo>"; Warning[] warnings; m_streams = new List<Stream>(); try { report.Render("Image", deviceInfo, CreateStream, out warnings); } catch (Exception ex) { Exception innerEx = ex.InnerException; while (innerEx != null) { string errmessage = innerEx.Message; innerEx = innerEx.InnerException; } } foreach (Stream stream in m_streams) { stream.Position = 0; } } private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek) { Stream stream = new FileStream(name + DateTime.Now.Millisecond + "." + fileNameExtension, FileMode.Create); m_streams.Add(stream); return stream; } private void Print(string printerName) { if (m_streams == null || m_streams.Count == 0) return; PrintDocument printDoc = new PrintDocument(); if (printerName.Length > 0) { printDoc.PrinterSettings.PrinterName = printerName; } foreach (PaperSize ps in printDoc.PrinterSettings.PaperSizes) { if (ps.PaperName == "A4") { printDoc.PrinterSettings.DefaultPageSettings.PaperSize = ps; printDoc.DefaultPageSettings.PaperSize = ps; } } if (!printDoc.PrinterSettings.IsValid) { string msg = string.Format("找不到打印机:{0}",printerName); LogUtil.Log(msg); return; } printDoc.PrintPage += new PrintPageEventHandler(PrintPage); printDoc.Print(); } private void PrintPage(object sender, PrintPageEventArgs ev) { Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]); ev.Graphics.DrawImage(pageImage, 0, 0, 827, 1169);//像素 m_currentPageIndex++; ev.HasMorePages = (m_currentPageIndex < m_streams.Count); } }
说明
打印格式
string deviceInfo = "<DeviceInfo>" + " <OutputFormat>EMF</OutputFormat>" + " <PageWidth>210mm</PageWidth>" + " <PageHeight>297mm</PageHeight>" + " <MarginTop>5mm</MarginTop>" + " <MarginLeft>10mm</MarginLeft>" + " <MarginRight>10mm</MarginRight>" + " <MarginBottom>5mm</MarginBottom>" + "</DeviceInfo>";//这里是设置打印的格式 边距什么的
关于OutputFormat:
http://support.supermap.com.cn/DataWarehouse/WebDocHelp/6.1.3/iserverOnlineHelp/mergedProjects/iServerJavadoc/com/supermap/services/components/commontypes/OutputFormat.html
参考文章
http://www.cnblogs.com/bfyx/p/3279385.html (详细)
http://blog.csdn.net/moshuchao/article/details/2607017
http://www.cnblogs.com/qiuweiguo/archive/2011/08/26/2154706.html
时间: 2024-10-18 13:32:17