C#中进行文本打印的功能
来源:互联网 发布:淘宝购物最便宜的软件 编辑:程序博客网 时间:2024/05/22 09:27
问题描述:
一个记事本程序,要求能按标准打印其中的文档,包括在每行文字数目上进行控制等、
解决方法:
一、搞清楚打印的过程:
1、定义PrintDocument类,并且声明其PrintPage事件。
- private void PrintDocument()
- {
- printDocument = new PrintDocument();
- printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
- }
2、实现PrintPage事件(关键):
- /**//// <summary>
- /// 用来处理打印过程的核心事件。
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- /// <remarks></remarks>
- void printDocument_PrintPage(object sender, PrintPageEventArgs e)
- {
- //StringReader lineReader = new StringReader(textBox.Text);
- Graphics graphic = e.Graphics;//获取绘图对象
- float linesPerPage = 0;//页面行号
- float yPosition = 0;//绘制字符串的纵向位置
- float leftMargin = e.MarginBounds.Left;//左边距
- float topMargin = e.MarginBounds.Top;//上边距
- string line = string.Empty;//读取的行字符串
- int currentPageLine=0;//当前页读取的行数
- Font charFont = richTextBox1.Font;//获取打印字体
- SolidBrush brush = new SolidBrush(Color.Black);//刷子
- linesPerPage = e.MarginBounds.Height / charFont.GetHeight(graphic);//每页可打印的行数
- //countNum记录全局行数,currentPageLine记录当前打印页行数。
- while (countNum < richTextBox1.Lines.Length)
- {
- if (currentPageLine < linesPerPage)
- {
- line = richTextBox1.Lines[countNum].ToString();
- if (line.Length <= 50)
- {
- yPosition = topMargin + (currentPageLine * charFont.GetHeight(graphic));
- //绘制当前行
- graphic.DrawString(line, charFont, brush, leftMargin, yPosition, new StringFormat());
- countNum++;
- currentPageLine++;
- }
- else
- {
- //拆分后的行数
- int moreLine = line.Length / 50 + 1;
- string tempLine;
- for (int i = 0; i < moreLine; i++)
- {
- //获得当前行的子串
- if ((line.Length - i * 50) >= 50)
- {
- tempLine = line.Substring(i * 50, 50);
- }
- else
- {
- tempLine = line.Substring(i * 50, line.Length - i * 50);
- }
- yPosition = topMargin + (currentPageLine * charFont.GetHeight(graphic));
- //绘制当前行
- graphic.DrawString(tempLine, charFont, brush, leftMargin, yPosition, new StringFormat());
- //当前打印页行数加1
- currentPageLine++;
- }
- //总行数加1
- countNum++;
- }
- }
- else
- {
- line = null;
- break;
- }
- }
- //一页显示不完时自动重新调用此方法
- if (line == null)
- {
- e.HasMorePages = true;
- }
- else
- {
- e.HasMorePages = false;
- }
- //每次打印完后countNum清0;
- if (countNum >= richTextBox1.Lines.Length)
- {
- countNum = 0;
- }
- }
3、使用 printDocument.Print()进行打印
- private void 打印PToolStripButton_Click(object sender, EventArgs e)
- {
- // tsmSave_Click(sender, e);
- //初始化PrintDocument()
- this.PrintDocument();
- PrintDialog print = new PrintDialog();
- //将初始化后的printDocument赋给print.Document
- print.Document = printDocument;
- try
- {
- if (print.ShowDialog() == DialogResult.OK)
- {
- //打印时直接调用PrintDocument的Print方法
- printDocument.Print();
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("打印出错:" + ex.ToString());
- //出错则结束打印过程
- printDocument.PrintController.OnEndPrint(printDocument, new PrintEventArgs());
- }
- }
4、定义打印预览(可选) \、、、、、、、
private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
{
PrintPreviewDialog printPreview = new PrintPreviewDialog();
//初始化PrintDocument
this.PrintDocument();
//将初始化后的printDocument赋给print.Document
printPreview.Document = printDocument;
try
{
printPreview.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
{
PrintPreviewDialog printPreview = new PrintPreviewDialog();
//初始化PrintDocument
this.PrintDocument();
//将初始化后的printDocument赋给print.Document
printPreview.Document = printDocument;
try
{
printPreview.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
- C#中进行文本打印的功能
- 初探C#的打印功能
- c#中实现朗读文本功能
- 在C#中实现打印功能
- 在C#中实现打印功能
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)实用的类
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- 在 C# 中实现打印功能(C# 中 PrintDialog,PrintDocument 的使用)
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- C#对文本进行简单的写入
- 克隆丛库.NO.2(50G以下数据量使用)
- android:process=":remote" .
- Home键监听与电源键
- vs2010 环境设置
- sqldeveloper 7注册码
- C#中进行文本打印的功能
- Symfony2学习笔记之数据校验
- Windows8系统下Windows Phone 8开发环境搭建
- 在Wireshark里加载lua
- Jena API 使用介绍
- 设计模式初探(四)——行为:问题为设计而生
- 黑马程序员-高新技术 1
- Symfony2学习笔记之HTTP Cache
- 分享我做的nginx+keepalived做的主主架构(一)