[.Net码农]PrintDocument.Print 方法

http://msdn.microsoft.com/zh-cn/library/system.drawing.printing.printdocument.print(v=vs.90).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-4

命名空间:  System.Drawing.Printing

程序集:  System.Drawing(在 System.Drawing.dll 中)


指定打印输出的方法是处理 PrintPage 事件并使用 PrintPageEventArgs 中包含的 Graphics

使用 PrinterSettings.PrinterName 属性可指定用哪一台打印机来打印文档。

Print 方法在打印文档时不使用打印对话框。若要为用户提供选择打印设置的能力,则使用 PrintDialog





下面的代码示例将通过命令行指定的文件打印到默认打印机。


此示例要求每一行都适合于页宽。

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

 public class PrintingExample 
 {
     private Font printFont;
     private StreamReader streamToPrint;
     static string filePath;


     public PrintingExample() 
     {
         Printing();
     }

     // The PrintPage event is raised for each page to be printed.
     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;

         // Calculate the number of lines per page.
         linesPerPage = ev.MarginBounds.Height  / 
            printFont.GetHeight(ev.Graphics) ;

         // Iterate over the file, printing each line.
         while (count < linesPerPage && 
            ((line=streamToPrint.ReadLine()) != null)) 
         {
            yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
            ev.Graphics.DrawString (line, printFont, Brushes.Black, 
               leftMargin, yPos, new StringFormat());
            count++;
         }

         // If more lines exist, print another page.
         if (line != null) 
            ev.HasMorePages = true;
         else 
            ev.HasMorePages = false;
     }

     // Print the file.
     public void Printing()
     {
         try 
         {
            streamToPrint = new StreamReader (filePath);
            try 
            {
               printFont = new Font("Arial", 10);
               PrintDocument pd = new PrintDocument(); 
               pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
               // Print the document.
               pd.Print();
            } 
            finally 
            {
               streamToPrint.Close() ;
            }
        } 
        catch(Exception ex) 
        { 
            MessageBox.Show(ex.Message);
        }
     }

     // This is the main entry point for the application.
     public static void Main(string[] args) 
     {
        string sampleName = Environment.GetCommandLineArgs()[0];
        if(args.Length != 1)
        {
           Console.WriteLine("Usage: " + sampleName +" <file path>");
           return;
        }
        filePath = args[0];
        new PrintingExample();
     }
 }





下面的代码示例将通过命令行指定的文件打印到默认打印机。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值