在C#中实现打印功能

在.NET中可以很方便的实现打印功能。

通常可以创建 PrintDocument 类的实例,设置描述打印方式的属性,然后调用 Print 方法开始打印进程。通过使用 PrintPageEventArgs 中包含的 Graphics 来处理用于指定打印输出的 PrintPage 事件。

具体代码:

public partial class PrintingExample : Form
    {
        public PrintingExample():base()
        {
            InitializeComponent();
        }
        private Font printFont;
        private StreamReader streamToPrint;

        private void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                streamToPrint = new StreamReader("c://Documents//MyFile.txt",System.Text.Encoding.Default  );
                try
                {
                    printFont = new Font ("arial",10);
                    PrintDocument pd = new PrintDocument ();
                    pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage );
                    pd.Print ();
                }
                finally
                {
                    streamToPrint.Close ();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            string line = null;
            linesPerPage = ev.MarginBounds.Height /
                printFont.GetHeight(ev.Graphics);

            while (count < linesPerPage &&
                ((line = streamToPrint.ReadLine()) != null))
            {
                yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
                ev.Graphics.DrawString(line ,printFont ,Brushes.Blue,leftMargin ,yPos , new StringFormat () );
                count ++;
            }
            if(line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;
        }

之后调试时StreamReader读取中文文本出现乱码,原来是没有搞明白.NET处理文件默认的编码方式。

从Windows 2000之后的操作系统在文件处理时默认编码采用Unicode,所以.Net 的文件默认编码也是Unicode。除非另外指定,StreamReader 的默认编码为 Unicode,而不是当前系统的 ANSI 代码页。但是文档大部分还是以ANSI编码储存,中文文本使用的是gb2312,所以才造成中文乱码的状况,也就是说在读取文本的时候要指定编码格式。
但是问题来了,System.Text.Encoding 里面一堆编码格式ASCII、UTF-8等等,要选哪一个好?
其实很简单,用 System.Text.Encoding.Default 告诉 StreamReader 目前操作系统的编码即可。

StreamReader reader = new StreamReader(FileName, System.Text.Encoding.Default)

C#实现打印功能的步骤如下: 1. 引入命名空间 ```csharp using System.Drawing.Printing; using System.Windows.Forms; ``` 2. 创建打印对话框 ```csharp PrintDialog printDialog = new PrintDialog(); ``` 3. 显示打印对话框 ```csharp if (printDialog.ShowDialog() == DialogResult.OK) { // 用户点击了“打印”按钮 } ``` 4. 创建打印文档对象 ```csharp PrintDocument printDocument = new PrintDocument(); ``` 5. 设置打印文档的打印机 ```csharp printDocument.PrinterSettings = printDialog.PrinterSettings; ``` 6. 设置打印文档的打印事件 ```csharp printDocument.PrintPage += new PrintPageEventHandler(PrintPage); ``` 7. 实现打印事件 ```csharp private void PrintPage(object sender, PrintPageEventArgs e) { // 在此处编写打印逻辑 } ``` 8. 开始打印 ```csharp printDocument.Print(); ``` 完整的代码示例: ```csharp using System.Drawing.Printing; using System.Windows.Forms; public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { PrintDialog printDialog = new PrintDialog(); if (printDialog.ShowDialog() == DialogResult.OK) { PrintDocument printDocument = new PrintDocument(); printDocument.PrinterSettings = printDialog.PrinterSettings; printDocument.PrintPage += new PrintPageEventHandler(PrintPage); printDocument.Print(); } } private void PrintPage(object sender, PrintPageEventArgs e) { e.Graphics.DrawString("Hello, World!", new Font("Arial", 12), Brushes.Black, new PointF(0, 0)); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值