网站建设资讯

NEWS

网站建设资讯

如何利用C#打印文件

这篇文章主要介绍“如何利用C#打印文件”,在日常操作中,相信很多人在如何利用C#打印文件问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何利用C#打印文件”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

创新互联2013年至今,先为柳河等服务建站,柳河等地企业,进行企业商务咨询服务。为柳河企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

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

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

using System;  using System.Drawing;  using System.Collections;  using System.ComponentModel;  using System.Windows.Forms;  using System.Data;  using System.IO;  using System.Text;  using System.Drawing.Printing;   namespace PrintDemo  ...{      /**////       /// Form1 的摘要说明。      ///       public class DemoForm : System.Windows.Forms.Form      ...{          private System.Windows.Forms.TextBox txtDoc;          private System.Windows.Forms.MainMenu mnuMain;          private System.Windows.Forms.MenuItem mnuFile;          private System.Windows.Forms.MenuItem mnuFileOpen;          private System.Windows.Forms.MenuItem mnuFilePrint;          private System.Windows.Forms.MenuItem mnuFormat;          private System.Windows.Forms.MenuItem mnuFormatFont;          private System.Windows.Forms.OpenFileDialog dlgOpen;          private System.Windows.Forms.FontDialog dlgFont;          private System.Drawing.Printing.PrintDocument pdoc;          /**////           /// 必需的设计器变量。          ///           private System.ComponentModel.Container components = null;          private System.Windows.Forms.MenuItem mnuFilePrintPreview;          private System.Windows.Forms.MenuItem mnuFilePageSetup;          private System.Windows.Forms.PrintPreviewDialog ppd;          private System.Windows.Forms.PageSetupDialog dlgPageSetup;          private System.Windows.Forms.PrintDialog dlgPrinterSetup;          private int totalLines;           public DemoForm()          ...{              //              // Windows 窗体设计器支持所必需的              //              InitializeComponent();               //              // TODO: 在 InitializeComponent 调用后添加任何构造函数代码              //          }           /**////           /// 清理所有正在使用的资源。          ///           protected override void Dispose( bool disposing )          ...{              if( disposing )              ...{                  if (components != null)                   ...{                      components.Dispose();                  }              }              base.Dispose( disposing );          }           Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码          /**////           /// 设计器支持所需的方法 - 不要使用代码编辑器修改          /// 此方法的内容。          ///           private void InitializeComponent()          ...{              System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(DemoForm));              this.txtDoc = new System.Windows.Forms.TextBox();              this.mnuMain = new System.Windows.Forms.MainMenu();              this.mnuFile = new System.Windows.Forms.MenuItem();              this.mnuFileOpen = new System.Windows.Forms.MenuItem();              this.mnuFilePrint = new System.Windows.Forms.MenuItem();              this.mnuFormat = new System.Windows.Forms.MenuItem();              this.mnuFormatFont = new System.Windows.Forms.MenuItem();              this.dlgOpen = new System.Windows.Forms.OpenFileDialog();              this.dlgFont = new System.Windows.Forms.FontDialog();              this.pdoc = new System.Drawing.Printing.PrintDocument();              this.mnuFilePrintPreview = new System.Windows.Forms.MenuItem();              this.mnuFilePageSetup = new System.Windows.Forms.MenuItem();              this.ppd = new System.Windows.Forms.PrintPreviewDialog();              this.dlgPageSetup = new System.Windows.Forms.PageSetupDialog();              this.dlgPrinterSetup = new System.Windows.Forms.PrintDialog();              this.SuspendLayout();              //               // txtDoc              //               this.txtDoc.Dock = System.Windows.Forms.DockStyle.Fill;              this.txtDoc.Location = new System.Drawing.Point(0, 0);              this.txtDoc.Multiline = true;              this.txtDoc.Name = "txtDoc";              this.txtDoc.ScrollBars = System.Windows.Forms.ScrollBars.Both;              this.txtDoc.Size = new System.Drawing.Size(560, 309);              this.txtDoc.TabIndex = 0;              this.txtDoc.Text = "";              this.txtDoc.WordWrap = false;              this.txtDoc.TextChanged += new System.EventHandler(this.txtDoc_TextChanged);              //               // mnuMain              //               this.mnuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] ...{                                                                                      this.mnuFile,                                                                                      this.mnuFormat});              //               // mnuFile              //               this.mnuFile.Index = 0;              this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] ...{                                                                                      this.mnuFileOpen,                                                                                      this.mnuFilePageSetup,                                                                                      this.mnuFilePrintPreview,                                                                                      this.mnuFilePrint});              this.mnuFile.Text = "文件(&F)";              //               // mnuFileOpen              //               this.mnuFileOpen.Index = 0;              this.mnuFileOpen.Text = "打开(&O)...";              this.mnuFileOpen.Click += new System.EventHandler(this.mnuFileOpen_Click);              //               // mnuFilePrint              //               this.mnuFilePrint.Index = 3;              this.mnuFilePrint.Text = "打印(&P)...";              this.mnuFilePrint.Click += new System.EventHandler(this.mnuFilePrint_Click);              //               // mnuFormat              //               this.mnuFormat.Index = 1;              this.mnuFormat.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] ...{                                                                                        this.mnuFormatFont});              this.mnuFormat.Text = "格式(&F)";              //               // mnuFormatFont              //               this.mnuFormatFont.Index = 0;              this.mnuFormatFont.Text = "字体(&O)";              this.mnuFormatFont.Click += new System.EventHandler(this.mnuFormatFont_Click);              //               // dlgOpen              //               this.dlgOpen.Filter = "文本文件|*.txt|所有文件|*.*";              //               // pdoc              //               this.pdoc.DocumentName = "abc";              this.pdoc.BeginPrint += new System.Drawing.Printing.PrintEventHandler(this.pdoc_BeginPrint);              this.pdoc.EndPrint += new System.Drawing.Printing.PrintEventHandler(this.pdoc_EndPrint);              this.pdoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.pdoc_PrintPage);              //               // mnuFilePrintPreview              //               this.mnuFilePrintPreview.Index = 2;              this.mnuFilePrintPreview.Text = "打印预览...";              this.mnuFilePrintPreview.Click += new System.EventHandler(this.mnuFilePrintPreview_Click);              //               // mnuFilePageSetup              //               this.mnuFilePageSetup.Index = 1;              this.mnuFilePageSetup.Text = "页面设置...";              this.mnuFilePageSetup.Click += new System.EventHandler(this.mnuFilePageSetup_Click);              //               // ppd              //               this.ppd.AutoScrollMargin = new System.Drawing.Size(0, 0);              this.ppd.AutoScrollMinSize = new System.Drawing.Size(0, 0);              this.ppd.ClientSize = new System.Drawing.Size(400, 300);              this.ppd.Document = this.pdoc;              this.ppd.Enabled = true;              this.ppd.Icon = ((System.Drawing.Icon)(resources.GetObject("ppd.Icon")));              this.ppd.Location = new System.Drawing.Point(317, 17);              this.ppd.MinimumSize = new System.Drawing.Size(375, 250);              this.ppd.Name = "ppd";              this.ppd.TransparencyKey = System.Drawing.Color.Empty;              this.ppd.Visible = false;              //               // dlgPageSetup              //               this.dlgPageSetup.Document = this.pdoc;              //               // dlgPrinterSetup              //               this.dlgPrinterSetup.Document = this.pdoc;              //               // DemoForm              //               this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);              this.ClientSize = new System.Drawing.Size(560, 309);              this.Controls.Add(this.txtDoc);              this.Menu = this.mnuMain;              this.Name = "DemoForm";              this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;              this.Text = "打印";              this.ResumeLayout(false);           }          #endregion           /**////           /// 应用程序的主入口点。          ///           [STAThread]          static void Main()           ...{              Application.Run(new DemoForm());          }           private void mnuFileOpen_Click(object sender, System.EventArgs e)          ...{              if (dlgOpen.ShowDialog()==DialogResult.OK)              ...{                  StreamReader sr=null;                  try                 ...{                      sr=new StreamReader(dlgOpen.FileName,Encoding.Default,true);                      txtDoc.Text=sr.ReadToEnd();                  }                  catch                 ...{                      MessageBox.Show("打开文件失败!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);                  }                  finally                 ...{                      if (sr!=null) sr.Close();                  }              }          }           private void mnuFormatFont_Click(object sender, System.EventArgs e)          ...{              dlgFont.Font=txtDoc.Font;              if (dlgFont.ShowDialog()==DialogResult.OK)              ...{                  txtDoc.Font=dlgFont.Font;              }          }           private void pdoc_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)          ...{              //MessageBox.Show("开始打印啦");          }           private void pdoc_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)          ...{              //MessageBox.Show("打印结束");          }           private void pdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)          ...{              Graphics g=e.Graphics;              float lineHeight=txtDoc.Font.GetHeight(g);              int linesPerPage=(int)(e.MarginBounds.Height/lineHeight);              int count=0;  //本页已打印行数               while (count

到此,关于“如何利用C#打印文件”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


网站栏目:如何利用C#打印文件
标题来源:http://njwzjz.com/article/jhcjpg.html