C# 實(shí)現(xiàn)簡單打印的實(shí)例代碼
主窗體代碼如下:
public partial class PrintFileForm : Form
{
public PrintFileForm()
{
InitializeComponent();
PrintFile prinFile = new PrintFile();
prinFile.Print();
}
}
打印文件類如下:
class PrintFile
{
StreamReader sr = null;
Font printFont = new Font("宋體", 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;
//設(shè)置一頁的行數(shù)=打印區(qū)域的高度除以字體高度.
float pageLine = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
//循環(huán)打印每一行
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);
}
//判斷是否繼續(xù)打印
if (line != null)
e.HasMorePages = true;
else
e.HasMorePages = false;
}
}
相關(guān)文章
DevExpress實(shí)現(xiàn)禁用TreeListNode CheckBox的方法
這篇文章主要介紹了DevExpress實(shí)現(xiàn)禁用TreeListNode CheckBox的方法,在項(xiàng)目開發(fā)中有應(yīng)用價值,需要的朋友可以參考下2014-08-08WPF ComboBox獲取當(dāng)前選擇值的實(shí)例詳解
這篇文章主要介紹了WPF ComboBox獲取當(dāng)前選擇值的實(shí)例詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01C#常用多線程(線程同步,事件觸發(fā),信號量,互斥鎖,共享內(nèi)存,消息隊(duì)列)
這篇文章主要介紹了C#常用多線程(線程同步,事件觸發(fā),信號量,互斥鎖,共享內(nèi)存,消息隊(duì)列),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09C#設(shè)計模式之Facade外觀模式解決天河城購物問題示例
這篇文章主要介紹了C#設(shè)計模式之Facade外觀模式解決天河城購物問題,簡單描述了外觀模式的定義并結(jié)合具體實(shí)例分析了外觀模式解決購物問題的相關(guān)步驟與操作技巧,需要的朋友可以參考下2017-09-09Winform ComboBox如何獨(dú)立繪制下拉選項(xiàng)的字體顏色
這篇文章主要介紹了Winform ComboBox如何獨(dú)立繪制下拉選項(xiàng)的字體顏色,幫助大家更好的理解和使用c# winform,感興趣的朋友可以了解下2020-11-11