StreamPrintDocument.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using System.IO;
using System.Drawing.Printing;
using System.Drawing.Imaging;

namespace FB.PrintHelper
{
    public partial class StreamPrintDocument : System.Drawing.Printing.PrintDocument
    {
        private int m_CurrentPageIndex;

        private IList m_EMFStreams;
        private Microsoft.Reporting.WinForms.LocalReport m_LocalReport = null;
        private System.Drawing.Imaging.Metafile m_PageImage = null;
        private string m_ErrorMessage = string.Empty;//错误信息
        private string print_name = null;//打印机名称
        private string paper_name = "PlaneTicket";//纸张名称
        private float paper_width = 210.1F;
        private float paper_height = 112.1F;
        public string ErrorMessage
        {
            get
            {
                return this.m_ErrorMessage;
            }
            set
            {
                this.m_ErrorMessage = value;
            }
        }
        /// 
        /// 构造函数
        /// 
        /// 
        /// 
        public StreamPrintDocument(Microsoft.Reporting.WinForms.LocalReport localReport)
            : base()
        {

            string strMessage = string.Empty;
            print_name = Printer.GetDeaultPrinterName();

            if (this.ReadSetting(out strMessage) == null)
                this.m_ErrorMessage = strMessage;

            if (this.m_ErrorMessage == string.Empty)
            {
                this.m_LocalReport = localReport;
            }
        }
        /// 
        /// 设置打印配置,如有错误返回错误信息
        /// 
        /// 
        /// 
        private DeviceInfo ReadSetting(out string WarningMessage)
        {

            WarningMessage = string.Empty;
            DeviceInfo emfdi = new DeviceInfo();
            base.PrinterSettings.PrinterName = print_name;
            //没有指定的纸张配置
            if (!Printer.FormInPrinter(print_name, paper_name))
            {
                Printer.AddCustomPaperSize(print_name, paper_name, paper_width, paper_height);
            }
            //与指定的纸张大小不匹配
            if (!Printer.FormSameSize(print_name, paper_name, System.Convert.ToDecimal(paper_width/10), System.Convert.ToDecimal(paper_height/10)))
            {
                Printer.DeleteCustomPaperSize(print_name, paper_name);
                Printer.AddCustomPaperSize(print_name, paper_name, paper_width, paper_height);
            }
            //设置将打印的纸张大小
            foreach (System.Drawing.Printing.PaperSize ps in base.PrinterSettings.PaperSizes)
            {
                if (ps.PaperName == paper_name)
                {
                    base.PrinterSettings.DefaultPageSettings.PaperSize = ps;
                    base.DefaultPageSettings.PaperSize = ps;
                    break;
                }
            }
            //DefaultPageSettings.Landscape如果页面应横向打印,则为 true;反之,则为 false。默认值由打印机决定。
            base.DefaultPageSettings.Landscape = false;
            base.PrinterSettings.DefaultPageSettings.Landscape = false;
            emfdi.Landscape = false;

            this.bolOrientation = emfdi.Landscape;

            emfdi.PageWidth = System.Convert.ToDecimal(paper_width);
            emfdi.PageHeight = System.Convert.ToDecimal(paper_height);
            emfdi.MarginTop = System.Convert.ToDecimal(0);//margin  top
            emfdi.MarginBottom = System.Convert.ToDecimal(0);//margin bottom
            emfdi.MarginLeft = System.Convert.ToDecimal(0);//margin left
            emfdi.MarginRight = System.Convert.ToDecimal(0);//margin right

            base.PrinterSettings.PrinterName = print_name;
            return emfdi;
        }

        private bool bolOrientation = false;

        private Stream CreateStream(string Name, string FileNameExtension, Encoding Encoding, string MimeType, bool WillSeek)
        {
            System.IO.Stream streamRet = new System.IO.MemoryStream();
            this.m_EMFStreams.Add(streamRet);
            return streamRet;
        }

        private void Export(LocalReport Report)
        {
            string strMessage = string.Empty;

            DeviceInfo emfdi = this.ReadSetting(out strMessage);

            string strDeviceInfo = emfdi.DeviceInfoString;

            emfdi = null;

            Microsoft.Reporting.WinForms.Warning[] Warnings;

            this.m_EMFStreams = new System.Collections.Generic.List();

            Report.Render("Image", strDeviceInfo, this.CreateStream, out Warnings);

            foreach (System.IO.Stream s in this.m_EMFStreams)
                s.Position = 0;


        }

        protected override void OnBeginPrint(PrintEventArgs ev)
        {
            base.OnBeginPrint(ev);
        }

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

            if (this.m_EMFStreams == null || this.m_EMFStreams.Count == 0)
            {
                this.Export(this.m_LocalReport);
                this.m_CurrentPageIndex = 0;
                if (this.m_EMFStreams.Count == 0 || this.m_EMFStreams == null)
                    return;
            }

            this.m_PageImage = new Metafile(this.m_EMFStreams[this.m_CurrentPageIndex]);

            ev.Graphics.DrawImageUnscaledAndClipped(this.m_PageImage, ev.PageBounds);

            this.m_CurrentPageIndex++;

            ev.HasMorePages = (this.m_CurrentPageIndex < this.m_EMFStreams.Count);

            if (this.m_CurrentPageIndex == this.m_EMFStreams.Count)
            {
                this.m_CurrentPageIndex = 0;
                this.m_EMFStreams.Clear();
                this.m_EMFStreams = null;
                this.m_PageImage.Dispose();
                base.Dispose();
            }

        }

    }
}


 
 

你可能感兴趣的:(报表打印)