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.
- OpenFileDialog fdlg = new OpenFileDialog();
- fdlg.Title = "C# Corner Open File Dialog";
- fdlg.InitialDirectory = @"C:\ ";
- fdlg.Filter =
- "Text files (*.txt | .txt | All files (*.*) | *.*";
- fdlg.FilterIndex = 2;
- fdlg.RestoreDirectory = true;
- if (fdlg.ShowDialog() == DialogResult.OK)
- {
- textBox1.Text = fdlg.FileName;
- }
Step 3
Before we write code on the Print button click event handler, define two private variables on class level.
- private Font verdana10Font;
- private StreamReader reader;
Now import these two namespaces.
- using System.IO;
- using System.Drawing.Printing;
Write the following code on Print button click event handler.
- string filename=textBox1.Text.ToString();
- //Create a StreamReader object
- reader = new StreamReader (filename);
- //Create a Verdana font with size 10
- verdana10Font = new Font ("Verdana", 10);
- //Create a PrintDocument object
- PrintDocument pd = new PrintDocument();
- //Add PrintPage event handler
- pd.PrintPage += new PrintPageEventHandler(this.PrintTextFileHandler);
- //Call Print Method
- pd.Print();
- //Close the reader
- if (reader != null)
- reader.Close();
And add the following method to the class.
- private void PrintTextFileHandler (object sender, PrintPageEventArgs ppeArgs)
- {
- //Get the Graphics object
- Graphics g = ppeArgs.Graphics;
- float linesPerPage = 0;
- float yPos = 0;
- int count = 0;
- //Read margins from PrintPageEventArgs
- float leftMargin = ppeArgs.MarginBounds.Left;
- float topMargin = ppeArgs.MarginBounds.Top;
- string line = null;
- //Calculate the lines per page on the basis of the height of the page and the height of the font
- linesPerPage = ppeArgs.MarginBounds.Height/verdana10Font.GetHeight (g);
- //Now read lines one by one, using StreamReader
- while (count<linesPerPage && (( line = reader.ReadLine ()) != null))
- {
- //Calculate the starting position
- yPos = topMargin + (count * verdana10Font.GetHeight (g));
- //Draw text
- g.DrawString (line, verdana10Font, Brushes.Black,leftMargin, yPos, new StringFormat());
- //Move to next line
- count++;
- }
- //If PrintPageEventArgs has more pages to print
- if (line != null)
- {
- ppeArgs.HasMorePages = true;
- }
- else
- {
- ppeArgs.HasMorePages = false;
- }
- }
Step 4

Sreemanoj JPosted Jan 11, 2019, 2:38 AM
Awesome. Thank you
Nhlanhla FakudePosted Nov 27, 2017, 9:08 AM
This is super, thanks a lot Mahesh.
Ravi KandelPosted Jul 11, 2016, 7:18 AM
Thanks
amit manePosted Oct 24, 2015, 4:03 AM
Up to the Mark. Thank you.
Ganesh DeoPosted Mar 19, 2015, 11:19 PM
Hi Mahesh, I tried your program to print a text file but when i click on print button it is directing me to another dialog window to save a file as .xps instead of printer pls advice "[email protected]"
Ganesh DeoPosted Mar 19, 2015, 11:17 PM
Hi Mahesh,
Bhavik PatelPosted Jul 16, 2014, 4:55 AM
very useful and working for me thanks
Kailash SalviPosted Jun 14, 2014, 6:41 AM
i have to print file present on serverusing web application
Kailash SalviPosted Jun 14, 2014, 6:40 AM
its good ,but i doesnt work on IIS
Philip LivingstonePosted Dec 24, 2013, 3:46 AM
Mahesh this is great, is it possible to print like this using Bold and regular font style?
vipul muralidhar pPosted Jul 27, 2012, 2:03 AM
thanks Mahesh Chand for giving such codes about text printing.
therese HuynhPosted Oct 12, 2011, 7:18 AM
c'est très bien votre projet mais si je veux imprimer les images,ou autre fichiers hors de fichiers Textes ,comment faire SVP
marut mahajanPosted Apr 9, 2011, 3:24 AM
thanx for above code bt i need sum help this code is working right for .txt files bt whn i trying to print files with .doc or .docx or any other it is nt printing in right format or u can say it is printing sum other text lik binary text or anythig else
ABDUL AZIZPosted Apr 8, 2011, 2:29 AM
how to craete bill report in c# visual studio 2008 and also i kindly request u to explain how to include these two name space( using System.Printing;using System.Windows.Documents;)
Mansur AhmedPosted Dec 22, 2010, 1:52 AM
It's fine. But how to print on a Blank cheque in C#.
subhash komminaPosted Oct 13, 2010, 2:08 AM
thank you soo much for this example
melika taPosted Sep 18, 2010, 3:10 AM
sgh
joseph reddyPosted Jul 23, 2010, 2:13 AM
excellent work..i read your articles frequently ,explanation is very good ,keep it up
Abdelkader SoudaniPosted Mar 24, 2010, 5:46 PM
ok cool, works just perfect ! one question though, will it work the same if the printer was connected to another pc in a local network? and how could we check if the printer is ever connected to any machine? thanks for your help
Rey ManjaresPosted Mar 1, 2010, 1:34 AM
Thanks pal, you really made my day... It was really a BIG help... :)
Umar AbbasPosted Oct 2, 2009, 7:04 AM
Every body above is Uncivilized....
Ahmed AlsafiPosted Aug 16, 2009, 3:58 AM
Hi Mr. Mahesh Chand I want to ask a question about printing. I have a right-to-left text and I want to print it starting from the right side of the print document. This is because printing from left-to-right gives me a varying position of my text according to the text size. I need your help sir, thanks.
johny ssPosted Jan 17, 2008, 12:17 AM
i want print txt file. this is like railway ticket.one slip hight 4 inch.i want print one by one.first the detail write in a text file then print. main problem is after printing i want cut smoothly,for that paper cutting point is point to printer cutter.then next print is comming the paper possion form feed mean paper scroll down and point top. here cant point cutting point and next print comming cannot scroll down to point top. what can i do? project is C#.
johny ssPosted Jan 17, 2008, 12:07 AM
dosprint