Need a program to print a text file in C#.

I don't really know C# at all.  What I need is a program that will prompt for a text file and then immediately print it to the printer of the user's choosing -- in landscape orientation.

I know .NET has a ton of built-in stuff to deal with this, so I can't imagine such a program being very complex.

One caveat: I do not know if the file will have a .TXT extension (although it will definetly be ASCII), and I cannot make the user jump through hoops (such as changing the default printer or modifying its settings to make it work).  It just need to be 2 clicks . . . one to select the file, and one to shoot it off to the printer.

Standard print/file dialogs are fine.  No font necessary.  Monospace courier is fine as well.

If anyone has some sample code I could use or could point me to an easy-to-understand tutorial that doesn't assume I've never used a computer before, it would be much appreciated.  Thanks!
LVL 14
cuziyqAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

debuggerauCommented:
I suggest you play around with printing an existing txt file initally, the actual form code will be the easy part.
Set it as landscape and try sending a preformatted file to it first.

http://support.microsoft.com/kb/322091

0
cuziyqAuthor Commented:
I found this lovely little gem that solved it for me:
http://msdn2.microsoft.com/en-us/library/aa287530.aspx

I posted the relevant sections of the program below.
		private void printButton_Click(object sender, System.EventArgs e)
		{
			// Allow the user to select a file.
			OpenFileDialog ofd = new OpenFileDialog();
			if( DialogResult.OK == ofd.ShowDialog(this) )
			{
				// Allow the user to select a printer.
				PrintDialog pd  = new PrintDialog();
				pd.PrinterSettings = new PrinterSettings();
				if( DialogResult.OK == pd.ShowDialog(this) )
				{
					string printPath = ofd.FileName;
					fileToPrint = new System.IO.StreamReader(printPath);
					printFont = new System.Drawing.Font("Courier", 12);
					printDocument1.Print();
					fileToPrint.Close();
				}
			}
		}
 
		private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
		{
			float yPos = 0f;
			int count = 0;
			float leftMargin = e.MarginBounds.Left;
			float topMargin = e.MarginBounds.Top;
			string line = null;
			float linesPerPage = e.MarginBounds.Height/printFont.GetHeight(e.Graphics);
			while (count < linesPerPage)
			{
				line = fileToPrint.ReadLine();
				if (line == null)
				{
					break;
				}
				yPos = topMargin + count * printFont.GetHeight(e.Graphics);
				e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
				count++;
			}
			if (line != null)
			{
				e.HasMorePages = true;
			}
		}
 
		private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
		{
		
		}
}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AnnieModCommented:
PAQ'd and 500 points refunded

AnnieMod
Cleanup Admin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.