C#打印文件的简单实例

开发 后端
C#打印文件是按照文件内的内容进行打印的过程,这其中需要.NET Framework组件的部分功能进行支持,希望本文能对大家有所帮助。

C#打印文件的过程中,需要对所打印文件进行预览,对版式也有所要求。C#打印文件期间,也有可能出现错误推出,这些都是我们开发人员需要考虑的问题。

以下是C#打印文件的代码。

  1. using System;  
  2. using System.Drawing;  
  3. using System.Collections;  
  4. using System.ComponentModel;  
  5. using System.Windows.Forms;  
  6. using System.Data;  
  7. using System.IO;  
  8. using System.Text;  
  9. using System.Drawing.Printing;  
  10.  
  11. namespace PrintDemo  
  12. ...{  
  13.     /**//// <summary>  
  14.     /// Form1 的摘要说明。  
  15.     /// </summary>  
  16.     public class DemoForm : System.Windows.Forms.Form  
  17.     ...{  
  18.         private System.Windows.Forms.TextBox txtDoc;  
  19.         private System.Windows.Forms.MainMenu mnuMain;  
  20.         private System.Windows.Forms.MenuItem mnuFile;  
  21.         private System.Windows.Forms.MenuItem mnuFileOpen;  
  22.         private System.Windows.Forms.MenuItem mnuFilePrint;  
  23.         private System.Windows.Forms.MenuItem mnuFormat;  
  24.         private System.Windows.Forms.MenuItem mnuFormatFont;  
  25.         private System.Windows.Forms.OpenFileDialog dlgOpen;  
  26.         private System.Windows.Forms.FontDialog dlgFont;  
  27.         private System.Drawing.Printing.PrintDocument pdoc;  
  28.         /**//// <summary>  
  29.         /// 必需的设计器变量。  
  30.         /// </summary>  
  31.         private System.ComponentModel.Container components = null;  
  32.         private System.Windows.Forms.MenuItem mnuFilePrintPreview;  
  33.         private System.Windows.Forms.MenuItem mnuFilePageSetup;  
  34.         private System.Windows.Forms.PrintPreviewDialog ppd;  
  35.         private System.Windows.Forms.PageSetupDialog dlgPageSetup;  
  36.         private System.Windows.Forms.PrintDialog dlgPrinterSetup;  
  37.         private int totalLines;  
  38.  
  39.         public DemoForm()  
  40.         ...{  
  41.             //  
  42.             // Windows 窗体设计器支持所必需的  
  43.             //  
  44.             InitializeComponent();  
  45.  
  46.             //  
  47.             // TODO: 在 InitializeComponent 调用后添加任何构造函数代码  
  48.             //  
  49.         }  
  50.  
  51.         /**//// <summary>  
  52.         /// 清理所有正在使用的资源。  
  53.         /// </summary>  
  54.         protected override void Dispose( bool disposing )  
  55.         ...{  
  56.             if( disposing )  
  57.             ...{  
  58.                 if (components != null)   
  59.                 ...{  
  60.                     components.Dispose();  
  61.                 }  
  62.             }  
  63.             base.Dispose( disposing );  
  64.         }  
  65.  
  66.         Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码  
  67.         /**//// <summary>  
  68.         /// 设计器支持所需的方法 - 不要使用代码编辑器修改  
  69.         /// 此方法的内容。  
  70.         /// </summary>  
  71.         private void InitializeComponent()  
  72.         ...{  
  73.             System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(DemoForm));  
  74.             this.txtDoc = new System.Windows.Forms.TextBox();  
  75.             this.mnuMain = new System.Windows.Forms.MainMenu();  
  76.             this.mnuFile = new System.Windows.Forms.MenuItem();  
  77.             this.mnuFileOpen = new System.Windows.Forms.MenuItem();  
  78.             this.mnuFilePrint = new System.Windows.Forms.MenuItem();  
  79.             this.mnuFormat = new System.Windows.Forms.MenuItem();  
  80.             this.mnuFormatFont = new System.Windows.Forms.MenuItem();  
  81.             this.dlgOpen = new System.Windows.Forms.OpenFileDialog();  
  82.             this.dlgFont = new System.Windows.Forms.FontDialog();  
  83.             this.pdoc = new System.Drawing.Printing.PrintDocument();  
  84.             this.mnuFilePrintPreview = new System.Windows.Forms.MenuItem();  
  85.             this.mnuFilePageSetup = new System.Windows.Forms.MenuItem();  
  86.             this.ppd = new System.Windows.Forms.PrintPreviewDialog();  
  87.             this.dlgPageSetup = new System.Windows.Forms.PageSetupDialog();  
  88.             this.dlgPrinterSetup = new System.Windows.Forms.PrintDialog();  
  89.             this.SuspendLayout();  
  90.             //   
  91.             // txtDoc  
  92.             //   
  93.             this.txtDoc.Dock = System.Windows.Forms.DockStyle.Fill;  
  94.             this.txtDoc.Location = new System.Drawing.Point(0, 0);  
  95.             this.txtDoc.Multiline = true;  
  96.             this.txtDoc.Name = "txtDoc";  
  97.             this.txtDoc.ScrollBars = System.Windows.Forms.ScrollBars.Both;  
  98.             this.txtDoc.Size = new System.Drawing.Size(560, 309);  
  99.             this.txtDoc.TabIndex = 0;  
  100.             this.txtDoc.Text = "";  
  101.             this.txtDoc.WordWrap = false;  
  102.             this.txtDoc.TextChanged += new System.EventHandler(this.txtDoc_TextChanged);  
  103.             //   
  104.             // mnuMain  
  105.             //   
  106.             this.mnuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] ...{  
  107.                                                                                     this.mnuFile,  
  108.                                                                                     this.mnuFormat});  
  109.             //   
  110.             // mnuFile  
  111.             //   
  112.             this.mnuFile.Index = 0;  
  113.             this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] ...{  
  114.                                                                                     this.mnuFileOpen,  
  115.                                                                                     this.mnuFilePageSetup,  
  116.                                                                                     this.mnuFilePrintPreview,  
  117.                                                                                     this.mnuFilePrint});  
  118.             this.mnuFile.Text = "文件(&F)";  
  119.             //   
  120.             // mnuFileOpen  
  121.             //   
  122.             this.mnuFileOpen.Index = 0;  
  123.             this.mnuFileOpen.Text = "打开(&O)...";  
  124.             this.mnuFileOpen.Click += new System.EventHandler(this.mnuFileOpen_Click);  
  125.             //   
  126.             // mnuFilePrint  
  127.             //   
  128.             this.mnuFilePrint.Index = 3;  
  129.             this.mnuFilePrint.Text = "打印(&P)...";  
  130.             this.mnuFilePrint.Click += new System.EventHandler(this.mnuFilePrint_Click);  
  131.             //   
  132.             // mnuFormat  
  133.             //   
  134.             this.mnuFormat.Index = 1;  
  135.             this.mnuFormat.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] ...{  
  136.                                                                                       this.mnuFormatFont});  
  137.             this.mnuFormat.Text = "格式(&F)";  
  138.             //   
  139.             // mnuFormatFont  
  140.             //   
  141.             this.mnuFormatFont.Index = 0;  
  142.             this.mnuFormatFont.Text = "字体(&O)";  
  143.             this.mnuFormatFont.Click += new System.EventHandler(this.mnuFormatFont_Click);  
  144.             //   
  145.             // dlgOpen  
  146.             //   
  147.             this.dlgOpen.Filter = "文本文件|*.txt|所有文件|*.*";  
  148.             //   
  149.             // pdoc  
  150.             //   
  151.             this.pdoc.DocumentName = "abc";  
  152.             this.pdoc.BeginPrint += new System.Drawing.Printing.PrintEventHandler(this.pdoc_BeginPrint);  
  153.             this.pdoc.EndPrint += new System.Drawing.Printing.PrintEventHandler(this.pdoc_EndPrint);  
  154.             this.pdoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.pdoc_PrintPage);  
  155.             //   
  156.             // mnuFilePrintPreview  
  157.             //   
  158.             this.mnuFilePrintPreview.Index = 2;  
  159.             this.mnuFilePrintPreview.Text = "打印预览...";  
  160.             this.mnuFilePrintPreview.Click += new System.EventHandler(this.mnuFilePrintPreview_Click);  
  161.             //   
  162.             // mnuFilePageSetup  
  163.             //   
  164.             this.mnuFilePageSetup.Index = 1;  
  165.             this.mnuFilePageSetup.Text = "页面设置...";  
  166.             this.mnuFilePageSetup.Click += new System.EventHandler(this.mnuFilePageSetup_Click);  
  167.             //   
  168.             // ppd  
  169.             //   
  170.             this.ppd.AutoScrollMargin = new System.Drawing.Size(0, 0);  
  171.             this.ppd.AutoScrollMinSize = new System.Drawing.Size(0, 0);  
  172.             this.ppd.ClientSize = new System.Drawing.Size(400, 300);  
  173.             this.ppd.Document = this.pdoc;  
  174.             this.ppd.Enabled = true;  
  175.             this.ppd.Icon = ((System.Drawing.Icon)(resources.GetObject("ppd.Icon")));  
  176.             this.ppd.Location = new System.Drawing.Point(317, 17);  
  177.             this.ppd.MinimumSize = new System.Drawing.Size(375, 250);  
  178.             this.ppd.Name = "ppd";  
  179.             this.ppd.TransparencyKey = System.Drawing.Color.Empty;  
  180.             this.ppd.Visible = false;  
  181.             //   
  182.             // dlgPageSetup  
  183.             //   
  184.             this.dlgPageSetup.Document = this.pdoc;  
  185.             //   
  186.             // dlgPrinterSetup  
  187.             //   
  188.             this.dlgPrinterSetup.Document = this.pdoc;  
  189.             //   
  190.             // DemoForm  
  191.             //   
  192.             this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);  
  193.             this.ClientSize = new System.Drawing.Size(560, 309);  
  194.             this.Controls.Add(this.txtDoc);  
  195.             this.Menu = this.mnuMain;  
  196.             this.Name = "DemoForm";  
  197.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;  
  198.             this.Text = "打印";  
  199.             this.ResumeLayout(false);  
  200.  
  201.         }  
  202.         #endregion  
  203.  
  204.         /**//// <summary>  
  205.         /// 应用程序的主入口点。  
  206.         /// </summary>  
  207.         [STAThread]  
  208.         static void Main()   
  209.         ...{  
  210.             Application.Run(new DemoForm());  
  211.         }  
  212.  
  213.         private void mnuFileOpen_Click(object sender, System.EventArgs e)  
  214.         ...{  
  215.             if (dlgOpen.ShowDialog()==DialogResult.OK)  
  216.             ...{  
  217.                 StreamReader sr=null;  
  218.                 try 
  219.                 ...{  
  220.                     sr=new StreamReader(dlgOpen.FileName,Encoding.Default,true);  
  221.                     txtDoc.Text=sr.ReadToEnd();  
  222.                 }  
  223.                 catch 
  224.                 ...{  
  225.                     MessageBox.Show("打开文件失败!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);  
  226.                 }  
  227.                 finally 
  228.                 ...{  
  229.                     if (sr!=null) sr.Close();  
  230.                 }  
  231.             }  
  232.         }  
  233.  
  234.         private void mnuFormatFont_Click(object sender, System.EventArgs e)  
  235.         ...{  
  236.             dlgFont.Font=txtDoc.Font;  
  237.             if (dlgFont.ShowDialog()==DialogResult.OK)  
  238.             ...{  
  239.                 txtDoc.Font=dlgFont.Font;  
  240.             }  
  241.         }  
  242.  
  243.         private void pdoc_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)  
  244.         ...{  
  245.             //MessageBox.Show("开始打印啦");  
  246.         }  
  247.  
  248.         private void pdoc_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)  
  249.         ...{  
  250.             //MessageBox.Show("打印结束");  
  251.         }  
  252.  
  253.         private void pdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)  
  254.         ...{  
  255.             Graphics g=e.Graphics;  
  256.             float lineHeight=txtDoc.Font.GetHeight(g);  
  257.             int linesPerPage=(int)(e.MarginBounds.Height/lineHeight);  
  258.             int count=0;  //本页已打印行数  
  259.  
  260.             while (count<linesPerPage && totalLines<txtDoc.Lines.Length)  
  261.             ...{  
  262.                 g.DrawString(txtDoc.Lines[totalLines],txtDoc.Font,Brushes.Black,e.MarginBounds.X,e.MarginBounds.Y+lineHeight*count);  
  263.                 count++;  
  264.                 totalLines++;  
  265.             }  
  266.  
  267.             if (totalLines<txtDoc.Lines.Length)  
  268.             ...{  
  269.                 e.HasMorePages=true;  
  270.             }  
  271.             else 
  272.             ...{  
  273.                 e.HasMorePages=false;  
  274.                 totalLines=0;  
  275.             }  
  276.         }  
  277.  
  278.         private void mnuFilePrint_Click(object sender, System.EventArgs e)  
  279.         ...{  
  280.             if (dlgPrinterSetup.ShowDialog()==DialogResult.OK)  
  281.             ...{  
  282.                 pdoc.Print();  //开始执行打印  
  283.             }  
  284.         }  
  285.  
  286.         private void txtDoc_TextChanged(object sender, System.EventArgs e)  
  287.         ...{  
  288.           
  289.         }  
  290.  
  291.         private void mnuFilePrintPreview_Click(object sender, System.EventArgs e)  
  292.         ...{  
  293.             ppd.ShowDialog();  
  294.         }  
  295.  
  296.         private void mnuFilePageSetup_Click(object sender, System.EventArgs e)  
  297.         ...{  
  298.             Margins oldMargins=dlgPageSetup.PageSettings.Margins;  
  299.             dlgPageSetup.PageSettings.Margins=new Margins((int)(oldMargins.Left*2.54),(int)(oldMargins.Right*2.54),(int)(oldMargins.Top*2.54),(int)(oldMargins.Bottom*2.54));  
  300.             if (dlgPageSetup.ShowDialog()==DialogResult.Cancel)  
  301.             ...{  
  302.                 dlgPageSetup.PageSettings.Margins=oldMargins;  
  303.             }  
  304.         }  
  305.     }  

