using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Drawing.Printing;
using
System.Windows.Forms;
using
xYuanShian.TOOLS;
using
System.Drawing;
namespace
xYuanShian.SaleSystem
{
public
class
PrintHelper
{
///
<summary>
///
文档打印对象
///
</summary>
private
PrintDocument lp;
///
<summary>
///
打印队列
///
</summary>
private
List
<
string
>
TextQueue;
///
<summary>
///
打印的纵坐标
///
</summary>
private
int
YPos
=
0
;
///
<summary>
///
被打印的文本字体属性
///
</summary>
private
Font Font;
///
<summary>
///
打印预览
///
</summary>
private
PrintPreviewDialog pView;
///
<summary>
///
页面设置
///
</summary>
private
PageSetupDialog pPageSetup;
///
<summary>
///
选择打印机
///
</summary>
private
PrintDialog pSPrint;
///
<summary>
///
打印标题
///
</summary>
public
string
Title;
///
<summary>
///
初始化打印机
///
</summary>
private
void
InitPrint()
{
this
.lp.DefaultPageSettings.Color
=
false
;
this
.lp.DefaultPageSettings.Landscape
=
false
;
this
.lp.DefaultPageSettings.Margins
=
new
Margins(
0
,
0
,
0
,
0
);
this
.lp.DefaultPageSettings.PaperSize
=
new
PaperSize(
"
自定义
"
,
200
,
0
);
}
///
<summary>
///
构造函数
///
</summary>
///
<param name="lp">
打印机
</param>
///
<param name="font">
打印的字体
</param>
public
PrintHelper( PrintDocument lp, Font font )
{
this
.lp
=
lp;
this
.lp.PrintPage
+=
PrintEvent_Page;
this
.InitPrint();
this
.Font
=
font;
this
.pView
=
new
PrintPreviewDialog();
this
.pPageSetup
=
new
PageSetupDialog();
this
.pSPrint
=
new
PrintDialog();
}
///
<summary>
///
设置纸张和边距
///
</summary>
///
<param name="migs">
边距
</param>
///
<param name="paperSize">
纸张规格
</param>
public
void
SettingPrint( Margins migs, PaperSize paperSize )
{
if
( migs
!=
null
)
this
.lp.DefaultPageSettings.Margins
=
migs;
if
( paperSize
!=
null
)
this
.lp.DefaultPageSettings.PaperSize
=
paperSize;
}
///
<summary>
///
打印文件
///
</summary>
public
void
PrintString(
string
strText )
{
this
.TextQueue
=
new
List
<
string
>
();
if
(
!
String.IsNullOrEmpty( strText ) )
{
this
.TextQueue.AddRange( strText.Split(
new
string
[] { Environment.NewLine }, StringSplitOptions.None ) );
try
{
this
.lp.Print();
}
catch
( Exception ex )
{
//
打印出错处理
MsgAlter.Alter(
"
打印小票发生错误:
"
+
ex.Message );
}
}
GC.Collect();
}
///
<summary>
///
设置预览
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
public
void
SetPreviewDialog()
{
//
设置Document属性
this
.pView.Document
=
this
.lp;
try
{
//
显示打印预览窗口
this
.pView.ShowDialog();
}
catch
( Exception ex )
{
//
显示打印出错消息
MsgAlter.Alter(
"
打印出错:
"
+
ex.Message );
}
}
///
<summary>
///
设置页面
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
public
void
PageSetup()
{
this
.pPageSetup.Document
=
this
.lp;
try
{
//
显示打印页面设置窗口
this
.pPageSetup.ShowDialog();
}
catch
( Exception ex )
{
//
显示打印出错消息
MsgAlter.Alter(
"
打印出错:
"
+
ex.Message );
}
}
///
<summary>
///
设置打印机
///
</summary>
public
void
PrintDialog(
string
strText )
{
this
.pSPrint.Document
=
this
.lp;
//
显示打印窗口
if
(
this
.pSPrint.ShowDialog()
==
DialogResult.OK )
{
try
{
this
.PrintString( strText );
}
catch
( Exception ex )
{
//
显示打印出错消息
MsgAlter.Alter(
"
打印出错:
"
+
ex.Message );
}
}
}
///
<summary>
///
页面打印事件
///
</summary>
///
<param name="Sender"></param>
///
<param name="e"></param>
private
void
PrintEvent_Page(
object
Sender, PrintPageEventArgs e )
{
///
获得绘制对象
Graphics g
=
e.Graphics;
///
顶边界
float
topMargin
=
e.MarginBounds.Top;
///
获取打印纵坐标
YPos
=
(
int
)topMargin;
SizeF s;
Rectangle rectText;
if
(
!
String.IsNullOrEmpty( Title ) )
{
s
=
g.MeasureString( Title,
new
Font(
"
宋体
"
,
14
), e.MarginBounds.Width );
rectText
=
new
Rectangle(
0
, YPos, e.MarginBounds.Width, (
int
)s.Height
+
1
);
g.DrawString( Title,
new
Font(
"
宋体
"
,
14
), Brushes.Black, rectText );
YPos
+=
(
int
)s.Height
+
1
;
}
string
PrintText;
///
每次从文件中读取一行并打印
foreach
(
string
strTmp
in
this
.TextQueue )
{
PrintText
=
strTmp;
if
( String.IsNullOrEmpty( strTmp ) ) PrintText
=
"
"
;
///
计算出打印指定文本的所需要的矩形区域大小
s
=
g.MeasureString( PrintText,
this
.Font, e.MarginBounds.Width );
///
画出矩形区域
rectText
=
new
Rectangle(
0
, YPos, e.MarginBounds.Width, (
int
)s.Height
+
1
);
///
开始绘制
g.DrawString( PrintText,
this
.Font, Brushes.Black, rectText );
YPos
+=
(
int
)s.Height
+
1
;
}
TextQueue.Clear();
e.HasMorePages
=
false
;
}
///
<summary>
///
可以计算分页的打印事件
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
private
void
printDocument11_PrintPage(
object
sender, PrintPageEventArgs e )
{
//
打印信息处理
///
/获得绘制对象
//
Graphics g = e.Graphics;
///
/一页中的行数
//
float LinePages = 0;
///
/待绘文本的纵向坐标
//
float YPos = 0;
///
/行计数7
//
int count = 0;
///
/左边界
//
float LeftMargin = e.MarginBounds.Left;
///
/顶边界
//
float topMargin = e.MarginBounds.Top;
///
/字符串流
//
String line = null;
///
/根据页面的高度和字体的高度计算
///
/一页中可以打印的行数
//
LinePages = e.MarginBounds.Height / this.Font.GetHeight( g );
///
/if ( i >= this.richTextBox1.Lines.Length - 1 )
///
/{
///
/ e.HasMorePages = false;
///
/ return;
///
/}
///
/每次从文件中读取一行并打印
//
while ( count < LinePages && ( ( line = this.richTextBox1.Lines[i++] ) != null ) )
//
{
//
//
计算这一行的显示位置
//
YPos = topMargin + ( count * this.Font.GetHeight( g ) );
//
//
绘制文本
//
SizeF s = g.MeasureString( "你说什么时候啊你说什么时候", new Font( "宋体", 9 ) );
//
g.DrawString( line, this.Font, Brushes.Black, LeftMargin, YPos, new StringFormat() );
//
//
行数增加
//
count++;
//
if ( i > this.richTextBox1.Lines.Length - 1 )
//
break;
//
}
///
/如果有多行,则另外打印一页
//
if ( line != null )
//
e.HasMorePages = true;
//
else
//
e.HasMorePages = false;
}
///
<summary>
///
微软网站上的打印事件代码
///
</summary>
///
<param name="sender"></param>
///
<param name="ev"></param>
private
void
pd_PrintPage(
object
sender, PrintPageEventArgs ev )
{
//
float linesPerPage = 0;
//
float yPos = 0;
//
int count = 0;
//
float leftMargin = ev.MarginBounds.Left;
//
float topMargin = ev.MarginBounds.Top;
//
string line = null;
///
/ Calculate the number of lines per page.
//
linesPerPage = ev.MarginBounds.Height /
//
printFont.GetHeight( ev.Graphics );
///
/ Print each line of the file.
//
while ( count < linesPerPage &&
//
( ( line = streamToPrint.ReadLine() ) != null ) )
//
{
//
yPos = topMargin + ( count *
//
printFont.GetHeight( ev.Graphics ) );
//
ev.Graphics.DrawString( line, printFont, Brushes.Black,
//
leftMargin, yPos, new StringFormat() );
//
count++;
//
}
///
/ If more lines exist, print another page.
//
if ( line != null )
//
ev.HasMorePages = true;
//
else
//
ev.HasMorePages = false;
}
}
}
用例:
PrintHelper ph
=
new
PrintHelper(
this
.lp,
this
.Font );
ph.Title
=
"
标题
"
;
ph.PrintString(
"
文本内容
"
);