using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
namespace PrinterDriver
{
public class PRINTER
{
private bool m_Printing;
public PRINTER(string printerName, string fileName)
{
m_Printing = false;
m_PrinterName = printerName;
m_FileName = fileName;
// default settings
m_Landscape = true;
m_LeftMargin = 0;
m_RightMargin = 0;
m_TopMargin = 0;
m_BottomMargin = 0;
m_FontName = "Courier New";
m_FontSize = 9;
leftOver = "";
// create our actual printer document & assign it to a specific printer
printDoc = new PrintDocument();
printDoc.PrinterSettings.PrinterName = m_PrinterName;
// add the event handlers to make everything happy-happy
printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
printDoc.QueryPageSettings += new
QueryPageSettingsEventHandler(printDoc_QueryPageSettings);
}
public static void Print(string printer,string file)
{
PRINTER ptr = new PRINTER(printer,file);
ptr.Print();
}
public void Print()
{
// open our file and start the magic.
m_Printing = true;
try
{
dataStream = new StreamReader(m_FileName);
printDoc.Print();
}
catch( Exception ex )
{
throw (ex);
}
finally
{
if ( dataStream != null )
dataStream.Close();
m_Printing = false;
}
}
private PrintDocument printDoc;
private StreamReader dataStream;
private string leftOver;
private void printDoc_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
// we'll either be working with the next line in the stream or the
leftover from the previous
// page
string strStreamLine = (leftOver.Length!=0)
? leftOver
: dataStream.ReadLine();
// specify our font
Font myFont = new System.Drawing.Font(m_FontName,m_FontSize);
StringFormat fmt = new StringFormat();
// figure out the specifics of the current page which we are printing
PageSettings pgSet = e.PageSettings;
float TopMargin = pgSet.Bounds.Top + pgSet.Margins.Top;
float BottomMargin = pgSet.Bounds.Bottom - pgSet.Margins.Bottom;
float LeftMargin = pgSet.Bounds.Left + pgSet.Margins.Left;
float RightMargin = pgSet.Bounds.Right - pgSet.Margins.Right;
float MarginWidth = RightMargin - LeftMargin;
float MarginHeight = BottomMargin - TopMargin;
float XPosition = LeftMargin;
float YPosition = TopMargin;
// Calculate the number of lines per page.
float LineHeight = myFont.GetHeight(e.Graphics);
int linesPerPage = (int)(e.MarginBounds.Height / LineHeight);
// Prints each line of the file, but stops at the end of a
// page
while( (YPosition+LineHeight<BottomMargin) && (strStreamLine!=null) )
{
// we build up the current line one character at a time, constantly
computing the lines
// width to ensure we aren't exceeding the right margin. when this
happens, we'll have to
// deal with the wordwrap situation
string strCurrentLine = "";
for ( int i = 0; i < strStreamLine.Length; i++ )
{
string strNextChar = strStreamLine.Substring(i,1);
if ( (int)strStreamLine
== 10 || (int)strStreamLine == 12 )
strNextChar = " ";
if ( e.Graphics.MeasureString(strCurrentLine+strNextChar,myFont).Width
{
// we have a wordwrap situation
e.Graphics.DrawString(strCurrentLine,myFont,Brushes.Black,XPosition,YPositio
n,fmt);
strCurrentLine = "";
XPosition = LeftMargin;
YPosition += LineHeight;
// the part that wraps: will it cause us to go beyond the bottom
margin? if so, then
// we need to preserve the remainder of the current line as leftover
for the next page
// that is printed
if ( (YPosition+LineHeight) >= BottomMargin )
{
leftOver = strStreamLine.Substring(i);
e.HasMorePages = true;
return;
}
}
strCurrentLine += strNextChar;
}
e.Graphics.DrawString(strCurrentLine,myFont,Brushes.Black,XPosition,YPositio
n,fmt);
XPosition = LeftMargin;
YPosition += LineHeight;
// don't bother reading the next line if we're going to blow our margin
if ( YPosition + LineHeight < BottomMargin )
strStreamLine = dataStream.ReadLine();
}
// we aren't at the end of the file yet so there is still more to print.
be sure to clear
// the leftover for the next page.
if ( strStreamLine != null )
{
leftOver = "";
e.HasMorePages = true;
}
}
private void printDoc_QueryPageSettings(object sender,
System.Drawing.Printing.QueryPageSettingsEventArgs e)
{
// it isn't that we can actually set these things when printing... oh
no... we can only set them
// when they are requested.
e.PageSettings.Landscape = m_Landscape;
e.PageSettings.Margins.Left = m_LeftMargin;
e.PageSettings.Margins.Right = m_RightMargin;
e.PageSettings.Margins.Top = m_TopMargin;
e.PageSettings.Margins.Bottom = m_BottomMargin;
}
private bool m_Landscape;
private int m_LeftMargin;
private int m_RightMargin;
private int m_TopMargin;
private int m_BottomMargin;
private string m_PrinterName;
private string m_FileName;
private string m_FontName;
private float m_FontSize;
public bool Landscape
{
get { return m_Landscape; }
set { m_Landscape = value; }
}
public int LeftMargin
{
get { return m_LeftMargin; }
set { m_LeftMargin = value; }
}
public int RightMargin
{
get { return m_RightMargin; }
set { m_RightMargin = value; }
}
public int TopMargin
{
get { return m_TopMargin; }
set { m_TopMargin = value; }
}
public int BottomMargin
{
get { return m_BottomMargin; }
set { m_BottomMargin = value; }
}
public string PrinterName
{
get { return m_PrinterName; }
set { m_PrinterName = value; }
}
public string FileName
{
get { return m_FileName; }
set { m_FileName = value; }
}
public string FontName
{
get { return m_FontName; }
set { m_FontName = value; }
}
public float FontSize
{
get { return m_FontSize; }
set { m_FontSize = value; }
}
public bool PrinterValid
{
get { return printDoc.DefaultPageSettings.PrinterSettings.IsValid; }
}
}
}