using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Reporting.WinForms; using Microsoft.Reporting; using System.IO; using System.Drawing; using System.Drawing.Drawing2D; using System.Data; namespace BatchService.Framework.Utility { public class EMFPrinter : IDisposable { public enum PrintType { PDF, Image, Print, } private LocalReport _report = new LocalReport(); private IList _streams = new List(); private int _emf_Width = 25; private int _emf_Height = 24; private int _emf_Top = 0; private int _emf_Left = 0; private int _emf_Bottom = 0; private int _emf_Right = 0; public EMFPrinter(string RdlcPath) : this(RdlcPath, 25, 24, 0, 0, 0, 0) { } public EMFPrinter(string RdlcPath, int EMFWidth, int EMFHeight, int EMFTop, int EMFLeft, int EMFBottom, int EMFRight) { _report.ReportPath = RdlcPath; _report.EnableExternalImages = true; _report.EnableHyperlinks = true; _emf_Width = EMFWidth; _emf_Height = EMFHeight; _emf_Top = EMFTop; _emf_Left = EMFLeft; _emf_Bottom = EMFBottom; _emf_Right = EMFRight; } public void AddParameter(string name, string value) { ReportParameter para = new ReportParameter(name); para.Values.Add(value); _report.SetParameters(para); } public void AddDataSource(string tableName, DataTable tableInfo) { ReportDataSource source = new ReportDataSource(tableName, tableInfo); _report.DataSources.Add(source); } private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek) { Stream stream = new MemoryStream(); _streams.Add(stream); return stream; } public byte[] Print(PrintType printType) { _report.Refresh(); string deviceInfo = string.Format( "" + " EMF" + " {0}cm" + " {1}cm" + " {2}cm" + " {3}cm" + " {4}cm" + " {5}cm" + "", _emf_Width, _emf_Height, _emf_Top, _emf_Left, _emf_Right, _emf_Bottom); Warning[] warnings; _report.Render(printType.ToString(), deviceInfo, CreateStream, out warnings); foreach (Stream stream in _streams) { stream.Position = 0; } if (_streams == null || _streams.Count == 0) { return null; } else { return _streams[0].ToByteArray(); } } public void Dispose() { if (_streams.Count > 0) { foreach (Stream stream in _streams) stream.Close(); _streams.Clear(); } } void IDisposable.Dispose() { _report.Dispose(); GC.SuppressFinalize(this); } } }