在windows應用程序中打印是一項非常重要的功能,在實際運用中也較多,.net中的打印功能都以組件的方式提供,為程序員提供了很大的方便,打印 操作通常包括以下四個功能
1 打印設置 設置打印機的一些參數比如更改打印機驅動程序等
2 頁面設置 設置頁面大小紙張類型等
3 打印預覽 類似於word中的打印預覽
4 打印
下面以是一個簡單的示例


1、打印機設置代碼
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument;
printDialog.ShowDialog();2、打印紙張設置代碼
PageSetupDialog pageSetupDialog = new PageSetupDialog();
pageSetupDialog.Document = printDocument;
pageSetupDialog.ShowDialog();3、打印預覽代碼
printDocument.PrintPage += PrintDocument_PrintPage;
PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog { Document = printDocument };
try
{
printPreviewDialog.ShowDialog();
}
catch (Exception excep)
{
MessageBox.Show(excep.Message, "打印出錯", MessageBoxButtons.OK, MessageBoxIcon.Error);
}4、打印內容設置代碼
Graphics g = e.Graphics ;
Pen p_Line = new Pen(Color.Blue, 2f);
p_Line.DashStyle = DashStyle.Solid;
//
g.DrawRectangle(p_Line, new Rectangle(100, 50, 300, 200));
g.DrawLine(p_Line, new Point(100, 150), new Point(400, 150));
g.DrawLine(p_Line, new Point(100, 188), new Point(400, 188));
g.DrawLine(p_Line, new Point(100, 221), new Point(400, 221));
//
g.DrawLine(p_Line, new Point(200, 83), new Point(400, 83));
g.DrawLine(p_Line, new Point(200, 116), new Point(400, 116));
//豎線
g.DrawLine(p_Line, new Point(200, 50), new Point(200, 250));
g.DrawLine(p_Line, new Point(300, 50), new Point(300, 150));
//文字
Brush b_Text = new SolidBrush(Color.Black);
g.DrawString("姓名", new Font("微軟雅黑", 12f, FontStyle.Regular), b_Text, new Point(230, 55));
g.DrawString("性別", new Font("微軟雅黑", 12f, FontStyle.Regular), b_Text, new Point(230, 88));
g.DrawString("民族", new Font("微軟雅黑", 12f, FontStyle.Regular), b_Text, new Point(230, 121));
g.DrawString("Lena", new Font("微軟雅黑", 12f, FontStyle.Regular), b_Text, new Point(330, 55));
g.DrawString("女", new Font("微軟雅黑", 12f, FontStyle.Regular), b_Text, new Point(340, 88));
g.DrawString("未知", new Font("微軟雅黑", 12f, FontStyle.Regular), b_Text, new Point(330, 121));
g.DrawString("公司名稱", new Font("微軟雅黑", 12f, FontStyle.Regular), b_Text, new Point(115, 155));
g.DrawString("職位", new Font("微軟雅黑", 12f, FontStyle.Regular), b_Text, new Point(130, 193));
g.DrawString("聯繫電話", new Font("微軟雅黑", 12f, FontStyle.Regular), b_Text, new Point(115, 225));
pic = resizeImage(pic,new Size(92,92));
g.DrawImage(pic, 102, 52);打印代碼
printDocument.PrintPage += PrintDocument_PrintPage;
try
{
printDocument.Print();
}
catch (Exception excep)
{
MessageBox.Show(excep.Message, "打印出錯", MessageBoxButtons.OK, MessageBoxIcon.Error);
printDocument.PrintController.OnEndPrint(printDocument, new PrintEventArgs());
}原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/224249.html
微信掃一掃
支付寶掃一掃