简单文本打印,PrintDocument使用

  1. using System;
  2. using System.Text;
  3. using System.Drawing;
  4. using System.Drawing.Printing;
  5. using System.Windows.Forms;
  6. using System.IO;
  7. namespace TextPrinter
  8. {
  9.         public class TextPrinter
  10.         {
  11.                 private string m_Title;
  12.                 private string m_Context;
  13.                 private Font m_Font;
  14.                 private Font m_TitleFont;
  15.                 private SizeF m_WordSize;
  16.                 private int m_CurrentPage;
  17.                 private Rectangle m_PrintableArea;
  18.                 private string m_LeftContext;
  19.                 private int m_RowPadding;
  20.                 private TitleType m_TitleType;
  21.                 private FooterType m_FooterType;
  22.                 //private int m_TotalPage;
  23.                 //private bool m_Prepare;
  24.                 private string m_FooterFormat;
  25.                 private PrintDocument printDoc;
  26.                 public TextPrinter()
  27.                 {
  28.                         printDoc = new PrintDocument();
  29.                         printDoc.PrintPage += new PrintPageEventHandler( pd_PrintPage );
  30.                         m_Font = SystemFonts.DefaultFont;
  31.                         m_TitleFont = SystemFonts.CaptionFont;
  32.                         m_TitleType = TitleType.None;
  33.                         m_FooterType = FooterType.None;
  34.                         FooterFormat = "第{0}页";
  35.                 }
  36.                 /**/
  37.                 /// <summary>
  38.                 /// Report Title
  39.                 /// </summary>
  40.                 public string Title
  41.                 {
  42.                         get { return m_Title; }
  43.                         set { m_Title = value; }
  44.                 }
  45.                 /**/
  46.                 /// <summary>
  47.                 /// Report Context string
  48.                 /// </summary>
  49.                 public string Context
  50.                 {
  51.                         get { return m_Context; }
  52.                         set { m_Context = value; }
  53.                 }
  54.                 /**/
  55.                 /// <summary>
  56.                 /// Report Context Font
  57.                 /// </summary>
  58.                 public Font Font
  59.                 {
  60.                         get { return m_Font; }
  61.                         set { m_Font = value; }
  62.                 }
  63.                 /**/
  64.                 /// <summary>
  65.                 /// Report Title Font
  66.                 /// </summary>
  67.                 public Font TitleFont
  68.                 {
  69.                         get { return m_TitleFont; }
  70.                         set { m_TitleFont = value; }
  71.                 }
  72.                 /**/
  73.                 /// <summary>
  74.                 /// Report Title Type
  75.                 /// </summary>
  76.                 public TitleType TitleType
  77.                 {
  78.                         get { return m_TitleType; }
  79.                         set { m_TitleType = value; }
  80.                 }
  81.                 /**/
  82.                 /// <summary>
  83.                 /// Report Footer Type
  84.                 /// </summary>
  85.                 public FooterType FooterType
  86.                 {
  87.                         get { return m_FooterType; }
  88.                         set { m_FooterType = value; }
  89.                 }
  90.                 /**/
  91.                 /// <summary>
  92.                 /// Report Footer Format. etc "第{0}页"
  93.                 /// </summary>
  94.                 public string FooterFormat
  95.                 {
  96.                         get { return m_FooterFormat; }
  97.                         set { m_FooterFormat = value; }
  98.                 }
  99.                 //public int TotalPage
  100.                 //{
  101.                 //    get { return m_TotalPage; }
  102.                 //}
  103.                 /**/
  104.                 /// <summary>
  105.                 /// Report row padding. 行间距
  106.                 /// </summary>
  107.                 public int RowPadding
  108.                 {
  109.                         get { return m_RowPadding; }
  110.                         set { m_RowPadding = value; }
  111.                 }
  112.                 /**/
  113.                 /// <summary>
  114.                 /// 初始化打印
  115.                 /// </summary>
  116.                 private void InitPrint()
  117.                 {
  118.                         m_CurrentPage = 1;
  119.                 }
  120.                 /**/
  121.                 /// <summary>
  122.                 /// 打印机设置
  123.                 /// </summary>
  124.                 public void PrintSetting()
  125.                 {
  126.                         PrintDialog pd = new PrintDialog();
  127.                         pd.Document = printDoc;
  128.                         pd.ShowDialog();
  129.                 }
  130.                 /**/
  131.                 /// <summary>
  132.                 /// 纸张设置
  133.                 /// </summary>
  134.                 public void PageSetting()
  135.                 {
  136.                         PageSetupDialog psd = new PageSetupDialog();
  137.                         psd.Document = printDoc;
  138.                         psd.ShowDialog();
  139.                 }
  140.                 /**/
  141.                 /// <summary>
  142.                 /// 预览报表
  143.                 /// </summary>
  144.                 public void Preview()
  145.                 {
  146.                         InitPrint();
  147.                         PrintPreviewDialog pdlg = new PrintPreviewDialog();
  148.                         pdlg.Document = printDoc;
  149.                         pdlg.WindowState = FormWindowState.Maximized;
  150.                         pdlg.PrintPreviewControl.Zoom = 1;
  151.                         //printDoc.Print();
  152.                         //m_Prepare = true;
  153.                         pdlg.ShowDialog();
  154.                 }
  155.                 /**/
  156.                 /// <summary>
  157.                 /// 打印报表
  158.                 /// </summary>
  159.                 public void Print()
  160.                 {
  161.                         InitPrint();
  162.                         printDoc.Print();
  163.                 }
  164.                 /**/
  165.                 /// <summary>
  166.                 /// 从内容Context中读出一行
  167.                 /// </summary>
  168.                 /// <param name="g"></param>
  169.                 /// <returns></returns>
  170.                 private string GetLine( Graphics g )
  171.                 {
  172.                         string tmp = String.Empty;
  173.                         try
  174.                         {
  175.                                 SizeF size;
  176.                                 for ( int i = 0; i < m_LeftContext.Length; i++ )
  177.                                 {
  178.                                         size = g.MeasureString( m_LeftContext.Substring( 0, i ), Font );
  179.                                         if ( size.Width > m_PrintableArea.Width )
  180.                                         {
  181.                                                 tmp = m_LeftContext.Substring( 0, i - 1 );
  182.                                                 m_LeftContext = m_LeftContext.Substring( i - 1 );
  183.                                                 return tmp;
  184.                                         }
  185.                                         if ( m_LeftContext.Substring( 0, i ).IndexOf( "/n", 0 ) >= 0 )
  186.                                         {
  187.                                                 tmp = m_LeftContext.Substring( 0, i );
  188.                                                 m_LeftContext = m_LeftContext.Substring( i );
  189.                                                 return tmp;
  190.                                         }
  191.                                 }
  192.                                 tmp = m_LeftContext;
  193.                                 m_LeftContext = String.Empty;
  194.                                 return tmp;
  195.                         }
  196.                         finally
  197.                         {
  198.                                 tmp = tmp.Replace( "/n""" );
  199.                         }
  200.                 }
  201.                 /**/
  202.                 /// <summary>
  203.                 /// 打印主过程
  204.                 /// </summary>
  205.                 /// <param name="sender"></param>
  206.                 /// <param name="e"></param>
  207.                 private void pd_PrintPage( object sender, PrintPageEventArgs e )
  208.                 {
  209.                         int top = 0;
  210.                         if ( m_CurrentPage == 1 )
  211.                         {
  212.                                 m_LeftContext = Context;
  213.                                 m_LeftContext = m_LeftContext.Replace( "/r""" );
  214.                                 m_PrintableArea = e.MarginBounds;
  215.                                 m_WordSize = e.Graphics.MeasureString( "W", Font );
  216.                                 if ( TitleType == TitleType.OnlyFirstPage )
  217.                                         top += DrawTitle( e.Graphics );
  218.                         }
  219.                         if ( TitleType == TitleType.AllPage )
  220.                                 top += DrawTitle( e.Graphics );
  221.                         while ( top < m_PrintableArea.Height - ( int )Math.Floor( m_WordSize.Height ) )
  222.                         {
  223.                                 string tmp = GetLine( e.Graphics );
  224.                                 e.Graphics.DrawString( tmp, Font, SystemBrushes.MenuText, e.MarginBounds.X, e.MarginBounds.Y + top );
  225.                                 top += ( int )Math.Floor( m_WordSize.Height ) + RowPadding;
  226.                         }
  227.                         if ( FooterType != FooterType.None )
  228.                                 DrawFooter( e.Graphics );
  229.                         e.HasMorePages = m_LeftContext != string.Empty;
  230.                         m_CurrentPage++;
  231.                 }
  232.                 /**/
  233.                 /// <summary>
  234.                 /// Print Report Title
  235.                 /// </summary>
  236.                 /// <param name="g"></param>
  237.                 /// <returns></returns>
  238.                 private int DrawTitle( Graphics g )
  239.                 {
  240.                         SizeF size = g.MeasureString( Title, TitleFont );
  241.                         DrawString( g, Title, TitleFont, SystemBrushes.WindowText, m_PrintableArea, StringAlignment.Center, StringAlignment.Near );
  242.                         return ( int )Math.Floor( size.Height );
  243.                 }
  244.                 /**/
  245.                 /// <summary>
  246.                 /// Print Report Footer
  247.                 /// </summary>
  248.                 /// <param name="g"></param>
  249.                 private void DrawFooter( Graphics g )
  250.                 {
  251.                         string tmp = string.Empty;
  252.                         if ( FooterType == FooterType.OnlyPageNum )
  253.                                 tmp = string.Format( FooterFormat, m_CurrentPage );
  254.                         //else if (FooterType == FooterType.PageNumOfTotal)
  255.                         //    tmp = string.Format("{0} of {1}", m_CurrentPage, 0);
  256.                         SizeF size = g.MeasureString( tmp, SystemFonts.DefaultFont );
  257.                         DrawString( g, tmp, SystemFonts.DefaultFont, SystemBrushes.WindowText,
  258.                                 new Rectangle( m_PrintableArea.X, m_PrintableArea.Bottom - ( int )size.Height,
  259.                                 m_PrintableArea.Width, ( int )size.Height ), StringAlignment.Far, StringAlignment.Center );
  260.                 }
  261.                 /**/
  262.                 /// <summary>
  263.                 /// Draw a string with alignment parameter
  264.                 /// </summary>
  265.                 /// <param name="g"></param>
  266.                 /// <param name="s"></param>
  267.                 /// <param name="font"></param>
  268.                 /// <param name="brush"></param>
  269.                 /// <param name="rect"></param>
  270.                 /// <param name="alignment"></param>
  271.                 /// <param name="lineAlignment"></param>
  272.                 private void DrawString( Graphics g, string s, Font font, Brush brush, Rectangle rect, StringAlignment alignment, StringAlignment lineAlignment )
  273.                 {
  274.                         StringFormat sf = new StringFormat();
  275.                         sf.Alignment = alignment;
  276.                         sf.LineAlignment = lineAlignment;
  277.                         g.DrawString( s, font, brush, rect, sf );
  278.                 }
  279.                 public static void TestPreview( string pStr )
  280.                 {
  281.                         TextPrinter textPrinter = new TextPrinter();
  282.                         textPrinter.Context = pStr;
  283.                         textPrinter.Font = new Font( "宋体", 16, FontStyle.Regular, GraphicsUnit.Pixel );
  284.                         textPrinter.Title = "打印测试页";
  285.                         textPrinter.TitleFont = new Font( "宋体", 24, FontStyle.Bold, GraphicsUnit.Pixel );
  286.                         textPrinter.TitleType = TitleType.AllPage;
  287.                         textPrinter.FooterType = FooterType.OnlyPageNum;
  288.                         textPrinter.FooterFormat = "Page {0}";
  289.                         //textPrinter.PrintSetting();
  290.                         //textPrinter.PageSetting();
  291.                         textPrinter.Preview();
  292.                         //textPrinter.Print();
  293.                 }
  294.         }
  295.         public enum TitleType
  296.         {
  297.                 None,
  298.                 OnlyFirstPage,
  299.                 AllPage
  300.         }
  301.         public enum FooterType
  302.         {
  303.                 None,
  304.                 OnlyPageNum
  305.                 //PageNumOfTotal
  306.         }
  307. }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值