使用喷墨打印机打印文本文件内容 [英] Print Text File Content using Inkjet Printer

查看:168
本文介绍了使用喷墨打印机打印文本文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用喷墨打印机打印文本文件内容.

I want to know how i will Print my Text File Content using Inkjet Printer

推荐答案

亲爱的,

这是用于打印文本文件内容的类,

创建该类的对象并调用PrintText()方法,然后将参数作为文件名传递给

Dear,

here''s class to print text file content,

create object of the class and call the PrintText() method and pass the parameter as filename

public class PrinterHandler
    {
        String printername;
        StreamReader reader;

        public PrinterHandler(String printername)
        {
            this.printername = printername;
        }

        public void PrintText(string filename)
        {
            try
            {
                FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                reader = new StreamReader(fs);

                //Create a PrintDocument object
                PrintDocument pd = new PrintDocument();
                //Add PrintPage event handler
                pd.PrintPage += new PrintPageEventHandler(this.PrintTextFileHandler);
                //Call Print Method
                pd.Print();
                //Close the reader
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }

        private void PrintTextFileHandler(object sender, PrintPageEventArgs ppeArgs)
        {
            //Create a Trebuchet MS font with size 10
            Font verdana10Font = new Font("Trebuchet MS", 10);

            //Get the Graphics object
            Graphics g = ppeArgs.Graphics;
            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            //Read margins from PrintPageEventArgs
            float leftMargin = ppeArgs.MarginBounds.Left;
            float topMargin = ppeArgs.MarginBounds.Top;
            string line = null;
            //Calculate the lines per page on the basis of the height of the page and the height of the font
            linesPerPage = ppeArgs.MarginBounds.Height / verdana10Font.GetHeight(g);
            //Now read lines one by one, using StreamReader
            while (count < linesPerPage && ((line = reader.ReadLine()) != null))
            {
                //Calculate the starting position
                yPos = topMargin + (count * verdana10Font.GetHeight(g));
                //Draw text
                g.DrawString(line, verdana10Font, Brushes.Black, leftMargin, yPos, new StringFormat());
                //Move to next line
                count++;
            }

            //If PrintPageEventArgs has more pages to print
            if (line != null)
            {
                ppeArgs.HasMorePages = true;
            }
            else
            {
                ppeArgs.HasMorePages = false;
            }

        }
    }



不要忘记导入:



dont forget to import :

using System.Drawing;
using System.Drawing.Printing;


这篇关于使用喷墨打印机打印文本文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