服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#利用PrintDocument定制打印单据的小例子

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

2022-07-24 17:45Alan.hsiang C#

这篇文章主要给大家介绍了关于C#利用PrintDocument定制打印单据的小例子,文中通过示例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

前言

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

涉及知识点:

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

效果图如下:

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

核心代码

关键代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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方法
  }
 }
}

源码链接

总结

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

原文链接:https://www.cnblogs.com/hsiang/p/6921817.html

延伸 · 阅读

精彩推荐
  • C#C#实现启用与禁用本地网络的方式小结【3种方式】

    C#实现启用与禁用本地网络的方式小结【3种方式】

    这篇文章主要介绍了C#实现启用与禁用本地网络的方式,结合实例形式总结分析了使用Hnetcfg.dll、Shell32.dll及setupapi.dll三种启用与禁用本地网络的操作方法,需...

    饅頭8722021-11-30
  • C#WPF字体或内容模糊的解决方法

    WPF字体或内容模糊的解决方法

    WPF下开发的程序字体模糊,这个问题或许大家都有遇到过,为了解决WPF字体模糊,查阅了各种资料,结果偶然发现是自己疏忽了一些细节造成的,具体是什...

    王旭9682021-12-13
  • C#C#利用Task实现任务超时多任务一起执行的方法

    C#利用Task实现任务超时多任务一起执行的方法

    这篇文章主要给大家介绍了关于C#利用Task实现任务超时,多任务一起执行的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定...

    change_4_now11292022-02-16
  • C#C#如何通过probing指定dll寻找文件夹详解

    C#如何通过probing指定dll寻找文件夹详解

    这篇文章主要给大家介绍了关于C#如何通过probing指定dll寻找文件夹的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考...

    lindexi5532022-03-06
  • C#Silverlight实现跑马灯动画

    Silverlight实现跑马灯动画

    这篇文章主要为大家详细介绍了Silverlight实现跑马灯动画,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    拉斐尔-Raphael11392022-02-25
  • C#C#简单操作MongoDB的步骤全纪录

    C#简单操作MongoDB的步骤全纪录

    最近花了不少时间研究学习了MongoDB数据库的相关知识,下面这篇文章主要给大家介绍了关于C#简单操作MongoDB的相关资料,文中通过示例代码介绍的非常详细...

    陈惊蛰10922022-03-01
  • C#C# 使用相同权限调用 cmd 传入命令的方法

    C# 使用相同权限调用 cmd 传入命令的方法

    本文告诉大家如何使用相同权限调用cmd并且传入命令,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友参考下吧...

    lindexi8322022-02-25
  • C#C#访问及调用类中私有成员与方法示例代码

    C#访问及调用类中私有成员与方法示例代码

    访问一个类的私有成员不是什么好做法,大家也都知道私有成员在外部是不能被访问的,这篇文章主要给大家介绍了关于C#访问及调用类中私有成员与方法...

    cnc4722022-02-24