C implements simple printing of the instance code
- 2020-05-09 19:09:01
- OfStack
The main form code is as follows:
public partial class PrintFileForm : Form
{
public PrintFileForm()
{
InitializeComponent();
PrintFile prinFile = new PrintFile();
prinFile.Print();
}
}
The type of print file is as follows:
class PrintFile
{
StreamReader sr = null;
Font printFont = new Font(" Song typeface ", 12);
public void Print()
{
try
{
sr = new StreamReader(@"F:\Temp.txt");
try
{
PrintDocument printDoc = new PrintDocument();
printDoc.PrintPage += printDoc_PrintPage;
printDoc.Print();
}
finally
{
sr.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
void printDoc_PrintPage(object sender, PrintPageEventArgs e)
{
string line = null;
// Set up the 1 The number of rows = The height of the print area divided by the height of the font .
float pageLine = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
// Cycle print per 1 line
for (int count = 0; count < pageLine && ((line=sr.ReadLine())!=null); count++)
{
float singleLine=e.MarginBounds.Top+(count*printFont.GetHeight(e.Graphics));
e.Graphics.DrawString(line, printFont, Brushes.Black, e.MarginBounds.Left, singleLine);
}
// Determine whether to continue printing
if (line != null)
e.HasMorePages = true;
else
e.HasMorePages = false;
}
}