在C#中實現列印功能(C#中PrintDialog,PrintDocument的使用)

來源:互聯網
上載者:User

在C#中實現列印功能(C#中PrintDialog,PrintDocument的使用)

 

 在C#中使用PrintDialog可以很方便的實現程式的列印功能。

其步驟如下:

  1. 建立一個PrintDialog的執行個體。如下:
    System.Windows.Forms.PrintDialog PrintDialog1=new PrintDialog ();
  2. 建立一個PrintDocument的執行個體.如下:
    System.Drawing.Printing.PrintDocument docToPrint =
       new System.Drawing.Printing.PrintDocument();
  3. 設定印表機開始列印的事件處理函數.函數原形如下:
    void docToPrint_PrintPage(object sender,
       System.Drawing.Printing.PrintPageEventArgs e)
  4. 將事件處理函數添加到PrintDocument的PrintPage事件中。
    docToPrint.PrintPage+=new PrintPageEventHandler(docToPrint_PrintPage);
  5. 設定PrintDocument的相關屬性,如:
    PrintDialog1.AllowSomePages = true;PrintDialog1.ShowHelp = true;
  6. 把PrintDialog的Document屬性設為上面配置好的PrintDocument的執行個體:
    PrintDialog1.Document = docToPrint;
  7. 調用PrintDialog的ShowDialog函數顯示列印對話方塊:
    DialogResult result = PrintDialog1.ShowDialog();
  8. 根據使用者的選擇,開始列印:
    if (result==DialogResult.OK)
       {
        docToPrint.Print();
       }

例子如下:

使用時先建立PrintService類的執行個體,然後調用void StartPrint(Stream streamToPrint,string streamType)函數開始列印。其中streamToPrint是要列印的內容(位元組流),streamType是流的類型(txt表示普通文本,image表示映像);

 

using System;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.IO;

namespace EDImageSystem
{
 /// <summary>
 /// PrintService 的摘要說明。
 /// </summary>
 public class PrintService
 {
  public PrintService()
  {
   //
   // TODO: 在此處添加建構函式邏輯
   //
   this.docToPrint.PrintPage+=new PrintPageEventHandler(docToPrint_PrintPage);
  }//將事件處理函數添加到PrintDocument的PrintPage中

  // Declare the PrintDocument object.
  private System.Drawing.Printing.PrintDocument docToPrint =
   new System.Drawing.Printing.PrintDocument();//建立一個PrintDocument的執行個體

  private System.IO.Stream streamToPrint;
  string streamType;

  // This method will set properties on the PrintDialog object and
  // then display the dialog.
  public void StartPrint(Stream streamToPrint,string streamType)
  {

   this.streamToPrint=streamToPrint;
   this.streamType=streamType;
   // Allow the user to choose the page range he or she would
   // like to print.
   System.Windows.Forms.PrintDialog PrintDialog1=new PrintDialog ();//建立一個PrintDialog的執行個體。
   PrintDialog1.AllowSomePages = true;

   // Show the help button.
   PrintDialog1.ShowHelp = true;

   // Set the Document property to the PrintDocument for
   // which the PrintPage Event has been handled. To display the
   // dialog, either this property or the PrinterSettings property
   // must be set
   PrintDialog1.Document = docToPrint;//把PrintDialog的Document屬性設為上面配置好的PrintDocument的執行個體

   DialogResult result = PrintDialog1.ShowDialog();//調用PrintDialog的ShowDialog函數顯示列印對話方塊

   // If the result is OK then print the document.
   if (result==DialogResult.OK)
   {
    docToPrint.Print();//開始列印
   }

  }

// The PrintDialog will print the document
  // by handling the document's PrintPage event.
  private void docToPrint_PrintPage(object sender,
   System.Drawing.Printing.PrintPageEventArgs e)//設定印表機開始列印的事件處理函數
  {

   // Insert code to render the page here.
   // This code will be called when the control is drawn.

   // The following code will render a simple
   // message on the printed document
   switch(this.streamType)
   {
    case "txt":
     string text = null;
     System.Drawing.Font printFont = new System.Drawing.Font
      ("Arial", 35, System.Drawing.FontStyle.Regular);

     // Draw the content.
     System.IO.StreamReader streamReader=new StreamReader(this.streamToPrint);
     text=streamReader.ReadToEnd();
     e.Graphics.DrawString(text,printFont,System.Drawing.Brushes.Black,e.MarginBounds.X,e.MarginBounds.Y);
     break;
    case "image":
     System.Drawing.Image image=System.Drawing.Image.FromStream(this.streamToPrint);
     int x=e.MarginBounds.X;
     int y=e.MarginBounds.Y;
     int width=image.Width;
     int height=image.Height;
     if((width/e.MarginBounds.Width)>(height/e.MarginBounds.Height))
     {
      width=e.MarginBounds.Width;
      height=image.Height*e.MarginBounds.Width/image.Width;
     }
     else
     {
      height=e.MarginBounds.Height;
      width=image.Width*e.MarginBounds.Height/image.Height;
     }
     System.Drawing.Rectangle destRect=new System.Drawing.Rectangle(x,y,width,height);
     e.Graphics.DrawImage(image,destRect,0,0,image.Width,image.Height,System.Drawing.GraphicsUnit.Pixel);
     break;
    default:
     break;
   }
   
  }

 }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.