public class PrintingExample : System.Windows.Forms.Form 
{
    private System.ComponentModel.Container components;
    private System.Windows.Forms.Button printButton;
    private Font printFont;
    private StreamReader streamToPrint;   public PrintingExample() : base() 
   {
      // The Windows Forms Designer requires the following call.
      InitializeComponent();
   }   // The Click event is raised when the user clicks the Print button.
   private void printButton_Click(object sender, EventArgs e) 
   {
      try 
      {
          streamToPrint = new StreamReader
             ("C:\\My Documents\\MyFile.txt");
          try 
          {
             printFont = new Font("Arial", 10);
             PrintDocument pd = new PrintDocument();
             pd.PrintPage += new PrintPageEventHandler
                (this.pd_PrintPage);
             pd.Print();
          }  
          finally 
          {
             streamToPrint.Close();
          }
      }  
      catch(Exception ex) 
      {
          MessageBox.Show(ex.Message);
      }
   }   // The PrintPage event is raised for each page to be printed.
   private void pd_PrintPage(object sender, PrintPageEventArgs ev) 
   {
      float linesPerPage = 0;
      float yPos = 0;
      int count = 0;
      float leftMargin = ev.MarginBounds.Left;
      float topMargin = ev.MarginBounds.Top;
      string line = null;      // Calculate the number of lines per page.
      linesPerPage = ev.MarginBounds.Height / 
         printFont.GetHeight(ev.Graphics);      // Print each line of the file.
      while(count < linesPerPage && 
         ((line=streamToPrint.ReadLine()) != null)) 
      {
         yPos = topMargin + (count * 
            printFont.GetHeight(ev.Graphics));
         ev.Graphics.DrawString(line, printFont, Brushes.Black, 
            leftMargin, yPos, new StringFormat());
         count++;
      }      // If more lines exist, print another page.
      if(line != null)
         ev.HasMorePages = true;
      else
         ev.HasMorePages = false;
   }
   // The Windows Forms Designer requires the following procedure.
   private void InitializeComponent() 
   {
      this.components = new System.ComponentModel.Container();
      this.printButton = new System.Windows.Forms.Button();      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(504, 381);
      this.Text = "Print Example";      printButton.ImageAlign = 
         System.Drawing.ContentAlignment.MiddleLeft;
      printButton.Location = new System.Drawing.Point(32, 110);
      printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
      printButton.TabIndex = 0;
      printButton.Text = "Print the file.";
      printButton.Size = new System.Drawing.Size(136, 40);
      printButton.Click += new System.EventHandler(printButton_Click);      this.Controls.Add(printButton);
   }   // This is the main entry point for the application.
   public static void Main(string[] args) 
   {
      Application.Run(new PrintingExample());
   }
}

解决方案 »

  1.   

    谢谢 abcynic(门外汉) ,现在可以打印了,可是打印汉字的时候是乱码,该怎么解决????
      

  2.   

    打印txt文件的中文乱码问题解决了,修改代码如下:
    streamToPrint = new StreamReader("C:\\t1.txt",System.Text.Encoding.GetEncoding("GB2312"));现在的问题是:如果我要打印word文档,现在打出来的全部是乱码了,怎么解决????
      

  3.   

    打印word文件的文件格式为:.doc
    streamToPrint = new StreamReader("C:\\t1.doc",System.Text.Encoding.GetEncoding("GB2312"));此时System.Text.Encoding.GetEncoding("GB2312")参数就不能使用了,System.Text.Encoding.GetEncoding("GB2312")参数支持.txt文件,不支持.doc文件
    可是应该是用什么参数我不知道,大家又知道的么?
    或者.doc文件根本不能用数据流读出打印?这个我了解得不多
      

  4.   

    现在我的打印.doc文件的问题解决了,用到了一个打印类(后面我会附上代码),谢谢以上大家的帮助,但我不想结贴,我现在又有新的问题了:我现在的情况是我可以在我的程序理显示.doc文件,当我想打印的时候,我的代码是这样的,取得当前显示的.doc文件的路径,然后用打印类打印出来,问题出在当我显示.doc文件的时候,系统已经开启了一个word进程,如果我用这个打印类来打印此.doc文件,这个类会另外再打开一个word进程(这个可以通过windows任务管理器看到),如果对微软word了解的人都知道,word文件可以共享多人浏览,为了防止多人修改,word的安全措施允许多个进程打开同一个.doc文件的时候只有第一个才有修改权限,其余的只有浏览权限,所以这个时候会有一个对话框弹出,来说明此文件已经被前面的用户进程打开(我不知道怎么去除这个对话框);
    如果打印成功,当关闭应为打印而打开的word进程的时候,应为是在本机操作word,这两个进程同时操作了一个word后台文件,所以当第二个进程打算关闭这个后台文件的时候,第一个进程又不允许它关,因此又会出现保存后台文件的对话框(恼火),这个打印一个当前显示的.doc文件竟然出现了这么大的麻烦,真不知道微软打印它的word文件是怎么实现的,大家有没有什么好的想法拉兄弟一把附上打印类代码:
    public class Printing  //打印当前程序所在路经的ti.doc文件的打印类{  public Printing()  {
      }
      public bool Bln_PrintAllFiles(string StrFileName)  {   Word.Application ObjWord =new Word.Application(); //Create Word Application
       //wd = new Word.ApplicationClass() 类
       //ObjWord =new Word.Application()  接口    object M=Type.Missing; //M is missing...hi..hi   object V= StrFileName; //Convert to Object..   object I=false; //Convert to Object
       try   {    if(StrFileName.EndsWith(".doc")) //Ends with .doc. itz Word document....    {     ObjWord.Documents.Open(ref V,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M);     ObjWord.PrintOut(ref I,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M); ObjWord.Quit(ref M,ref M,ref M); return true;
       }   else //for all other types..I did check for .txt,.pdf and .Msg
       {
    System.Diagnostics.Process P=new Process(); //Create a process P.StartInfo.FileName = StrFileName.ToString(); //convert ....> Readable P.StartInfo.CreateNoWindow = true; //Perfect...Create No Window P.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//WindowStyle-HIDDEN P.StartInfo.Verb = "print"; P.Start(); //Start the process P.Dispose(); ObjWord.Quit(ref M,ref M,ref M); return true;    }   }   catch(Exception ee) //Err Handler.....   {
         ObjWord.Quit(ref M,ref M,ref M); MessageBox.Show(ee.Message);
    return false;   }
      } }}
      

  5.   

    能不能试着直接用调用WORD自身的打印程序?
      

  6.   

    wd_318(饭加加) ,这个想过,我用ildasm.exe看了Interop.Word.dll,眼睛都看花了,我注意到word的ApplicationClass()创建的wd = new Word.ApplicationClass()中是有一个printout方法,可是调用它的同时还是新打开了一个word进程,还是2个进程操作一个word文件的问题;  我现在想能不能打印的时候杀掉前面一个word进程,不过这样就看不到显示的文件了,最好在显示文件的同时可以打印当前显示的.doc文件。不知大家有什么看法?????????????