马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

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

  [复制链接]
查看11679 | 回复6 | 2014-10-15 15:50:22 | 显示全部楼层 |阅读模式
[C#] 纯文本查看 复制代码
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
        {
            [System.Runtime.InteropServices.DllImport("winspool.drv")]

            //调用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[i].PaperName)
                    {
                        PrintPaperSize = PaperSizes[i];
                        break;
                    }
                }

                //遍历进纸类型,根据输入设置,选择进纸类型
                for (int j = 0; j < PaperSources.Count; j++)
                {
                    //匹配成功,将当前进纸类型设置为进纸类型
                    if (PaperSource == PaperSources[j].SourceName)
                    {
                        PrintPaperSource = PaperSources[j];
                        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[i];
                // 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
飞/可爱朋 | 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
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
飞/可爱朋 | 2014-10-16 09:19:26 | 显示全部楼层
ibcadmin 发表于 2014-10-15 16:34
- -   跟1楼认识啊?

认识
ibcadmin | 2014-10-16 13:46:11 | 显示全部楼层

那好啊  多拉点人来IBC
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
ching | 2014-12-24 14:08:14 | 显示全部楼层
哈哈哈
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则