在windows应用程序中打印是一项非常重要的功能,在实际运用中也较多,.net中的打印功能都以组件的方式提供,为程序员提供了很大的方便,打印 操作通常包括以下四个功能

1 打印设置 设置打印机的一些参数比如更改打印机驱动程序等

2 页面设置 设置页面大小纸张类型等

3 打印预览 类似于word中的打印预览

4 打印

下面以是一个简单的示例

c#操作word源代码(c语言读写word文档表格)(1)c#操作word源代码(c语言读写word文档表格)(2)

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());}