Code:
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace PrintEvents_c
{
public partial class PrintEvents : Form
{
private Font PrintFont;
private StreamReader PrintStream;
public PrintEvents()
{
InitializeComponent();
}
public void Print_It()
{
try
{
//Get the file to print
PrintStream = new StreamReader("html.docx");
try
{
PrintFont = new Font("Times New Roman", 14);
PrintDocument pd = new PrintDocument();
//Assign my overloaded version of the standard print controller
//Send it a reference to the label so it can tell us what is
//going on.
pd.PrintController = new MyPrintController(ref lblEvents);
//Install event handlers
pd.BeginPrint += new PrintEventHandler(this.pd_StartPrint);
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.EndPrint += new PrintEventHandler(this.pd_EndPrint);
// Print the document.
pd.Print();
}
finally
{
PrintStream.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void pd_StartPrint(object sender, PrintEventArgs ev)
{
lblEvents.Text += "PrintDocument: BeginPrint\n";
}
private void pd_EndPrint(object sender, PrintEventArgs ev)
{
lblEvents.Text += "PrintDocument: EndPrinting\n";
}
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
String line = null;
lblEvents.Text += "PrintDocument: PagePrint\n";
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height / PrintFont.GetHeight(ev.Graphics);
// Iterate over the file, printing each line. Use a basic StringFormat
while (count++ < linesPerPage && ((line = PrintStream.ReadLine()) != null))
{
//Calculate vertical position of the line.
yPos = topMargin + (count * PrintFont.GetHeight(ev.Graphics));
//This is the graphics object obtained by the PrintController
//OnStartPage method. We are drawing to the printer!!
ev.Graphics.DrawString(line, PrintFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
private void cmdPrint_Click(object sender, System.EventArgs e)
{
Print_It();
}
}
public class MyPrintController : StandardPrintController
{
private Label lblEvents;
public MyPrintController(ref Label lbl)
: base()
{
lblEvents = lbl;
}
public override void OnStartPrint(PrintDocument doc, PrintEventArgs e)
{
lblEvents.Text += " PrintController: OnStartPrint\n";
base.OnStartPrint(doc, e);
}
public override Graphics OnStartPage(PrintDocument doc, PrintPageEventArgs e)
{
lblEvents.Text += " PrintController: OnStartPage\n";
return (base.OnStartPage(doc, e));
}
public override void OnEndPage(PrintDocument doc, PrintPageEventArgs e)
{
lblEvents.Text += " PrintController: OnEndPage\n";
base.OnEndPage(doc, e);
}
public override void OnEndPrint(PrintDocument doc, PrintEventArgs e)
{
lblEvents.Text += " PrintController: OnEndPrint\n";
base.OnEndPrint(doc, e);
}
}
}
i try this coding but it not working propley