Printing Text File in C#

In this article, you will learn how to print a text file in C#.

Step 1

Create a Windows Forms application using Visual Studio and add two Button and one TextBox controls to the Form. Change names for the Buttons to Browse and Print respectively.

Step 2

Write the following code on the Browse button click event handler.

  1. OpenFileDialog fdlg = new OpenFileDialog();  
  2. fdlg.Title = "C# Corner Open File Dialog";  
  3. fdlg.InitialDirectory = @"C:\ ";  
  4. fdlg.Filter =  
  5. "Text files (*.txt | .txt | All files (*.*) | *.*";  
  6. fdlg.FilterIndex = 2;  
  7. fdlg.RestoreDirectory = true;  
  8. if (fdlg.ShowDialog() == DialogResult.OK)  
  9. {  
  10.     textBox1.Text = fdlg.FileName;  
  11. } 

Step 3

Before we write code on the Print button click event handler, define two private variables on class level.

  1. private Font verdana10Font;  
  2. private StreamReader reader;  

Now import these two namespaces.

  1. using System.IO;  
  2. using System.Drawing.Printing; 

Write the following code on Print button click event handler. 

  1. string filename=textBox1.Text.ToString();  
  2. //Create a StreamReader object  
  3. reader = new StreamReader (filename);  
  4. //Create a Verdana font with size 10  
  5. verdana10Font = new Font ("Verdana", 10);  
  6. //Create a PrintDocument object  
  7. PrintDocument pd = new PrintDocument();  
  8. //Add PrintPage event handler  
  9. pd.PrintPage += new PrintPageEventHandler(this.PrintTextFileHandler);  
  10. //Call Print Method  
  11. pd.Print();  
  12. //Close the reader  
  13. if (reader != null)  
  14.     reader.Close();

And add the following method to the class.

  1. private void PrintTextFileHandler (object sender, PrintPageEventArgs ppeArgs)  
  2. {  
  3.     //Get the Graphics object  
  4.     Graphics g = ppeArgs.Graphics;  
  5.     float linesPerPage = 0;  
  6.     float yPos = 0;  
  7.     int count = 0;  
  8.     //Read margins from PrintPageEventArgs  
  9.     float leftMargin = ppeArgs.MarginBounds.Left;  
  10.     float topMargin = ppeArgs.MarginBounds.Top;  
  11.     string line = null;  
  12.     //Calculate the lines per page on the basis of the height of the page and the height of the font  
  13.     linesPerPage = ppeArgs.MarginBounds.Height/verdana10Font.GetHeight (g);  
  14.     //Now read lines one by one, using StreamReader  
  15.     while (count<linesPerPage && (( line = reader.ReadLine ()) != null))  
  16.     {  
  17.         //Calculate the starting position  
  18.         yPos = topMargin + (count * verdana10Font.GetHeight (g));  
  19.         //Draw text  
  20.         g.DrawString (line, verdana10Font, Brushes.Black,leftMargin, yPos, new StringFormat());  
  21.         //Move to next line  
  22.         count++;  
  23.     }  
  24.     //If PrintPageEventArgs has more pages to print  
  25.     if (line != null)  
  26.     {  
  27.         ppeArgs.HasMorePages = true;  
  28.     }  
  29.     else  
  30.     {  
  31.         ppeArgs.HasMorePages = false;  
  32.     }  
  33. } 

Step 4

Now build and run the application. Click Browse button and open a text file and click Print button to print the file contents.
 
Print Text File in C# 


Recommended Ebook

Printing in C# Made Easy

Download Now!
Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.