c# print file code example
Example 1: how to print a text c# code
Console.WriteLine("Type inside here what you want to print");
Example 2: print file c#
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float yPos = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
ev.Graphics.DrawString(sr.ReadToEnd(), printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
}
private void CallPrint() {
printFont = new Font("Arial", 15);
sr = new StreamReader(@"MyFileToPrint.txt");
PrintDialog printDlg = new PrintDialog();
PrintDocument printDoc = new PrintDocument();
if (printDlg.ShowDialog() == DialogResult.OK)
{
printDoc.PrintPage += new PrintPageEventHandler(pd_PrintPage);
printDoc.Print();
}
}
Example 3: print a file from C#
private void SendToPrinter()
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = @"c:\output.pdf";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(3000);
if (false == p.CloseMainWindow())
p.Kill();
}