.转载自该前辈的博客:Net中的打印A4纸如何实现(以C#为例)——VB中的Printer.Print在.Net中如何实现 - MaoBisheng - 博客园 (cnblogs.com)
注:本文代码为参考博客中前辈的代码段改编形成。
在VB中,如果要打印打印A4文档,且内容是从DB中,或者DataGrid中等动态获取的,实现起来非常简单,诸如以下代码(rs表示一记录集):
Public Function PrintReport(Pallet_ID As String) ''''+++++++++++++打印Report
Dim totalqty As String
Dim sStartRS As String, sEndRS As String
Dim NewLine2 As String, strsql As String
Dim rs As New ADODB.Recordset, rs1 As New ADODB.Recordset
strsql = "select No, PR.Box_ID, PR.SN, mfg_line , PR.Part_Num as work_order " & _
",boxqty from PackingRpt_ForSpareParts PR where pallet_id=" & squote(Pallet_ID) & " order by no"
rs.CursorLocation = adUseClient
If rs.State = 1 Then rs.Close
rs.Open strsql, conn, adOpenForwardOnly, adLockReadOnly
If Not rs.EOF Then
strsql = "select sum(cast(boxqty as int)) as qty from packingrpt_forspareparts where pallet_id='" & Pallet_ID & "'"
Set rs1 = conn.Execute(strsql)
If rs1.EOF = False Then totalqty = rs1("qty")
Printer.PaperSize = vbPRPSA4 'A4纸大小
Printer.Orientation = vbPRORPortrait '纵向打印
Printer.FontName = "Courier New"
Printer.FontBold = True
Printer.FontSize = 24
Printer.Print " FQA Report"
Printer.FontSize = 18
Printer.Print "Pallet ID:" & Pallet_ID
Printer.FontBold = False
Printer.FontSize = 24
Printer.FontName = "3 of 9 Barcode"
Printer.Print "*" & Pallet_ID & "*"
Printer.FontSize = 12
Printer.FontName = "Courier New"
Printer.FontSize = 16
Printer.Print " Date :" & Now
Printer.Print " Line :" & Trim(rs("Mfg_Line"))
Printer.FontSize = 10
Printer.FontBold = False
NewLine2 = String(100, " ")
Mid(NewLine2, 1, 3) = "NO#"
Mid(NewLine2, 6, 30) = "System S/N"
Mid(NewLine2, 36, 20) = "BOX_ID"
Mid(NewLine2, 58, 18) = "QTY/BOX"
Mid(NewLine2, 76, 100) = "Part Number"
Printer.Print NewLine2
Printer.Print String(100, "-")
While Not rs.EOF
If CInt(rs!No) = 1 Then sStartRS = Trim(rs!SN)
sEndRS = Trim(rs!SN)
NewLine2 = String(100, " ")
Mid(NewLine2, 1, 5) = Trim(rs("No"))
Mid(NewLine2, 6, 20) = Trim(rs("SN"))
Mid(NewLine2, 36, 20) = "" & Trim(rs("sn"))
Mid(NewLine2, 58, 18) = "" & Trim(rs("BOXQTY"))
Mid(NewLine2, 76, 100) = Trim(rs("Work_Order"))
Debug.Print NewLine2
Printer.Print NewLine2
rs.MoveNext
Wend
Printer.FontSize = 12
Printer.Print sStartRS
Printer.FontName = "3 of 9 Barcode"
Printer.FontSize = 12
Printer.Print "*" & sStartRS & "*"
Printer.FontName = "Courier New"
Printer.Print sEndRS
Printer.FontName = "3 of 9 Barcode"
Printer.FontSize = 12
Printer.Print "*" & sEndRS & "*"
Printer.FontName = "Courier New"
Printer.FontSize = 10
Printer.Print String(100, "-")
Printer.Print " Total Q'ty.= " & totalqty & " Box Q'ty.= " & rs.RecordCount
Printer.Print String(100, "=")
Printer.Print ""
Printer.Print ""
Printer.Print ""
Printer.FontBold = False
End If
Set rs = Nothing
Set rs1 = Nothing
Printer.EndDoc
Exit Function
errHandler:
Set rs = Nothing
Set rs1 = Nothing
End Function
在上述代码中,如果记录集中的数量很多,即内容超出了一页纸,程序不用做任何设置,便会自动分页,那么到.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后,然后计算中共有多少行,然后设置一些属性等等,虽然功能强大,但是极其复杂,不能直接拿来用....
public void PrintReport()
{
txtMessage.Text = "";//清空txtMessage信息
this.printPreviewDialog1.UseAntiAlias = true;
//设置要预览的文档
this.printPreviewDialog1.Document = this.printDocument1;//设置要预览的文档
printPreviewDialog1.ShowDialog();//打开预览窗口
//printDocument1.Print();//直接打印
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
try
{
string strPalletID = Convert.ToString(dgvPacking.Rows[0].Cells["Pallet_id"].Value.ToString());//要打印的pallet_ID信息
DataSet ds = DBAccessPub.FindPalletID("PrintItem", strPalletID);//存放要打印的信息,资料从DB中获取,表一位为要打印的item,表二为TotalQty信息
string beginBoxID = ds.Tables[0].Rows[0]["Box_ID"].ToString(); ;//用于记录该PalletID所对应的NO为1的BOX_ID
string endBoxID = ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1]["Box_ID"].ToString(); ;//用于记录该PalletID所对应最后一个no号的BOX_ID
string TotalQty = ds.Tables[1].Rows[0]["Qty"].ToString();//用于该PalletID所对应的BoxQty的总数
string BoxQty = dgvPacking.RowCount.ToString();//用于记录该pallet上存放的机器数,一个机器算作1
string strLine;//用于存放当前行打印的信息
//float leftMargin = (e.MarginBounds.Left) * 3 / 4; //左边距
float leftMargin = (e.MarginBounds.Left) / 4; //左边距
float topMargin = e.MarginBounds.Top * 2 / 3; //顶边距
float verticalPosition = topMargin; //初始化垂直位置,设为顶边距
//int lineNo = 0; //存放当前要打印行的行号
//int lineQty = 0; //存放总共要打印的行数,可以是一个估算值,略大于实际行数
//int printingPageNo = 0; //当前打印的页号
Font mainFont = new Font("Courier New", 10);//打印的字体
//每页的行数,当打印行数超过这个时,要换页(1.05这个值是根据实际情况中设定的,可以不要)
int linesPerPage = (int)(e.MarginBounds.Height * 1.05 / mainFont.GetHeight(e.Graphics));
#region //Format of this DocReport
if (printingPageNo == 0) //打印第一页时,需要打印以下头信息
{
mainFont = new Font("Courier New", 24, FontStyle.Bold);
strLine = String.Format("{0,24}", "FQA Report");
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
mainFont = new Font("Courier New", 18, FontStyle.Bold);
//strLine = "Pallet ID:" + ds.Tables[0].Rows[0]["Pallet_ID"].ToString();
strLine = "Pallet ID:" + strPalletID;
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);
//strLine = "*" + ds.Tables[0].Rows[0]["Pallet_ID"].ToString() + "*";
strLine = "*" + strPalletID + "*";
//e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin * 4, verticalPosition, new StringFormat());
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
mainFont = new Font("Courier New", 16);
strLine = "Date :" + DateTime.Now;
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
strLine = "Line :" + ds.Tables[0].Rows[0]["Mfg_Line"].ToString();
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
#region //打印具体的SN,BoxID,PalletID等列表
//打印标题头信息
mainFont = new Font("Courier New", 10);
//strLine = "NO#"+
strLine = String.Format("{0,-15}", "NO#") +
String.Format("{0,-21}", "System S/N") +//25
String.Format("{0,-21}", "BOX_ID") +
String.Format("{0,-15}", "QTY/BOX") +
String.Format("{0,-21}", "Part Number");
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
//打印一条横线
e.Graphics.DrawString("-".PadLeft(95, '-'), mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
//e.Graphics.DrawLine(new Pen(Color.Black), leftMargin, verticalPosition, e.MarginBounds.Right, verticalPosition);
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
//linesPerPage:经过实验上面的头信息预计占的行数(为11),因此第一页还能打印(linesPerPage - 11)行。当设置的值大于11时会出现第一张纸有空白。若小于11可能会造成结尾页信息不显示
linesPerPage = linesPerPage - 11;
//lineQty:lineQty行数大概是表行数加上11
lineQty = ds.Tables[0].Rows.Count + 11;
}
#endregion
//打印记录集信息
int count = 0; //当前页的行数计数
if (printingPageNo != 0)
{
linesPerPage = (int)(e.MarginBounds.Height / mainFont.GetHeight(e.Graphics));
}
mainFont = new Font("Courier New", 10);
//如果(当前页行计数器)小于(每页可以打印的行数)且(要打印行的行号)小于(总共要打印的行数)
while (count < linesPerPage && lineNo < lineQty)
{
if (lineNo < ds.Tables[0].Rows.Count) //由于lineNo用于表中的下标值,因此需要加上该判断
{
//endBoxID = ds.Tables[0].Rows[lineNo]["Box_ID"].ToString();
//string strLineTemp = ds.Tables[0].Rows[lineNo]["NO"].ToString()+
string strLineTemp = String.Format("{0,-15}", ds.Tables[0].Rows[lineNo]["NO"].ToString()) +
String.Format("{0,-21}", ds.Tables[0].Rows[lineNo]["SN"].ToString()) +
String.Format("{0,-21}", ds.Tables[0].Rows[lineNo]["BOX_ID"].ToString()) +
String.Format("{0,-15}", ds.Tables[0].Rows[lineNo]["BoxQty"].ToString()) +
//String.Format("{0,-25}", ds.Tables[0].Rows[lineNo]["Pallet_ID"].ToString());
String.Format("{0,-21}", ds.Tables[0].Rows[lineNo]["Work_Order"].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;
printingPageNo++; //页号加一
}
else
{
lineNo = 0; //存放当前要打印行的行号
lineQty = 0; //存放总共要打印的行数,可以是一个估算值,略大于实际行数
printingPageNo = 0; //当前打印的页号
e.HasMorePages = false;
}
#region //打印结尾信息
if (!e.HasMorePages)
{
mainFont = new Font("Courier New", 12);
strLine = String.Format("{0,-7}", beginBoxID);
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
mainFont = new Font("3 of 9 Barcode", 12);
strLine = String.Format("{0,-10}", "*" + beginBoxID + "*");
//e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin * 4, verticalPosition, new StringFormat());
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
mainFont = new Font("Courier New", 12);
strLine = String.Format("{0,-7}", endBoxID);
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
mainFont = new Font("3 of 9 Barcode", 12);
strLine = String.Format("{0,-10}", "*" + endBoxID + "*");
//e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin * 4, verticalPosition, new StringFormat());
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
mainFont = new Font("Courier New", 10);
//e.Graphics.DrawLine(new Pen(Color.Black), leftMargin, verticalPosition, e.MarginBounds.Right, verticalPosition);//画一条线
e.Graphics.DrawString("-".PadLeft(95, '-'), mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());//画一条虚线线
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
mainFont = new Font("Courier New", 10);
strLine = "Total Q'ty.=" + TotalQty + " " + "Box Q'ty.=" + BoxQty;
e.Graphics.DrawString(strLine, mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
mainFont = new Font("Courier New", 10);
//e.Graphics.DrawLine(new Pen(Color.Black), leftMargin, verticalPosition, e.MarginBounds.Right, verticalPosition);//画一条线
e.Graphics.DrawString("=".PadLeft(95, '='), mainFont, Brushes.Black, leftMargin, verticalPosition, new StringFormat());//画两条虚线
verticalPosition = verticalPosition + mainFont.GetHeight(e.Graphics);
}
#endregion
}
catch (Exception ex)
{
log.Error(PubParameter.appInfo.AppName + " Printlabel Fail:" + ex.Message);
//this.txtMessage.Text = ex.Message;
MessageBox.Show(ex.Message);
}
}
C#中效果图如下: