当前位置:编程学习 > C#/ASP.NET >>

封装一个打印的公共类,跟童鞋们分享一下

ImagePrinter 类是封装好的打印类,打印前需要调用 public bool PrinterReady(Bitmap printContent) 方法传入一个图片文件,
之后就可以调用 PrintView() 进行打印浏览,调用 PrintOut()  就可以打印了。
ExpressPrinter 类是调用 ImagePrinter 打印封装类的一个实例,旨在与看看怎么使用GDI+绘制需要打印的内容和字体。
================================================
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
using Lib.Biz.ClientService;
using Lib.Utility.Extension;
using PrintOrder.Utility;
 
 
namespace PrintOrder.ExpressPrinter.PrintDoc
{
  /// <summary>
  /// 打印图片内容
  /// </summary>
  public class ExpressPrinter
  {
    /// <summary>
    /// 创建快递单打印对象实例
    /// </summary>
    /// <param name="printerName">打印机名称,如果为空将使用默认打印机</param>
    /// <param name="template">打印模板</param>
    /// <param name="items">打印项</param>
    /// <param name="isCoordinateRule">是否打印定位标尺</param>
    public ExpressPrinter(string printerName, Print_Template template, List<Print_Item> items, bool isCoordinateRule)
    {
      m_template = template;
      m_items = items;
      m_print = new ImagePrinter(printerName);
      m_isCoordinateRule = isCoordinateRule;
      InitPrinter();
    }
    #region 变量
 
 
    private Print_Template m_template;
    private List<Print_Item> m_items;
    private Bitmap m_bitmap = new Bitmap(10, 10);
    private Brush m_brush = new SolidBrush(Color.Black);
    private ImagePrinter m_print = null;
    private bool m_isCoordinateRule = false;
    #endregion
    /// <summary>
    /// 设置纸张大小
    /// </summary>
    private void SetPrintPageSize()
    {
      //使用百分之一英寸为单位重新创建绘图图面
      using (Graphics g = Graphics.FromImage(m_bitmap))
      {
        //注:使用百分之一为单位
        SizeF sizeF = new SizeF(
         CommonMethod.PixelsToInches(m_template.Width,g.DpiX)*100,
         CommonMethod.PixelsToInches(m_template.Height,g.DpiY)*100);
        Size size = Size.Ceiling(sizeF);
        m_print.DefaultPaperSize = new PaperSize("NewPage", size.Width,size.Height);
      }
      m_print.DefaultMargins = new Margins(0, 0, 0, 0);
      m_print.PrintDocName = string.Format("{0}({1})",m_template.TemplateName,m_template.Modifier);
    }
    /// <summary>
    /// 打印前初始化
    /// </summary>
    private void InitPrinter()
    {
      SetPrintPageSize(); //设置纸张大小
      m_bitmap = new Bitmap(m_template.Width, m_template.Height);
      Func<Print_Item, Font> getFont = (o =>
      {
        FontStyle fontStyle = FontStyle.Regular;
        if (o.FontBold == 1)
          fontStyle = fontStyle | FontStyle.Bold;
        if (o.FontItalic == 1)
          fontStyle = fontStyle | FontStyle.Italic;
        if (o.FontStrikeout == 1)
          fontStyle = fontStyle | FontStyle.Strikeout;
        if (o.FontUnderline == 1)
          fontStyle = fontStyle | FontStyle.Underline;
        Font font = new Font(o.FontName, o.FontSize, fontStyle);
        return font;
      });
      using (Graphics g = Graphics.FromImage(m_bitmap))
      {
        g.PageUnit = GraphicsUnit.Pixel;
        //填充数据到图片模板(位置要在制作图片模板的时候度量好)
        g.FillRectangle(Brushes.White, 0f, 0f
          ,m_template.Width
          ,m_template.Height);
        //测试纸张大小
        //this.DrawTestRectangle(g);
        if (m_isCoordinateRule)
        {
          TestPageSize(g);
        }
        foreach (Print_Item item in m_items)
        {
          g.DrawString(
            item.Text
            , getFont(item)
            , Brushes.Black
            , new RectangleF(
               item.Offset_X + m_template.Offset_X   //设置全局打印偏移 X 坐标
              ,item.Offset_Y + m_template.Offset_Y //设置全局打印偏移 Y 坐标
              ,item.Width
              ,item.Height));
        }
        g.Dispose();
      }
      //m_bitmap.Save(@"D:\WorkTest\temp\express.jpg");
      m_print.PrinterReady(m_bitmap);
    }
 
 
    private void DrawTestRectangle(Graphics g)
    {
      g.PageUnit = GraphicsUnit.Pixel;
      SizeF size = new SizeF(
        Utility.CommonMethod.MillimetersToPixel(30, g.DpiX)
        , Utility.CommonMethod.MillimetersToPixel(30, g.DpiY));
      g.DrawRectangle(new Pen(Color.Black, 1), 50, 50, size.Width, size.Height);
    }
    /// <summary>
    /// 打印标尺
    /// </summary>
    private void TestPageSize(Graphics g)
    {
   
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,