本篇內(nèi)容介紹了“C#怎么利用PrintDocument定制打印單據(jù)”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),詔安企業(yè)網(wǎng)站建設(shè),詔安品牌網(wǎng)站建設(shè),網(wǎng)站定制,詔安網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷(xiāo),網(wǎng)絡(luò)優(yōu)化,
詔安網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
前言
本文是利用PrintDocument定制打印單據(jù)的小例子,僅供學(xué)習(xí)分享使用,如果不足之處,還請(qǐng)指正。
涉及知識(shí)點(diǎn):
PrintDocument :從 Windows 窗體應(yīng)用程序打印時(shí),定義一種可重用的可發(fā)送到打印機(jī)上的對(duì)象。 PrintPreviewControl :表示 Windows 窗體應(yīng)用程序打印預(yù)覽的原始預(yù)覽部分,沒(méi)有任何對(duì)話框或按鈕。 Graphics :GDI+繪圖對(duì)象 PrinterSettings:設(shè)置打印機(jī)屬性,如:設(shè)置屬性Copies,可以設(shè)置打印份數(shù),默認(rèn)為1, PageSettings:指定應(yīng)用于單頁(yè)打印的設(shè)置 DefaultPageSettings:PrintDocument的屬性 PrintPage事件:PrintDocument的事件,通過(guò)此事件來(lái)繪制需要打印的內(nèi)容 PaperSize:指定紙張大小 毫米和英寸的換算:打印機(jī)是以英寸為單位的,單據(jù)設(shè)置是以毫米為單位的,所以需要轉(zhuǎn)換
效果圖如下:
核心代碼
關(guān)鍵代碼如下:
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;//上下邊距 /// /// 需要打印的內(nèi)容 /// public List 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 lstPrintInfos = new List(); 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; } /// /// 轉(zhuǎn)換毫米到百分之一英寸 /// /// /// private int MM2Inch(int mm) { return (int)(mm * 100.0f / 25.4f); } /// /// 打印開(kāi)始事件 /// /// /// private void pdControl_BeginPrint(object sender, PrintEventArgs e) { } /// /// 打印事件 /// /// /// 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; //先畫(huà)一個(gè)矩形 Pen lineColor = new Pen(Color.Black, 0.2f); g.FillRectangle(Brushes.Linen,0,0,width_p,height_p); printHelper.Print(g); } /// /// 打印結(jié)束事件 /// /// /// private void pdControl_EndPrint(object sender, PrintEventArgs e) { } /// /// 打印 /// /// /// private void btnPrint_Click(object sender, EventArgs e) { //打印對(duì)話框 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; } } /// /// 刷新 /// /// /// private void btnRefresh_Click(object sender, EventArgs e) { List lstPrintInfos = new List(); //表格配置 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); //標(biāo)題配置 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); //內(nèi)容 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); } //打印時(shí)間 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();//刷新文檔的預(yù)覽,重新調(diào)用PrintDocument的Print方法 } }}
“C#怎么利用PrintDocument定制打印單據(jù)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
名稱欄目:C#怎么利用PrintDocument定制打印單據(jù)-創(chuàng)新互聯(lián)
分享網(wǎng)址:
http://weahome.cn/article/cejiho.html