A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© aisini 金牌黑马   /  2014-7-30 11:41  /  1809 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

使用PrintDocument控件打印票据或者文本内容时,根据内容的的高度进行不分页打印。
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Printing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;

  11. namespace pdPrint
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         private int lineSize;                //每行打印字数
  16.         private List<string> textList;       //打印内容行
  17.         private int lineHeight;              //打印行高
  18.         private int fontSize;                //字大小
  19.         public Form1()
  20.         {
  21.             lineSize = 20;                   //设置每行打印字数
  22.             lineHeight = 22;                 //行高  1/100 英寸
  23.             fontSize = 12;                   //字体大小 1/英寸
  24.             InitializeComponent();
  25.         }

  26.         private void btn_Print_Click(object sender, EventArgs e)
  27.         {
  28.             if (string.IsNullOrWhiteSpace(this.txt_PrintText.Text))
  29.             {
  30.                 return;
  31.             }

  32.             //原文字行或者段落内容
  33.             var sourceTexts = this.txt_PrintText.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

  34.             //重新把文字进行分行树立
  35.             textList = new List<string>();
  36.             foreach (var item in sourceTexts)
  37.             {
  38.                 if (!string.IsNullOrWhiteSpace(item))
  39.                 {
  40.                     if (item.Length > lineSize)
  41.                     {
  42.                         textList.AddRange(GetArr(lineSize, item));
  43.                     }
  44.                     else
  45.                     {
  46.                         textList.Add(item);
  47.                     }
  48.                 }
  49.             }


  50.             PrintDocument pd = new PrintDocument();
  51.             pd.PrintPage += new PrintPageEventHandler(Print_Content);
  52.             //纸张设置默认
  53.             PaperSize pageSize = new PaperSize("自定义纸张", fontSize * lineSize, (textList.Count * lineHeight));
  54.             pd.DefaultPageSettings.PaperSize = pageSize;
  55.             try
  56.             {
  57.                 pd.Print();
  58.             }
  59.             catch (Exception ex)
  60.             {
  61.                 MessageBox.Show("打印失败." + ex.Message);
  62.             }
  63.         }

  64.         /// <summary>
  65.         /// 打印内容事件
  66.         /// </summary>
  67.         /// <param name="sender"></param>
  68.         /// <param name="e"></param>
  69.         private void Print_Content(object sender, PrintPageEventArgs e)
  70.         {
  71.             var mark = 0;
  72.             foreach (var item in textList)
  73.             {
  74.                 e.Graphics.DrawString(item, new Font(new FontFamily("宋体"), fontSize), System.Drawing.Brushes.Black, 0, mark * lineSize);
  75.                 mark++;
  76.             }
  77.         }

  78.         /// <summary>
  79.         /// 根据内容进行分行
  80.         /// </summary>
  81.         /// <param name="linelen">每行字数</param>
  82.         /// <param name="text">原文字行(段落)文字</param>
  83.         /// <returns></returns>
  84.         private List<string> GetArr(int linelen, string text)
  85.         {
  86.             var list = new List<string>();
  87.             int listcount = text.Length % linelen == 0 ? text.Length / linelen : (text.Length / linelen) + 1;
  88.             for (int j = 0; j < listcount; j++)
  89.             {
  90.                 try
  91.                 {
  92.                     list.Add(text.Substring(j * linelen, linelen));
  93.                 }
  94.                 catch (Exception)
  95.                 {
  96.                     list.Add(text.Substring(j * linelen));
  97.                 }
  98.             }
  99.             return list;
  100.         }
  101.     }
  102. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马