原創|使用教程|編輯:龔雪|2016-03-08 17:14:58.000|閱讀 791 次
概述:本次教程主要介紹了在Barcode Professional SDK for .NET中運用PrintDocument類打印各類型條形碼圖像的方法。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
正如您可能知道的那樣,大多數.NET框架中的打印工作是由System.Drawing.Printing 命名空間下的PrintDocument類執行的。Print-Document類被用來設置描述打印內容的字符串、圖像等的屬性。main函數PrintDocument類的特點是打印開始文檔的打印進程。在Print方法被調用后,PrintDocument類將為每個被打印的頁碼添加一個PrintPage事件。這就是您應該將打印邏輯添加到該事件的事件處理程序的地方。
如果您的應用程序需要條形碼功能起作用,那么打印包含條形碼圖像的文檔也是必須的。
Barcode Professional控制一個名為DrawOnCanvas的函數方法,該函數方法可以使你在任何GDI+ Graphics對象上繪制條形碼圖像。PrintDocument's PrintPage事件展示了一個繪制頁面內容的Graphics對象,為了打印條形碼圖像Graphics對象必須通過DrawOnCanvas方法。
這是最簡單的條形碼打印方案。例如,假設您已經為PrintDocument's PrintPage事件設置了一個事件處理程序,在下列的代碼中,文檔/頁面中處理器程序將會創建一個Barcode Professional對象并打印在指定位置生成的條形碼圖像。
Private Sub printDocumentObject_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) 'Create a Barcode Professional object Dim bcp As New BarcodeProfessional() 'Barcode settings bcp.Symbology = Symbology.Code39 bcp.Code = "123456789" '... 'Print the barcode at x=1in, y=1in using DrawOnCanvas method bcp.DrawOnCanvas(e.Graphics, New PointF(1F, 1F)) End Sub
private void printDocumentObject_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { //Create a Barcode Professional object BarcodeProfessional bcp = new BarcodeProfessional(); //Barcode settings bcp.Symbology = Symbology.Code39; bcp.Code = "123456789"; //... //Print the barcode at x=1in, y=1in using DrawOnCanvas method bcp.DrawOnCanvas(e.Graphics, new PointF(1f, 1f)); }
DrawOnCanvas方法的一個版本使你可以通過特定的scale參數來打印縮放的條形碼圖像。例如,如果你想把條形碼圖像縮放至當前大小的50%,那么scale參數必須指定為0.5;如果你想把條形碼圖像擴大至當前大小的200%,那么scale參數必須指定為2。
假設您已經為PrintDocument's PrintPage事件設置了一個事件處理程序,在下列的代碼中,文檔/頁碼中的處理器程序將會創建一個Barcode Professional對象并打印在指定位置生成的縮放比例為50%的條形碼圖像。
Private Sub printDocumentObject_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) 'Create a Barcode Professional object Dim bcp As New BarcodeProfessional() 'Barcode settings bcp.Symbology = Symbology.Code39 bcp.Code = "123456789" '... 'Print the barcode at x=2in, y=3in and scaled at 50% using DrawOnCanvas method bcp.DrawOnCanvas(e.Graphics, New PointF(2F, 3F), 0.5F) End Sub
private void printDocumentObject_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { //Create a Barcode Professional object BarcodeProfessional bcp = new BarcodeProfessional(); //Barcode settings bcp.Symbology = Symbology.Code39; bcp.Code = "123456789"; //... //Print the barcode at x=2in, y=3in and scaled at 50% using DrawOnCanvas method bcp.DrawOnCanvas(e.Graphics, new PointF(2f, 3f), 0.5f); }
打印條形碼的部分區域是很常見的事,例如,要打印的條形碼圖像被指定大小為1.5英寸x1英寸,DrawOnCanvas方法使你可以用barsAreaInInches參數指定目標區域來實現,請記住關于目標區域的以下幾點:
假設您已經為PrintDocument's PrintPage事件設置了一個事件處理程序,在下列的代碼中,文檔/頁碼中的處理器程序將會創建一個Barcode Professional對象并打印生成的尺寸大小為2英寸x1英寸的條形碼圖像。
Private Sub printDocumentObject_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) 'Create a Barcode Professional object Dim bcp As New BarcodeProfessional() 'Barcode settings bcp.Symbology = Symbology.Code39 bcp.Code = "123456789" '... 'Print the barcode to fit an area of size 2x1in using DrawOnCanvas method bcp.DrawOnCanvas(e.Graphics, New PointF(0,0), New SizeF(2F,1F)) End Sub
private void printDocumentObject_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { //Create a Barcode Professional object BarcodeProfessional bcp = new BarcodeProfessional(); //Barcode settings bcp.Symbology = Symbology.Code39; bcp.Code = "123456789"; //... //Print the barcode to fit an area of size 2x1in using DrawOnCanvas method bcp.DrawOnCanvas(e.Graphics, New PointF(0,0), New SizeF(2f,1f)); }
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn