zoukankan      html  css  js  c++  java
  • winfrom项目的打印

    自己可以下一个PDF打印机(例如下载64位office虚拟打印文档)

    首先要添加控件

    1、添加打印的选项卡,并命名为打印

    2、点击打印选项卡,右击鼠标,选择选择项

    using System;
    using System.Xml;
    using System.Collections;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Printing;

    namespace PubLibrary
    {
    /// <summary>
    /// PrinterClass 的摘要说明。
    /// 需要两个控件
    /// </summary>
    public class PrinterClass
    {
    public int _CNCount = 15;//打印中文时换行
    public int _ENCount = 10;//打印英文时换行
    private SolidBrush drawBrush = new SolidBrush(Color.Black); //颜色
    private ArrayList _ItemList = null; //所打印的字段列表信息
    public PrinterClass()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }

    /// <summary>
    /// 对所打印的字段列表信息进行初始化
    /// </summary>
    /// <param name="billName">所打印单证名称</param>
    /// <param name="s_File">XML文件路径</param>
    public void InitPrinter(string billName, string s_File)
    {
    this._ItemList = new ArrayList();
    XmlDocument xDoc = new XmlDocument();
    xDoc.Load(s_File);
    XmlElement root = xDoc.DocumentElement;
    XmlNode node = root.SelectSingleNode("/rootNode/subNode"); //打印单证名称列表节点
    foreach (XmlNode billNode in node.ChildNodes)
    {
    string s_BillName = billNode.Attributes["name"].Value;

    if (s_BillName == billName) //打印单名
    {
    foreach (XmlNode fieldNode in billNode.ChildNodes)
    {
    if (fieldNode.Attributes == null)
    {
    continue;
    }
    PrintItem pItem = new PrintItem();
    try
    {
    pItem.s_Name = fieldNode.Attributes["name"].Value;
    pItem.s_Color = fieldNode.Attributes["color"].Value;
    pItem.i_Size = Convert.ToInt32(fieldNode.Attributes["size"].Value);
    pItem.i_X = Convert.ToInt32(fieldNode.Attributes["pixX"].Value);
    pItem.i_Y = Convert.ToInt32(fieldNode.Attributes["pixY"].Value);
    pItem.bol_IsPrint = Convert.ToBoolean(fieldNode.Attributes["IsPrint"].Value);
    //if (fieldNode.Attributes["value"] != null)
    //{
    // pItem.s_Value = fieldNode.Attributes["value"].Value;
    //}
    //--
    this._ItemList.Add(pItem);
    }
    catch
    {
    MsgBox.ShowError(pItem.s_Name);
    }
    }
    }
    }
    }

    public void InitPrinterNew(string billName, string s_File)
    {
    this._ItemList = new ArrayList();
    XmlDocument xDoc = new XmlDocument();
    xDoc.Load(s_File);
    XmlElement root = xDoc.DocumentElement;
    XmlNode node = root.SelectSingleNode("/rootNode/subNode"); //打印单证名称列表节点
    foreach (XmlNode billNode in node.ChildNodes)
    {
    string s_BillName = billNode.Attributes["name"].Value;

    if (s_BillName == billName) //打印单名
    {
    foreach (XmlNode fieldNode in billNode.ChildNodes)
    {
    if (fieldNode.Attributes == null)
    {
    continue;
    }
    PrintItem pItem = new PrintItem();
    try
    {
    pItem.s_Name = fieldNode.Attributes["name"].Value;
    pItem.s_Color = fieldNode.Attributes["color"].Value;
    pItem.i_Size = Convert.ToInt32(fieldNode.Attributes["size"].Value);
    pItem.i_X = Convert.ToInt32(fieldNode.Attributes["pixX"].Value);
    pItem.i_Y = Convert.ToInt32(fieldNode.Attributes["pixY"].Value);
    pItem.bol_IsPrint = Convert.ToBoolean(fieldNode.Attributes["IsPrint"].Value);
    if (fieldNode.Attributes["value"] != null)
    {
    pItem.s_Value = fieldNode.Attributes["value"].Value;
    }
    //--
    this._ItemList.Add(pItem);
    }
    catch
    {
    MsgBox.ShowError(pItem.s_Name);
    }
    }
    }
    }
    }
    /// <summary>
    /// 将打印字段列表信息一一打印出来
    /// </summary>
    /// <param name="e"></param>
    public void Printer(System.Drawing.Printing.PrintPageEventArgs e)
    {
    Font drawFont = null;

    for (int i = 0; i < this._ItemList.Count; i++)
    {
    PrintItem pI = (PrintItem)this._ItemList[i];
    if (pI.bol_IsPrint)
    {
    //drawFont = new Font(string.Empty, pI.i_Size, FontStyle.Bold);
    drawFont = new Font(pI.s_FontName, pI.i_Size, pI.fStye);
    e.Graphics.DrawString(pI.s_Value.ToString(), drawFont, drawBrush, pI.i_X, pI.i_Y);
    }
    }
    }

    /// <summary>
    /// 打印表格线条
    /// </summary>
    /// <param name="sP"></param>
    /// <param name="eP"></param>
    /// <param name="e"></param>
    public void DrawLine(PrintItem sP, PrintItem eP, System.Drawing.Printing.PrintPageEventArgs e)
    {
    if (sP == null)
    {
    MsgBox.ShowError(sP.s_Name + "不存在!");
    return;
    }
    if (eP == null)
    {
    MsgBox.ShowError(eP.s_Name + "不存在!");
    return;
    }
    e.Graphics.DrawLine(new Pen(Brushes.Black, 1), new Point(sP.i_X, sP.i_Y), new Point(eP.i_X, eP.i_Y));
    }

    public void DrawLine(PrintItem sP, PrintItem eP, int LineBold, System.Drawing.Printing.PrintPageEventArgs e)
    {
    if (sP == null)
    {
    MsgBox.ShowError(sP.s_Name + "不存在!");
    return;
    }
    if (eP == null)
    {
    MsgBox.ShowError(eP.s_Name + "不存在!");
    return;
    }
    e.Graphics.DrawLine(new Pen(Brushes.Black, LineBold), new Point(sP.i_X, sP.i_Y), new Point(eP.i_X, eP.i_Y));
    }

    /// <summary>
    /// 根据字段名称,获取该字段信息
    /// </summary>
    /// <param name="s_Name">字段名称</param>
    /// <returns></returns>
    public PrintItem getPrintItem(string s_Name)
    {
    PrintItem pI = null;
    for (int i = 0; i < this._ItemList.Count; i++)
    {
    pI = (PrintItem)this._ItemList[i];
    if (s_Name == pI.s_Name)
    {
    return pI;
    }
    }
    return null;
    }

    /// <summary>
    /// 获取换行后的字符串
    /// </summary>
    /// <param name="s_Text"></param>
    /// <param name="i_Count">如果为中文则为15,英文则为10</param>
    /// <param name="i_Width"></param>
    /// <returns></returns>
    public string GetPrintText(string s_Text, int i_Count, int i_Width)
    {
    string s_Result = string.Empty;
    int i_Len = 0;
    foreach (char c in s_Text)
    {
    s_Result += c.ToString();
    i_Len++;
    if (i_Len * i_Count > i_Width)
    {
    s_Result += " ";
    i_Len = 0;
    }
    }

    return s_Result;
    }

    /// <summary>
    /// 获取换行后的字符串
    /// </summary>
    /// <param name="s_Text"></param>
    /// <param name="i_Count">如果为中文则为15,英文则为10</param>
    /// <param name="i_Width"></param>
    /// <returns></returns>
    public string GetPrintTextSim(string s_Text, int i_Count, int i_Width)
    {
    string s_Result = string.Empty;
    int i_Len = 0;
    foreach (char c in s_Text)
    {
    s_Result += c.ToString();
    i_Len++;
    if (i_Len * i_Count > i_Width)
    {
    s_Result += "..";
    break;
    }
    }

    return s_Result;
    }
    /// <summary>
    /// 打印表格字符
    /// </summary>
    /// <param name="sP"></param>
    /// <param name="eP"></param>
    /// <param name="e"></param>
    public void DrawText(PrintItem pI, System.Drawing.Printing.PrintPageEventArgs e)
    {
    Font drawFont = null;
    if (pI == null)
    {
    MsgBox.ShowError(pI.s_Name + "不存在!");
    return;
    }
    if (pI == null)
    {
    MsgBox.ShowError(pI.s_Name + "不存在!");
    return;
    }
    drawFont = new Font(string.Empty, pI.i_Size, FontStyle.Bold);
    e.Graphics.DrawString(pI.s_Value.ToString(), drawFont, drawBrush, pI.i_X, pI.i_Y);
    // drawFont = new Font(string.Empty, pI.i_Size, FontStyle.Bold);
    //e.Graphics.DrawString((new Pen(Brushes.Black, 1), new Point(sP.i_X, sP.i_Y), new Point(eP.i_X, eP.i_Y));
    }
    /// <summary>
    /// 获取换行后的字符串
    /// </summary>
    /// <param name="s_Text"></param>
    /// <param name="eP"></param>
    /// <param name="sP"></param>
    /// <returns></returns>
    public string GetPrintText(string s_Text, PrintItem eP, PrintItem sP, System.Drawing.Printing.PrintPageEventArgs e)
    {
    string s_Result = string.Empty;
    string s_curRowText = string.Empty;

    Font curFont = new Font(string.Empty, eP.i_Size, FontStyle.Bold);
    int i_RowWidth = eP.i_X - sP.i_X;
    int i_Position = 0;
    foreach (char c in s_Text)
    {
    s_curRowText += c.ToString();
    int i_Length = (int)e.Graphics.MeasureString(s_curRowText, curFont).Width;
    if (i_Length > i_RowWidth)
    {
    s_Result += s_curRowText.Substring(0, i_Position) + " ";
    s_curRowText = c.ToString();
    i_Position = 0;
    }
    i_Position++;
    }
    s_Result += s_curRowText;

    return s_Result;
    }

    /// <summary>
    /// 获取换行后的字符串
    /// </summary>
    /// <param name="s_Text"></param>
    /// <param name="eP"></param>
    /// <param name="sP"></param>
    /// <returns></returns>
    public string GetPrintText(string s_Text, int i_Width, System.Drawing.Printing.PrintPageEventArgs e)
    {
    string s_Result = string.Empty;
    string s_curRowText = string.Empty;

    Font curFont = new Font(string.Empty, 8, FontStyle.Bold);
    int i_RowWidth = i_Width;
    int i_Position = 0;
    foreach (char c in s_Text)
    {
    s_curRowText += c.ToString();
    int i_Length = (int)e.Graphics.MeasureString(s_curRowText, curFont).Width;
    if (i_Length > i_RowWidth)
    {
    s_Result += s_curRowText.Substring(0, i_Position) + " ";
    s_curRowText = c.ToString();
    i_Position = 0;
    }
    i_Position++;
    }
    s_Result += s_curRowText;

    return s_Result;
    }


    public void ClearPrintItem()
    {
    for (int i = 0; i < this._ItemList.Count; i++)
    {
    PrintItem pI = (PrintItem)this._ItemList[i];
    if (pI.bol_IsPrint)
    {
    pI.s_Value = string.Empty;
    }
    }
    }

    public void PrintImage(Image img, PrintItem pI, float w, float h, System.Drawing.Printing.PrintPageEventArgs e)
    {
    if (pI == null)
    {
    MsgBox.ShowError(pI.s_Name + "不存在!");
    return;
    }

    //e.Graphics.DrawImageUnscaled(img, pI.i_X, pI.i_Y);
    e.Graphics.DrawImage(img, pI.i_X, pI.i_Y, w, h);
    }

    /// <summary>
    /// 获取格子的宽度
    /// </summary>
    /// <param name="sP"></param>
    /// <param name="eP"></param>
    /// <returns></returns>
    public int GetCellWidth(PrintItem eP, PrintItem sP)
    {
    return eP.i_X - sP.i_X;
    }

    #region Ext test function
    public bool _isChgPos = false;
    public void PrinterEx(string itemName, System.Drawing.Printing.PrintPageEventArgs e)
    {
    Font drawFont = null;

    for (int i = 0; i < this._ItemList.Count; i++)
    {
    PrintItem pI = (PrintItem)this._ItemList[i];
    if ((pI.bol_IsPrint) && (pI.s_Name == itemName))
    {
    drawFont = new Font(string.Empty, pI.i_Size, FontStyle.Bold);
    if (this._isChgPos)
    e.Graphics.DrawString(pI.s_Value.ToString(), drawFont, drawBrush, pI.i_X, pI.realY);
    else
    e.Graphics.DrawString(pI.s_Value.ToString(), drawFont, drawBrush, pI.i_X, pI.i_Y);

    break;
    }
    }
    }
    #endregion
    }

    /// <summary>
    /// 打印信息
    /// </summary>
    public class PrintItem
    {
    public string s_Name = string.Empty; //字段名称
    public string s_Value = string.Empty;//字段值
    public string s_Color = string.Empty;//颜色
    public int i_Size = -1;//字体大小
    public int i_X = -1; //位置坐标X值
    public int i_Y = -1; //位置会标Y值
    // public int realX = -1;
    public int realY = -1;
    public bool bol_IsPrint = true; //是否将该字段打印出来

    public string s_FontName = string.Empty; //字体名称
    public FontStyle fStye = FontStyle.Bold; //字体风格
    }
    }

  • 相关阅读:
    7.21 高博教育 数组 内存
    【基础扎实】Python操作Excel三模块
    PAT 甲级 1012 The Best Rank
    PAT 甲级 1011  World Cup Betting
    PAT 甲级 1010 Radix
    链式线性表——实验及提升训练
    循环程序设计能力自测
    链表应用能力自测
    PAT 甲级 1009 Product of Polynomials
    1008 Elevator (20分)
  • 原文地址:https://www.cnblogs.com/zhudezhiwansui/p/6398014.html
Copyright © 2011-2022 走看看