VB中的Printer.Print在.Net中如何实现:Visual Basic 6.0 中的 Printer 在 Visual Basic .NET 中是由 PrintDocument 组件取代。。。那么如何使用呢?个人感觉,用.Net实现起来比在VB中麻烦多了,尤其是在.Net中需要计算什么时候该分页,使得在打印内容动态产生时,还得预先计算总行数。此文仅供需要使用该功能的人参考。
.Net中的打印A4纸如何实现(以C#为例)
——VB中的Printer.Print在.Net中如何实现
在VB中,如果要打印打印A4文档,且内容是从DB中,或者DataGrid中等动态获取的,实现起来非常简单,诸如以下代码(rs表示一记录集):
rs.MoveFirst
Printer.PaperSize = vbPRPSA4
Printer.Orientation = vbPRORPortrait
Printer.FontName = "Courier New"
Printer.FontBold = True
Printer.FontSize = 24
Printer.Print " FQA Report"
Printer.FontSize = 16
Printer.Print " Pallet ID:" & Trim(rs("pallet_id"))
NewLine2 = String(100, " ")
Mid(NewLine2, 1, 5) = "NO#"
Mid(NewLine2, 10, 30) = "System S/N"
Mid(NewLine2, 35, 20) = "BOX_ID"
Mid(NewLine2, 60, 20) = "Pallet_ID"
While Not rs.EOF
NewLine2 = String(100, " ")
Mid(NewLine2, 1, 5) = Trim(rs("No"))
Mid(NewLine2, 10, 30) = Trim(rs("SN"))
Mid(NewLine2, 35, 20) = Trim(rs("BOX_ID"))
Mid(NewLine2, 60, 20) = "" & Trim(rs("Pallet_ID"))
Printer.Print NewLine2
rs.MoveNext
Wend
Printer.Print NewLine2
Printer.Print String(100, "-")
在上述代码中,如果记录集中的数量很多,即内容超出了一页纸,程序不用做任何设置,便会自动分页,那么到.Net中,如何实现这么一个简单的功能呢?
查了好多资料,上网搜了好久,发现没有类似的例子,看了MSDN后,才发现,到VB.Net中,VB中那么好用的Printer不见了,一下为MSDN的描述:http://msdn.microsoft.com/zh-tw/library/cc438273(VS.71).aspx
"Visual Basic 6.0 中的 Printer 物件在 Visual Basic .NET 中是由 PrintDocument 组件取代。两者的行为不同,但在多数情况下可复制功能。下表将列出 Visual Basic 6.0 属性 (Property)、方法及事件与其 Visual Basic .NET 对等用法。如果没有直接的对等用法,则会提供连结以取得其它信息。除非另外注明,否则所有的对象都是在 System.Drawing 命名空间中。"
第一感觉是,在.Net中有比这更好用的打印功能,而且功能更强大,于是开始翻书,上网找例子,花了好长时间后,终于在网上找到了两个例子,跟教科书(C#高级编程第四版第25章,VB 2005入门经典第7章)里的例子差不多,例子中都是要先打开文档,LoadFile后,然后计算中共有多少行,然后设置一些属性等等,虽然功能强大,但是极其复杂,不能直接拿来用,于是只好自己改写。。。
打印Function:
public bool PrintDoc()
{
try
{
//获取需要打印的内容
string strSQL = "exec [usp_PalletPrintDocInfo] '"
+ Parameters.strStation + "', '" + Parameters.strPalletID + "'";
dsPrintInfo = doDB.GetDataSet(strSQL);
if (dsPrintInfo.Tables[0].Rows.Count < 1 || dsPrintInfo.Tables[1].Rows.Count < 1)
{
Parameters.strMsg = "Get Print Information Error";
return false;
}
//打印
PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
printDocument.Print();
/**/////打印预览,调试的时候,可以通过这个,节约纸张
//PrintPreviewDialog ppd = new PrintPreviewDialog();
//PrintDocument pd = new PrintDocument();
//pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
//ppd.Document = pd;
//ppd.ShowDialog();
return true;
}
catch(Exception ex)
{
Parameters.strMsg = ex.Message.ToString();
return false;
}
}
上面代码中,核心的是下面这个事件,调用pd_PrintPage方法:
//打印
PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
printDocument.Print();
pd_PrintPage方法:
//打印A4纸用的变量#region //打印A4纸用的变量
DataSet dsPrintInfo; //存放要打印的信息,资料从DB中获取,表一位SN信息,表二为WO信息
private int lineNo; //存放当前要打印行的行号
private int lineQty; //存放总共要打印的行数,可以是一个估算值,略大于实际行数
private int printingPageNo = 0; //当前打印的页号
#endregion
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
string strLine;//用于存放当前行打印的信息
float leftMargin = (e.MarginBounds.Left) * 3 / 4; //左边距
float topMargin = e.MarginBounds.Top * 2 / 3; //顶边距
float verticalPosition = topMargin; //初始化垂直位置,设为顶边距
Font mainFont = new Font("Courier New", 10);//打印的字体
//每页的行数,当打印行数超过这个时,要换页(1.05这个值是根据实际情况中设定的,可以不要)
int linesPerPage = (int)(e.MarginBounds.Height * 1.05 / mainFont.GetHeight(e.Graphics));
//Format of this DocReport#region //Format of this DocReport
if (printingPageNo == 0) //打印第一页时,需要打印以下头信息
{
mainFont = new Font("Courier New", 24, FontStyle.Bold);
strLine = String.Format("{0,21}", "FQA Report");
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
mainFont = new Font("Courier New", 16, FontStyle.Bold);
strLine = "Pallet ID:" + dsPrintInfo.Tables[0].Rows[0]["Pallet_ID"].ToString();
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
mainFont = new Font("3 of 9 Barcode", 24, FontStyle.Bold);
strLine = "*" + dsPrintInfo.Tables[0].Rows[0]["Pallet_ID"].ToString() + "*";
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin * 4, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
mainFont = new Font("Courier New", 16, FontStyle.Bold);
strLine = "QCI_PN:" + dsPrintInfo.Tables[0].Rows[0]["PARTNUMBER"].ToString();
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
strLine = "Date :" + DateTime.Now;
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
strLine = "Line :" + dsPrintInfo.Tables[0].Rows[0]["Mfg_Line"].ToString();
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
strLine = "Model:" + dsPrintInfo.Tables[0].Rows[0]["Model"].ToString();
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics) * 2;
//linesPerPage:上面的头信息预计占的行数,因此第一页还能打印(linesPerPage - 15)行
linesPerPage = linesPerPage - 15;
//lineQty:lineQty行数大概是表行数加上15
lineQty = dsPrintInfo.Tables[0].Rows.Count + 15;
}
#endregion
//打印具体的SN,BoxID,PalletID列表#region //打印具体的SN,BoxID,PalletID列表
//打印标题头信息
mainFont = new Font("Courier New", 10);
strLine = String.Format("{0,-10}", "NO#") +
String.Format("{0,-25}", "System S/N") +
String.Format("{0,-25}", "BOX_ID") +
String.Format("{0,-25}", "Pallet_ID");
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
//打印一条横线
mainFont = new Font("Courier New", 3);
e.Graphics.DrawLine(new Pen(Color.Black), leftMargin, verticalPosition, e.MarginBounds.Right, verticalPosition);
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
//打印记录集信息
int count = 0; //当前页的行数计数
mainFont = new Font("Courier New", 10);
//如果(当前页行计数器)小于(每页可以打印的行数)且(要打印行的行号)小于(总共要打印的行数)
while (count < linesPerPage && lineNo < this.lineQty)
{
if (lineNo < dsPrintInfo.Tables[0].Rows.Count) //由于lineNo用于表中的下标值,因此需要加上该判断
{
string strLineTemp = String.Format("{0,-10}", dsPrintInfo.Tables[0].Rows[lineNo]["NO"].ToString()) +
String.Format("{0,-25}", dsPrintInfo.Tables[0].Rows[lineNo]["SN"].ToString()) +
String.Format("{0,-25}", dsPrintInfo.Tables[0].Rows[lineNo]["BOX_ID"].ToString()) +
String.Format("{0,-25}", dsPrintInfo.Tables[0].Rows[lineNo]["Pallet_ID"].ToString());
e.Graphics.DrawString(strLineTemp, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
count++; //注意:这句要放到If中,否则,如果实际只有一页的话,尾信息会跑到第二页去
}
lineNo++;
}
#endregion
//如果总行数大于目前实际行号,表明还有页要打,lineQty是估计数,因此实际用时要仔细估算
if (lineQty > lineNo)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
//打印结尾信息#region //打印结尾信息
if (!e.HasMorePages)
{
mainFont = new Font("Courier New", 10);
e.Graphics.DrawLine(new Pen(Color.Black), leftMargin, verticalPosition, e.MarginBounds.Right, verticalPosition);
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
foreach (DataRow dr in dsPrintInfo.Tables[1].Rows)
{
mainFont = new Font("Courier New", 16);
strLine = String.Format("{0,-3}", "WO:") + String.Format("{0,-16}",dr["WO"].ToString())
+ String.Format("{0,-7}", "Qty:") + String.Format("{0,-10}",dr["Qty"].ToString());
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
}
foreach (DataRow dr in dsPrintInfo.Tables[2].Rows)
{
mainFont = new Font("Courier New", 16);
strLine = String.Format("{0,-9}", "TotalQty:") + String.Format("{0,-10}", dr["TotalQty"].ToString())
+ String.Format("{0,-7}", "BoxQty:") + String.Format("{0,-10}",dr["BoxQty"].ToString());
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
}
}
#endregion
printingPageNo++; //页号加一
}
打印效果大致截图:(如果有多页,尾部的信息只在最后一页显示)
总结:如此简单的功能,在.Net中实现起来确实那么的复杂,尤其是对于自动分页的功能,虽然总体上功能强大了好多,但是把原先那么好用的东西给去掉了。以上代码改写成VB.Net的话,都差不多。。。