C#打印文件的简单实例就介绍到这里。

【编辑推荐】

  1. C#打印设置实现源码详解
  2. C#打印控件的使用实例浅析
  3. C#打印条码操作的实例浅析
  4. C#打印原理解析及实例操作
  5. C#及.NET FrameWork的概念浅析
责任编辑:彭凡 来源: CSDN
相关推荐

2009-08-26 11:53:56

C#打印文本文件

2009-08-26 12:14:44

C#打印设置

2009-09-01 18:25:32

C#结构实例

2009-09-01 18:36:35

C#委托实例

2009-08-26 13:36:33

C#打印控件

2009-08-26 13:48:31

C#打印条码

2009-08-26 11:07:36

C#打印窗体

2009-08-26 09:22:44

C#实现打印功能

2009-08-26 11:32:37

C#打印文档

2009-08-31 18:24:26

编译C#文件

2009-08-26 09:54:45

C#打印预览C#打印

2009-08-26 14:03:26

C#打印原理

2009-08-18 17:05:08

C#操作xml文件

2009-09-01 14:39:47

C#创建Excel文件

2009-08-31 18:38:59

C#写文件

2009-09-01 11:25:08

C#读取Word文件

2009-08-24 17:58:19

C#读取XML文件

2011-05-20 16:07:29

C#

2021-03-15 08:18:23

C#反射模块

2009-09-01 16:14:06

C#窗口抖动
点赞
收藏

51CTO技术栈公众号