VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 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, 15= "NO#"
    Mid(NewLine2, 1030= "System S/N"
    Mid(NewLine2, 3520= "BOX_ID"
    Mid(NewLine2, 6020= "Pallet_ID"
    
    
While Not rs.EOF
        NewLine2 
= String(100" ")
        
Mid(NewLine2, 15= Trim(rs("No"))
        
Mid(NewLine2, 1030= Trim(rs("SN"))
        
Mid(NewLine2, 3520= Trim(rs("BOX_ID"))
        
Mid(NewLine2, 6020= "" & 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纸用的变量

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

    
//打印具体的SN,BoxID,PalletID列表

    
//如果总行数大于目前实际行号,表明还有页要打,lineQty是估计数,因此实际用时要仔细估算
    if (lineQty > lineNo)   
    
{
        e.HasMorePages 
= true
    }

    else
    {
        e.HasMorePages 
= false;
    }


    
//打印结尾信息

    printingPageNo
++//页号加一
}

复制代码

 

打印效果大致截图:(如果有多页,尾部的信息只在最后一页显示)
 
总结:如此简单的功能,在.Net中实现起来确实那么的复杂,尤其是对于自动分页的功能,虽然总体上功能强大了好多,但是把原先那么好用的东西给去掉了。以上代码改写成VB.Net的话,都差不多。。。

 

 



相关教程