zoukankan      html  css  js  c++  java
  • C# 实现简单打印(二)打印一个文本文档,打印的内容是多行的

    主窗体代码如下:

        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; 
                //设置一页的行数=打印区域的高度除以字体高度.
                float pageLine = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
                //循环打印每一行
                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);
                }
                //判断是否继续打印
                if (line != null)
                    e.HasMorePages = true;
                else
                    e.HasMorePages = false;
    
            }
        }
  • 相关阅读:
    Fluxbox 1.0 RC 2
    Cairo 1.2.0
    Transmission 0.6.1
    Xara LX Xtreme 0.5 Rev1175
    Totem 1.4.2
    ImageMagick 6.2.81
    恢复 Firefox 和 Thunderbird 图标
    Conky 1.4.2
    BitTorrent 4.20.0
    Geany 疾速简便的集成启示情况
  • 原文地址:https://www.cnblogs.com/lqsilly/p/2947422.html
Copyright © 2011-2022 走看看