飞/可爱朋 发表于 2014-10-15 15:50:22

winForm和WPF通用打印类,只需要将数据重新处理传递即可

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Media;
using System.Drawing.Printing;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using System.IO;
using System.Collections;
using System.Drawing.Design;
using System.Drawing;
using System.Management;

namespace Bll.Printer_Bll
{
    public class PrintManager
    {
      /// <summary>
      /// 构造接受txt完整数据的字符数组
      /// </summary>
      public string[] TxtString;

      /// <summary>
      /// 实例化打印文档
      /// </summary>
      PrintDocument printTxt = new PrintDocument();

      /// <summary>
      /// 打印页的纸张大小
      /// </summary>
      public string PaperSize;

      /// <summary>
      /// 打印的纸张来源
      /// </summary>
      public string PaperSource;

      /// <summary>
      /// 打印的方向,纵向,横向
      /// </summary>
      public bool Landscape = false;

      /// <summary>
      /// 设置的打印机的名称
      /// </summary>
      public string PrinterName;

      /*与页面的编辑,左,右,上,下*/
      public int MarginLeft = 0;
      public int MarginRight = 0;
      public int MarginTop = 0;
      public int MarginBottom = 0;

      public PrintManager(string TxtPath)
      {
            //将txt文档的数据加载出来
            this.TxtString = ReadTxtDocument(TxtPath);
            this.PrinterName = "GP-58N Series";
            this.PaperSize = "Gprinter 58(48) x 210 mm";
            this.PaperSource = "自动选择";

            TxtDocumentPrint();
      }

      /// <summary>
      /// 调用win api将指定名称的打印机设置为默认打印机
      /// </summary>
      class Externs
      {
            

            //调用win api将指定名称的打印机设置为默认打印机
            public static extern bool SetDefaultPrinter(String Name);
      }
      /// <summary>
      /// 文档txt打印方法
      /// </summary>
      public void TxtDocumentPrint()
      {
            //设置打印机为默认
            if (Externs.SetDefaultPrinter(PrinterName) == true)
            {
                //初始化两个打印参数
                System.Drawing.Printing.PaperSize PrintPaperSize = null;
                System.Drawing.Printing.PaperSource PrintPaperSource = null;

                //获取打印机支持的纸张种类集合
                PrinterSettings.PaperSizeCollection PaperSizes = printTxt.PrinterSettings.PaperSizes;

                //获取打印机的进纸类型集合
                PrinterSettings.PaperSourceCollection PaperSources = printTxt.PrinterSettings.PaperSources;

                //遍历纸张种类集合,根据输入设置,选择纸张类型
                for (int i = 0; i < PaperSizes.Count; i++)
                {
                  //匹配成功,将当前纸张类型设置为打印纸张
                  if (PaperSize == PaperSizes.PaperName)
                  {
                        PrintPaperSize = PaperSizes;
                        break;
                  }
                }

                //遍历进纸类型,根据输入设置,选择进纸类型
                for (int j = 0; j < PaperSources.Count; j++)
                {
                  //匹配成功,将当前进纸类型设置为进纸类型
                  if (PaperSource == PaperSources.SourceName)
                  {
                        PrintPaperSource = PaperSources;
                        break;
                  }
                }

                if (PrintPaperSize != null && PrintPaperSource != null)
                {
                  //设置打印页面
                  PrintSettings(printTxt, PrintPaperSize, PrintPaperSource, true, PrinterName, 10, 10, 10, 10);

                  //绘制打印页面
                  printTxt.PrintPage += new PrintPageEventHandler(DrawImage);

                  //打印
                  printTxt.Print();
                }
            }
      }

      /// <summary>
      /// 读取txt文件,将文件转为string[]对象类型集合
      /// </summary>
      /// <param name="TxtPath">txt文件的路径</param>
      /// <returns>转换完成的string[]</returns>
      public string[] ReadTxtDocument(string TxtPath)
      {
            try
            {
                //创建txt文档读取对象和数据存储List对象
                StreamReader ReadTxt = new StreamReader(TxtPath, System.Text.Encoding.Default);
                System.Collections.ArrayList TxtData = new System.Collections.ArrayList();

                //循环读取txt文档的所有内容
                //使用arraylist的目的是读取完整的txt文档
                while (ReadTxt.Peek() != -1)
                {
                  TxtData.Add(ReadTxt.ReadLine());
                }

                //将ArrayList对象转为string[]对象
                string[] TempString = (string[])(TxtData.ToArray(typeof(string)));

                //关闭读取对象,清空缓冲区
                ReadTxt.Close();

                return TempString;
            }
            catch (Exception)
            {
                throw;
            }
      }

