http://www.byywee.com/page/M0/S543/543762.html
C#打印(PrintDocument、PrintDialog、PageSetupDialog、PrintPreviewD...
Admin
2011年5月31日
名人名言:理想并不是一种空虚的东西,也并不玄奇,它既非幻想,更非野心,而是一种追求真美的意识。——莎菲德拉
这几天一直在弄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
{
public partial class PrintOther : Form
{
public PrintOther()
{
InitializeComponent();
}
//PrintDocument类是实现打印功能的核心,它封装了打印有关的属性、事件、和方法
PrintDocument printDocument = new PrintDocument();
private void btnPrint_Click(object sender, EventArgs e)
{
//printDocument.PrinterSettings可以获取或设置计算机默认打印相关属性或参数,如:printDocument.PrinterSettings.PrinterName获得默认打印机打印机名称
//printDocument.DefaultPageSettings //可以获取或设置打印页面参数信息、如是纸张大小,是否横向打印等
//设置文档名
printDocument.DocumentName = "处方笺";//设置完后可在打印对话框及队列中显示(默认显示document)
//设置纸张大小(可以不设置取,取默认设置)
PaperSize ps = new PaperSize("Your Paper Name", 100, 70);
ps.RawKind = 150; //如果是自定义纸张,就要大于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("宋体", 16);
string title = "五凤街道梅峰社区卫生服务站 处方笺";
g.DrawString(title, titleFont, b, new PointF((e.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 2, 20));
//e.Cancel//获取或设置是否取消打印
//e.HasMorePages //为true时,该函数执行完毕后还会重新执行一遍(可用于动态分页)
}
void printDocument_EndPrint(object sender, PrintEventArgs e)
{
//打印结束后相关操作
}
}
}
http://hi.baidu.com/mmaey/item/b2a91a6b4cf4843cac3e83d0
C#链接打印机(多个打印机选择、图片加载)
部分代码:
后台代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Drawing.Printing;
using System.Drawing.Imaging;
using System.Net;
using System.IO;
using System.Text;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = "XP-58II";//指定打印机(如果只有一台可不写 如果你的电脑连有多个打印机则需要指定你希望输出的打印机名称 否则按默认打印机)
PaperSize p=null;
foreach (PaperSize ps in pd.PrinterSettings.PaperSizes)
{
if (ps.PaperName.Equals("Printer 58(48) x 210 mm"))
{
p = ps;
break;
}
}
pd.DefaultPageSettings.Landscape = true; // 横向
pd.DefaultPageSettings.Landscape = false; // 综向
pd.DefaultPageSettings.PaperSize = p;
pd.PrintPage +=new System.Drawing.Printing.PrintPageEventHandler(pdoc_PrintPage);
pd.Print();
}
public void pdoc_PrintPage( object sender,System.Drawing.Printing.PrintPageEventArgs e)
{
//读取图片模板
string path = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + @"\resources\proof_ic.png");
System.Drawing.Image temp = System.Drawing.Image.FromFile(path);
Brush brush = new SolidBrush(Color.Black);
Pen pen = new Pen(brush);
Font f = new Font("宋体", 9);
GetResultIntoImage(ref temp);
int x = e.MarginBounds.X;
int y = e.MarginBounds.Y;
int width = temp.Width;
int height = temp.Height;
Rectangle destRect = new Rectangle(0, 0, width, height);
e.Graphics.DrawImage(temp, destRect, 0, 0, temp.Width, temp.Height, System.Drawing.GraphicsUnit.Pixel);
e.Graphics.DrawString("客服电话 96718" , new Font("宋体", 9, FontStyle.Regular), new SolidBrush(Color.Black), new PointF(88, 24));
e.Graphics.DrawString("签购单", new Font("宋体", 13, FontStyle.Bold), new SolidBrush(Color.Black), new PointF(67, 60));
e.Graphics.DrawLine(pen, 5, 90, 200, 90);
e.Graphics.DrawLine(pen, 5, 93, 200, 93);
e.Graphics.DrawString("i超市名称: ", f, new SolidBrush(Color.Black), new PointF(10, 110));
e.Graphics.DrawString("账户: ", f, new SolidBrush(Color.Black), new PointF(10, 135));
e.Graphics.DrawString("i超市编号: "", f, new SolidBrush(Color.Black), new PointF(10, 160));
e.Graphics.DrawString("交易单号: ", f, new SolidBrush(Color.Black), new PointF(10, 185));
e.Graphics.DrawString("交易类型: ", f, new SolidBrush(Color.Black), new PointF(10, 210));
e.Graphics.DrawString("商品内容: ", f, new SolidBrush(Color.Black), new PointF(10, 235));
e.Graphics.DrawString("交易金额:" + "30.00元", f, new SolidBrush(Color.Black), new PointF(10, 270));
e.Graphics.DrawString("交易时间:" +"", new Font("宋体", 8), new SolidBrush(Color.Black), new PointF(10, 295));
e.Graphics.DrawLine(pen, 5, 320, 200, 320);
e.Graphics.DrawLine(pen, 5, 323, 200, 323);
e.Graphics.DrawString("本单据为本次交易的有", new Font("宋体", 12, FontStyle.Regular), new SolidBrush(Color.Black), new PointF(15, 330));
e.Graphics.DrawString("效凭证,请妥善保存", new Font("宋体", 12, FontStyle.Regular), new SolidBrush(Color.Black), new PointF(20, 360));
}
private void GetResultIntoImage(ref System.Drawing.Image temp )
{
//读取图片模板
Graphics g = Graphics.FromImage(temp);
Font f = new Font("宋体", 12);
Brush b = new SolidBrush(Color.Black);
g.DrawImage(temp, 0, 0, temp.Width, temp.Height);
g.Dispose();
}
}
http://bbs.csdn.net/topics/190150551
C#修改默认打印机
using System.Runtime.InteropServices;
[DllImport("winspool.drv")]
public static extern bool SetDefaultPrinter(string Name);
private void button1_Click(object sender, EventArgs e)
{
SetDefaultPrinter("My Printer");
}
最后 ,打印还可以用控件webbrowser实现
C#打印(PrintDocument、PrintDialog、PageSetupDialog、PrintPreviewD...
Admin
2011年5月31日
名人名言:理想并不是一种空虚的东西,也并不玄奇,它既非幻想,更非野心,而是一种追求真美的意识。——莎菲德拉
这几天一直在弄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
{
public partial class PrintOther : Form
{
public PrintOther()
{
InitializeComponent();
}
//PrintDocument类是实现打印功能的核心,它封装了打印有关的属性、事件、和方法
PrintDocument printDocument = new PrintDocument();
private void btnPrint_Click(object sender, EventArgs e)
{
//printDocument.PrinterSettings可以获取或设置计算机默认打印相关属性或参数,如:printDocument.PrinterSettings.PrinterName获得默认打印机打印机名称
//printDocument.DefaultPageSettings //可以获取或设置打印页面参数信息、如是纸张大小,是否横向打印等
//设置文档名
printDocument.DocumentName = "处方笺";//设置完后可在打印对话框及队列中显示(默认显示document)
//设置纸张大小(可以不设置取,取默认设置)
PaperSize ps = new PaperSize("Your Paper Name", 100, 70);
ps.RawKind = 150; //如果是自定义纸张,就要大于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("宋体", 16);
string title = "五凤街道梅峰社区卫生服务站 处方笺";
g.DrawString(title, titleFont, b, new PointF((e.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 2, 20));
//e.Cancel//获取或设置是否取消打印
//e.HasMorePages //为true时,该函数执行完毕后还会重新执行一遍(可用于动态分页)
}
void printDocument_EndPrint(object sender, PrintEventArgs e)
{
//打印结束后相关操作
}
}
}
http://hi.baidu.com/mmaey/item/b2a91a6b4cf4843cac3e83d0
C#链接打印机(多个打印机选择、图片加载)
部分代码:
后台代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Drawing.Printing;
using System.Drawing.Imaging;
using System.Net;
using System.IO;
using System.Text;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = "XP-58II";//指定打印机(如果只有一台可不写 如果你的电脑连有多个打印机则需要指定你希望输出的打印机名称 否则按默认打印机)
PaperSize p=null;
foreach (PaperSize ps in pd.PrinterSettings.PaperSizes)
{
if (ps.PaperName.Equals("Printer 58(48) x 210 mm"))
{
p = ps;
break;
}
}
pd.DefaultPageSettings.Landscape = true; // 横向
pd.DefaultPageSettings.Landscape = false; // 综向
pd.DefaultPageSettings.PaperSize = p;
pd.PrintPage +=new System.Drawing.Printing.PrintPageEventHandler(pdoc_PrintPage);
pd.Print();
}
public void pdoc_PrintPage( object sender,System.Drawing.Printing.PrintPageEventArgs e)
{
//读取图片模板
string path = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + @"\resources\proof_ic.png");
System.Drawing.Image temp = System.Drawing.Image.FromFile(path);
Brush brush = new SolidBrush(Color.Black);
Pen pen = new Pen(brush);
Font f = new Font("宋体", 9);
GetResultIntoImage(ref temp);
int x = e.MarginBounds.X;
int y = e.MarginBounds.Y;
int width = temp.Width;
int height = temp.Height;
Rectangle destRect = new Rectangle(0, 0, width, height);
e.Graphics.DrawImage(temp, destRect, 0, 0, temp.Width, temp.Height, System.Drawing.GraphicsUnit.Pixel);
e.Graphics.DrawString("客服电话 96718" , new Font("宋体", 9, FontStyle.Regular), new SolidBrush(Color.Black), new PointF(88, 24));
e.Graphics.DrawString("签购单", new Font("宋体", 13, FontStyle.Bold), new SolidBrush(Color.Black), new PointF(67, 60));
e.Graphics.DrawLine(pen, 5, 90, 200, 90);
e.Graphics.DrawLine(pen, 5, 93, 200, 93);
e.Graphics.DrawString("i超市名称: ", f, new SolidBrush(Color.Black), new PointF(10, 110));
e.Graphics.DrawString("账户: ", f, new SolidBrush(Color.Black), new PointF(10, 135));
e.Graphics.DrawString("i超市编号: "", f, new SolidBrush(Color.Black), new PointF(10, 160));
e.Graphics.DrawString("交易单号: ", f, new SolidBrush(Color.Black), new PointF(10, 185));
e.Graphics.DrawString("交易类型: ", f, new SolidBrush(Color.Black), new PointF(10, 210));
e.Graphics.DrawString("商品内容: ", f, new SolidBrush(Color.Black), new PointF(10, 235));
e.Graphics.DrawString("交易金额:" + "30.00元", f, new SolidBrush(Color.Black), new PointF(10, 270));
e.Graphics.DrawString("交易时间:" +"", new Font("宋体", 8), new SolidBrush(Color.Black), new PointF(10, 295));
e.Graphics.DrawLine(pen, 5, 320, 200, 320);
e.Graphics.DrawLine(pen, 5, 323, 200, 323);
e.Graphics.DrawString("本单据为本次交易的有", new Font("宋体", 12, FontStyle.Regular), new SolidBrush(Color.Black), new PointF(15, 330));
e.Graphics.DrawString("效凭证,请妥善保存", new Font("宋体", 12, FontStyle.Regular), new SolidBrush(Color.Black), new PointF(20, 360));
}
private void GetResultIntoImage(ref System.Drawing.Image temp )
{
//读取图片模板
Graphics g = Graphics.FromImage(temp);
Font f = new Font("宋体", 12);
Brush b = new SolidBrush(Color.Black);
g.DrawImage(temp, 0, 0, temp.Width, temp.Height);
g.Dispose();
}
}
http://bbs.csdn.net/topics/190150551
C#修改默认打印机
using System.Runtime.InteropServices;
[DllImport("winspool.drv")]
public static extern bool SetDefaultPrinter(string Name);
private void button1_Click(object sender, EventArgs e)
{
SetDefaultPrinter("My Printer");
}
最后 ,打印还可以用控件webbrowser实现