Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Общие вопросы по .NET и C# > Как напечатать текстовый файл?


Автор: Ch0bits 29.1.2006, 17:41
САБЖ
smile

Автор: mr.DUDA 29.1.2006, 18:02
Пример из MSDN:

Код
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;

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.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());
   }
}

Автор: Ch0bits 30.1.2006, 00:03
Работает, только чего то русские буквы не печатаются. smile
Сейчас ещё покапаюсь...

Автор: mr.DUDA 30.1.2006, 09:09
Цитата(Ch0bits @ 29.1.2006, 23:03 Найти цитируемый пост)
Работает, только чего то русские буквы не печатаются.

Либо в конструктор StreamReader'а нужно добавить Win-1251 кодировку, либо шрифт создавать не "Arial" а "Arial CYR", либо и то и другое одновременно smile

Автор: Ch0bits 30.1.2006, 11:56
Угу.
Указал в конструкторе System.Text.Encoding.Default, теперь работает.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)