[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1243
  • Last Modified:

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!
0
cuziyq
Asked:
cuziyq
1 Solution
 
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
 
AnnieModCommented:
PAQ'd and 500 points refunded

AnnieMod
Cleanup Admin
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now