91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何利用C#打印文件

發布時間:2021-06-16 13:55:21 來源:億速云 閱讀:162 作者:chen 欄目:編程語言

這篇文章主要介紹“如何利用C#打印文件”,在日常操作中,相信很多人在如何利用C#打印文件問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何利用C#打印文件”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

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  ...{      /**//// <summary>      /// Form1 的摘要說明。      /// </summary>      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;          /**//// <summary>          /// 必需的設計器變量。          /// </summary>          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 調用后添加任何構造函數代碼              //          }           /**//// <summary>          /// 清理所有正在使用的資源。          /// </summary>          protected override void Dispose( bool disposing )          ...{              if( disposing )              ...{                  if (components != null)                   ...{                      components.Dispose();                  }              }              base.Dispose( disposing );          }           Windows 窗體設計器生成的代碼#region Windows 窗體設計器生成的代碼          /**//// <summary>          /// 設計器支持所需的方法 - 不要使用代碼編輯器修改          /// 此方法的內容。          /// </summary>          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           /**//// <summary>          /// 應用程序的主入口點。          /// </summary>          [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<linesPerPage && totalLines<txtDoc.Lines.Length)              ...{                  g.DrawString(txtDoc.Lines[totalLines],txtDoc.Font,Brushes.Black,e.MarginBounds.X,e.MarginBounds.Y+lineHeight*count);                  count++;                  totalLines++;              }               if (totalLines<txtDoc.Lines.Length)              ...{                  e.HasMorePages=true;              }              else             ...{                  e.HasMorePages=false;                  totalLines=0;              }          }           private void mnuFilePrint_Click(object sender, System.EventArgs e)          ...{              if (dlgPrinterSetup.ShowDialog()==DialogResult.OK)              ...{                  pdoc.Print();  //開始執行打印              }          }           private void txtDoc_TextChanged(object sender, System.EventArgs e)          ...{                    }           private void mnuFilePrintPreview_Click(object sender, System.EventArgs e)          ...{              ppd.ShowDialog();          }           private void mnuFilePageSetup_Click(object sender, System.EventArgs e)          ...{              Margins oldMargins=dlgPageSetup.PageSettings.Margins;              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));              if (dlgPageSetup.ShowDialog()==DialogResult.Cancel)              ...{                  dlgPageSetup.PageSettings.Margins=oldMargins;              }          }      }  }

到此,關于“如何利用C#打印文件”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

时尚| 武冈市| 乐东| 神农架林区| 嘉禾县| 上林县| 湖州市| 仙桃市| 南丰县| 台东县| 涟源市| 侯马市| 年辖:市辖区| 东乡县| 黄陵县| 新丰县| 伊通| 石楼县| 越西县| 鄂温| 宿松县| 龙岩市| 玉林市| 正安县| 南华县| 中西区| 贺兰县| 普洱| 千阳县| 类乌齐县| 铜鼓县| 荣昌县| 梅州市| 平阴县| 浏阳市| 南丹县| 高清| 大足县| 新绛县| 东乡族自治县| 翁牛特旗|