      /// <summary>
      /// 对调取的打印机进行文档的打印设置
      /// </summary>
      /// <param name="printTxt">需要打印的文档</param>
      /// <param name="PaperName">打印的页面的纸张大小,eg:A4</param>
      /// <param name="SourceName">设置打印文档的纸张来源,eg:上层纸盒</param>
      /// <param name="Landscape">设置打印文档的打印方向,eg:纵向</param>
      /// <param name="PrinterName">设置需要打印该文档的打印机</param>
      /// <param name="MarginLeft">打印页左边距,单位:英寸</param>
      /// <param name="MarginRight">打印页右边距,单位:英寸</param>
      /// <param name="MarginTop">打印页顶部边距,单位:英寸</param>
      /// <param name="MarginBottom">打印页底部边距,单位:英寸</param>
      public void PrintSettings(PrintDocument printTxt, PaperSize PaperName, PaperSource SourceName, bool Landscape, string PrinterName, int MarginLeft, int MarginRight, int MarginTop, int MarginBottom)
      {
            //设置打印的页面的纸张大小
            //eg:A4
            printTxt.DefaultPageSettings.PaperSize = PaperName;

            //设置打印文档的纸张来源
            // eg:上层纸盒
            printTxt.DefaultPageSettings.PaperSource = SourceName;

            //设置打印文档的打印方向
            // eg:纵向
            printTxt.DefaultPageSettings.Landscape = Landscape;

            //设置需要打印的打印机的名称
            printTxt.PrinterSettings.PrinterName = PrinterName;

            //设置打印文档的页边距
            printTxt.DefaultPageSettings.Margins = new Margins(MarginLeft, MarginRight, MarginTop, MarginBottom);
      }

      /// <summary>
      /// 将txt文档的数据draw到画布上
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="PaintData"></param>
      public void DrawImage(object sender,PrintPageEventArgs PaintData)
      {
            string[] TempString = TxtString;
            for (int i = 0; i < TempString.Length; i++)
            {
                string drawString = TempString;
                // Create font and brush.
                Font drawFont = new Font("Arial", 16);
                SolidBrush drawBrush = new SolidBrush(Color.Black);

                // Create rectangle for drawing.
                float x = 150.0F;
                float y = 150.0F;
                float width = 200.0F;
                float height = 50.0F;
                RectangleF drawRect = new RectangleF(x, y, width, height);

                // Draw rectangle to screen.
                Pen blackPen = new Pen(Color.Black);
                PaintData.Graphics.DrawRectangle(blackPen, x, y, width, height);

                // Set format of string.
                StringFormat drawFormat = new StringFormat();
                drawFormat.Alignment = StringAlignment.Center;

                // Draw string to screen.
                PaintData.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect, drawFormat);
            }

            
      }
    }

}

≮转折┱徘徊 发表于 2014-10-15 16:04:07

飞P,你总是那么的有魅力!

飞/可爱朋 发表于 2014-10-15 16:08:35

≮转折┱徘徊 发表于 2014-10-15 16:04
飞P,你总是那么的有魅力!

你大爷的二条

ibcadmin 发表于 2014-10-15 16:34:06

- -   跟1楼认识啊?

飞/可爱朋 发表于 2014-10-16 09:19:26

ibcadmin 发表于 2014-10-15 16:34
- -   跟1楼认识啊?

:lol认识

ibcadmin 发表于 2014-10-16 13:46:11

飞/可爱朋 发表于 2014-10-16 01:19
认识

那好啊多拉点人来IBC

ching 发表于 2014-12-24 14:08:14

哈哈哈
页: [1]
查看完整版本: winForm和WPF通用打印类,只需要将数据重新处理传递即可