本篇文章給大家分享的是有關C#中怎么利用PrintDocument定制打印單據,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
站在用戶的角度思考問題,與客戶深入溝通,找到睢陽網站設計與睢陽網站推廣的解決方案,憑借多年的經驗,讓設計與互聯網技術結合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網站建設、做網站、企業(yè)官網、英文網站、手機端網站、網站推廣、域名與空間、雅安服務器托管、企業(yè)郵箱。業(yè)務覆蓋睢陽地區(qū)。
涉及知識點:
PrintDocument :從 Windows 窗體應用程序打印時,定義一種可重用的可發(fā)送到打印機上的對象。
PrintPreviewControl :表示 Windows 窗體應用程序打印預覽的原始預覽部分,沒有任何對話框或按鈕。
Graphics :GDI+繪圖對象
PrinterSettings:設置打印機屬性,如:設置屬性Copies,可以設置打印份數,默認為1,
PageSettings:指定應用于單頁打印的設置
DefaultPageSettings:PrintDocument的屬性
PrintPage事件:PrintDocument的事件,通過此事件來繪制需要打印的內容
PaperSize:指定紙張大小
毫米和英寸的換算:打印機是以英寸為單位的,單據設置是以毫米為單位的,所以需要轉換
效果圖如下:
核心代碼
關鍵代碼如下:
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方法 } } }
以上就是C#中怎么利用PrintDocument定制打印單據,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注創(chuàng)新互聯行業(yè)資訊頻道。
文章標題:C#中怎么利用PrintDocument定制打印單據
文章轉載:http://jinyejixie.com/article12/pdcgdc.html
成都網站建設公司_創(chuàng)新互聯,為您提供虛擬主機、網站改版、電子商務、網站制作、網站營銷、網頁設計公司
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