欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#利用PrintDocument定制打印单据的小例子

程序员文章站 2023-09-10 09:00:40
前言 本文是利用printdocument定制打印单据的小例子,仅供学习分享使用,如果不足之处,还请指正。 涉及知识点: printdocument :从 w...

前言

本文是利用printdocument定制打印单据的小例子,仅供学习分享使用,如果不足之处,还请指正。

涉及知识点:

  • printdocument :从 windows 窗体应用程序打印时,定义一种可重用的可发送到打印机上的对象。
  • printpreviewcontrol :表示 windows 窗体应用程序打印预览的原始预览部分,没有任何对话框或按钮。
  • graphics :gdi+绘图对象
  • printersettings:设置打印机属性,如:设置属性copies,可以设置打印份数,默认为1,
  • pagesettings:指定应用于单页打印的设置
  • defaultpagesettings:printdocument的属性
  • printpage事件:printdocument的事件,通过此事件来绘制需要打印的内容
  • papersize:指定纸张大小
  • 毫米和英寸的换算:打印机是以英寸为单位的,单据设置是以毫米为单位的,所以需要转换

效果图如下:

C#利用PrintDocument定制打印单据的小例子

核心代码

关键代码如下:

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.drawing.printing;
using system.linq;
using system.text;
using system.threading.tasks;
using system.windows.forms;

namespace demoprint
{
 public partial class mainform : form
 {
  private int width_p = 200;//单位是mm

  private int height_p = 70;//单位是mm

  private int margin_lr = 2;//左右边距

  private int margin_tb = 2;//上下边距

  /// <summary>
  /// 需要打印的内容
  /// </summary>
  public list<printinfo> printinfos { get; set; }

  private printhelper printhelper = new printhelper();

  public mainform()
  {
   initializecomponent();
  }

  private void mainform_load(object sender, eventargs e)
  {
   initinfo();
   initdocument();
  }

  private void initinfo() {
   printersettings printsetting = new printersettings();
   printsetting.printrange = printrange.allpages;

  
   int width_in = mm2inch(width_p);
   int height_in = mm2inch(height_p);
   pagesettings pagesetting = new pagesettings(printsetting);
   pagesetting.papersize = new papersize("customer",width_in, height_in);
   
   int margin_lr_in = mm2inch(margin_lr);
   int margin_tb_in = mm2inch(margin_tb);
   pagesetting.margins = new margins(margin_lr_in, margin_lr_in, margin_tb_in, margin_tb_in);
   this.pdcontrol.defaultpagesettings = pagesetting;
  }

  private void initdocument() {
   list<printinfo> lstprintinfos = new list<printinfo>();
   printinfo p0 = new printinfo()
   {
    prttype = printtype.table,
    prtcolor = color.brown,
    row = int.parse(this.txtrow.text.trim()),
    column = int.parse(this.txtcolumn.text.trim()),
    start = new point(int.parse(this.txtstart.text.trim(new char[] { '(', ')' }).split(',')[0]), int.parse(this.txtstart.text.trim(new char[] { '(', ')' }).split(',')[1])),
    end = new point(int.parse(this.txtend.text.trim(new char[] { '(', ')' }).split(',')[0]), int.parse(this.txtend.text.trim(new char[] { '(', ')' }).split(',')[1]))
    
   };
   lstprintinfos.add(p0);
   printhelper.printinfos = lstprintinfos;
  }

  /// <summary>
  /// 转换毫米到百分之一英寸
  /// </summary>
  /// <param name="mm"></param>
  /// <returns></returns>
  private int mm2inch(int mm) {
   return (int)(mm * 100.0f / 25.4f);
  }

  /// <summary>
  /// 打印开始事件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void pdcontrol_beginprint(object sender, printeventargs e)
  {

  }

  /// <summary>
  /// 打印事件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void pdcontrol_printpage(object sender, printpageeventargs e)
  {
   font font = new font("arial", 14f, fontstyle.regular);
   graphics g = e.graphics;
   g.pagescale = 1;
   g.pageunit = graphicsunit.millimeter;
   //先画一个矩形
   pen linecolor = new pen(color.black, 0.2f);
   g.fillrectangle(brushes.linen,0,0,width_p,height_p);
   printhelper.print(g);
  }

  /// <summary>
  /// 打印结束事件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void pdcontrol_endprint(object sender, printeventargs e)
  {

  }


  /// <summary>
  /// 打印
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void btnprint_click(object sender, eventargs e)
  {
   //打印对话框
   if (this.ptdcontrol.showdialog() == dialogresult.ok)
   {
    this.pdcontrol.print();
   }

  }

  private void lblcolor_click(object sender, eventargs e)
  {
   colordialog f = new colordialog();
   if (f.showdialog() == dialogresult.ok)
   {

    this.lblcolor.backcolor = f.color;
   }
  }

  /// <summary>
  /// 刷新
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void btnrefresh_click(object sender, eventargs e)
  {
   list<printinfo> lstprintinfos = new list<printinfo>();
   //表格配置
   printinfo p0 = new printinfo()
   {
    prttype = printtype.table,
    prtcolor = color.brown,
    row = int.parse(this.txtrow.text.trim()),
    column = int.parse(this.txtcolumn.text.trim()),
    start = new point(int.parse(this.txtstart.text.trim(new char[] { '(', ')' }).split(',')[0]), int.parse(this.txtstart.text.trim(new char[] { '(', ')' }).split(',')[1])),
    end = new point(int.parse(this.txtend.text.trim(new char[] { '(', ')' }).split(',')[0]), int.parse(this.txtend.text.trim(new char[] { '(', ')' }).split(',')[1]))

   };
   lstprintinfos.add(p0);
   //标题配置
   printinfo p1 = new printinfo()
   {
    prttype = printtype.text,
    prtcolor = this.lblcolor.backcolor,
    content = this.txttitle.text.trim(),
    size = int.parse(this.txtsize.text.trim()),
    fontstyle = chkbold.checked ? fontstyle.bold : fontstyle.regular,
    start = new point(int.parse(this.txtlocation.text.trim(new char[] { '(', ')' }).split(',')[0]), int.parse(this.txtlocation.text.trim(new char[] { '(', ')' }).split(',')[1]))
   };
   lstprintinfos.add(p1);
   //内容
   textbox[] t = new textbox[12] { t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12 };
   textbox[] l = new textbox[12] { l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12 };
   for (int i = 0; i < 12; i++)
   {
    printinfo p = new printinfo()
    {
     prttype = printtype.text,
     prtcolor = color.black,
     content = t[i].text.trim(),
     size = 12,
     fontstyle = fontstyle.regular,
     start = new point(int.parse(l[i].text.trim(new char[] { '(', ')' }).split(',')[0]), int.parse(l[i].text.trim(new char[] { '(', ')' }).split(',')[1]))
    };
    lstprintinfos.add(p);
   }
   //打印时间
   printinfo p2 = new printinfo()
   {
    prttype = printtype.text,
    prtcolor = this.lblcolor.backcolor,
    content = datetime.now.tostring("yyyy-mm-dd hh:mm:ss"),
    size =11,
    fontstyle =fontstyle.regular,
    start = new point(145,63)
   };
   lstprintinfos.add(p2);

   printhelper.printinfos = lstprintinfos;
   this.ppvcontrol.invalidatepreview();//刷新文档的预览,重新调用printdocument的print方法
  }
 }
}

源码链接

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。