C# 使用打印
2014-03-30 13:57
246 查看
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace PrintdDocument { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void pageSetupToolStripMenuItem1_Click(object sender, EventArgs e) { printDialog1.ShowDialog(); } private void PagePreviewToolStripMenuItem_Click(object sender, EventArgs e) { try { printPreviewDialog1.ShowDialog(); } catch(Exception ex) { MessageBox.Show(ex.Message,"打印出错",MessageBoxButtons.OK,MessageBoxIcon.Error); } } private void printToolStripMenuItem_Click(object sender, EventArgs e) { try { printDocument1.Print(); } catch(Exception ex) { MessageBox.Show(ex.Message,"打印出错",MessageBoxButtons.OK,MessageBoxIcon.Error); } } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { Graphics g = e.Graphics;//获得绘图对象 float linesPerPage = 0;//页面行号 float yPosition = 0;//绘制字符串的纵向位置 int count = 0;//行计数器 float leftMargin = e.MarginBounds.Left;//左边距 float topMargin = e.MarginBounds.Top;//上边距 string line = null;//行字符串 StringReader lineReader = new StringReader(richTextBox1.Text); Font printFont = this.richTextBox1.Font;//当前的打印字体 SolidBrush myBrush = new SolidBrush(Color.Black);//刷子 linesPerPage = e.MarginBounds.Height / printFont.GetHeight(g);//每页可打印行数 //逐行的循环打印一页 while(count<linesPerPage&&((line=lineReader.ReadLine())!=null)) { yPosition=topMargin+(count*printFont.GetHeight(g)); g.DrawString(line,printFont,myBrush,leftMargin,yPosition,new StringFormat()); count++; } if(line!=null) e.HasMorePages=true; else e.HasMorePages=false; } } }
相关文章推荐
- C# 使用EPL语言通过并口打印条码
- 使用 C# 进行 label 打印 的两种方式 .
- C# 使用printDocument1.Print打印时不显示 正在打印对话框(里面还有一个讨厌的取消按钮)
- C#中使用GDI+实现复杂打印
- C#使用指定打印机打印Word,Excel等Office文件和打印PDF文件的代码
- C#使用Zebra 斑马打印标签--使用winspool.Drv方式
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- c++.dll打印日志到c#界面,使用回调函数
- C#中使用PrintDialog方便的实现程序的打印功能
- 在 C# 中实现打印功能(C# 中 PrintDialog,PrintDocument 的使用)
- C#使用window API 控制打印纸张大小
- C#中使用GDI+实现复杂打印
- C# 打印PDF文件之使用不同打印机打印所有页面或部分页面
- C#使用window API 控制打印纸张大小(转载)
- 使用C#编写一个用于客户端打印的ActiveX控件(1)
- HOW TO:使用 Visual C# .NET 打印 RichTextBox 控件的内容
- c#.net 文本编辑控件——使用 Visual C# .NET 打印 RichTextBox 控件的内容
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- C#使用结构体,输入5个人的学号,姓名,分数,按照成绩高低排列打印出来
- 在VS2008中使用C#代码打印 RichTextBox 控件的内容[方法采自微软支持][Part 1]