Here is code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace PrintTextFile
{
public partial class Form1 : Form
{
string str;
Font myFont = new Font("Arial", 12);
public Form1()
{
InitializeComponent();
}
private void bttnOpen_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Text files (*.txt)|*.txt";
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
{
str = (sr.ReadLine());// textbox index
}
}
}
private void bttnPreview_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
private void bttnPrint_Click(object sender, EventArgs e)
{
printDocument1.Print();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Point p = new Point(10,10);//start printing at 10 across and 10 down
e.Graphics.DrawString(str, myFont, Brushes.Black, p);
}
private void bttnPrintDialog_Click(object sender, EventArgs e)
{
printDialog1.Document = printDocument1;
printDialog1.ShowDialog();
}
}
}
Make sure you have printDocument1, printPreviewDialog1 and printDialog1 in your tray (ie drag them from the toolbox)