原文地址:http://www.cnblogs.com/smallsoftfox/archive/2012/06/25/2562718.html

参考文章:http://www.cnblogs.com/huangfr/archive/2011/09/25/2190579.html

参考文章:http://www.cnblogs.com/weixing/archive/2013/08/26/3283182.html

这几天一直在弄C#打印,下面整理过后的打印范例,主要介绍了PrintDocument的主要属性、方法的应用。及打印过程中可能用的到得PrintDialog、PageSetupDialog、PrintPreviewDialog对话框。
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing; namespace demo
{
publicpartialclass PrintOther : Form
{ public PrintOther()
{
InitializeComponent();
} //PrintDocument类是实现打印功能的核心,它封装了打印有关的属性、事件、和方法
PrintDocument printDocument =new PrintDocument(); privatevoid btnPrint_Click(object sender, EventArgs e)
{
//printDocument.PrinterSettings可以获取或设置计算机默认打印相关属性或参数,如:printDocument.PrinterSettings.PrinterName获得默认打印机打印机名称
//printDocument.DefaultPageSettings //可以获取或设置打印页面参数信息、如是纸张大小,是否横向打印等 //设置文档名
printDocument.DocumentName ="处方笺";//设置完后可在打印对话框及队列中显示(默认显示document) //设置纸张大小(可以不设置取,取默认设置)
PaperSize ps =new PaperSize("Your Paper Name", , );
ps.RawKind =; //如果是自定义纸张,就要大于118,(A4值为9,详细纸张类型与值的对照请看http://msdn.microsoft.com/zh-tw/library/system.drawing.printing.papersize.rawkind(v=vs.85).aspx)
printDocument.DefaultPageSettings.PaperSize = ps; //打印开始前
printDocument.BeginPrint +=new PrintEventHandler(printDocument_BeginPrint);
//打印输出(过程)
printDocument.PrintPage +=new PrintPageEventHandler(printDocument_PrintPage);
//打印结束
printDocument.EndPrint +=new PrintEventHandler(printDocument_EndPrint); //跳出打印对话框,提供打印参数可视化设置,如选择哪个打印机打印此文档等
PrintDialog pd =new PrintDialog();
pd.Document = printDocument;
if (DialogResult.OK == pd.ShowDialog()) //如果确认,将会覆盖所有的打印参数设置
{
//页面设置对话框(可以不使用,其实PrintDialog对话框已提供页面设置)
PageSetupDialog psd =new PageSetupDialog();
psd.Document = printDocument;
if (DialogResult.OK == psd.ShowDialog())
{
//打印预览
PrintPreviewDialog ppd =new PrintPreviewDialog();
ppd.Document = printDocument;
if (DialogResult.OK == ppd.ShowDialog())
printDocument.Print(); //打印
}
}
} void printDocument_BeginPrint(object sender, PrintEventArgs e)
{
//也可以把一些打印的参数放在此处设置
} void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
//打印啥东东就在这写了
Graphics g = e.Graphics;
Brush b =new SolidBrush(Color.Black);
Font titleFont =new Font("宋体", );
string title ="社区卫生服务站 处方笺";
g.DrawString(title, titleFont, b, new PointF((e.PageBounds.Width - g.MeasureString(title, titleFont).Width) /, )); //e.Cancel//获取或设置是否取消打印
//e.HasMorePages //为true时,该函数执行完毕后还会重新执行一遍(可用于动态分页)
} void printDocument_EndPrint(object sender, PrintEventArgs e)
{
//打印结束后相关操作
}
}
}

最新文章

  1. 【转】超实用PHP函数总结整理
  2. boolean 和 Boolean 类型数据的差别
  3. compact过滤数组中的nil
  4. 【转】javascript性能优化-repaint和reflow
  5. C#的SerialPort串口程序设计总结
  6. Linux系统监控
  7. php 添加 redis 扩展模块
  8. C#中的Virtual
  9. java.lang.IllegalArgumentException: Can't convert argument: null
  10. js对敏感词的判断
  11. Hive-ORC文件存储格式(续)
  12. Metasploit Framework(4)信息收集
  13. 洛谷.4245.[模板]任意模数NTT(MTT/三模数NTT)
  14. Linux——awk命令解析
  15. [BZOJ3339]Rmq Problem / mex
  16. (18)ProcessPoolExecutor进程池
  17. mysql真的不能做搜索引擎吗?
  18. [django]django corepython核心编程
  19. CF444(Div. 1简单题解)
  20. mysql深坑之--group_concat有长度限制!!!!默认1024

热门文章

  1. SAS笔记
  2. SUPERSOCKET 客户端
  3. react-redux 知识点
  4. Package has no installation candidate解决方法
  5. c++获取键盘输入cin、scanf使用详解
  6. delphi中Time消息的使用方法
  7. python 类继承与子类实例初始化
  8. Ubuntu(虚拟机)下安装Qt5.5.1
  9. PREV-5_蓝桥杯_错误票据
  10. C++11--正则表达式<regex>