如何用Barcode Professional SDK for .NET打印条形码图像

正如您可能知道的那样,大多数.NET框架中的打印工作是由System.Drawing.Printing 命名空间下的PrintDocument类执行的。Print-Document类被用来设置描述打印内容的字符串、图像等的属性。main函数PrintDocument类的特点是打印开始文档的打印进程。在Print方法被调用后,PrintDocument类将为每个被打印的页码添加一个PrintPage事件。这就是您应该将打印逻辑添加到该事件的事件处理程序的地方。

如果您的应用程序需要条形码功能起作用,那么打印包含条形码图像的文档也是必须的。

Barcode Professional控制一个名为DrawOnCanvas的函数方法,该函数方法可以使你在任何GDI+ Graphics对象上绘制条形码图像。PrintDocument's PrintPage事件展示了一个绘制页面内容的Graphics对象,为了打印条形码图像Graphics对象必须通过DrawOnCanvas方法。

最新版Barcode Professional SDK for .NET下载

如何使用PrintDocument类打印条形码图像

这是最简单的条形码打印方案。例如,假设您已经为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));
}          

如何使用PrintDocument类打印缩放的条形码图像

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);
}      

如何使用PrintDocument类打印条形码部分区域的图像

打印条形码的部分区域是很常见的事,例如,要打印的条形码图像被指定大小为1.5英寸x1英寸,DrawOnCanvas方法使你可以用barsAreaInInches参数指定目标区域来实现,请记住关于目标区域的以下几点:

  1. 目标区域的尺寸(长度和宽度)是由BarcodeProfessional对象包含的BarcodeUnit属性指定的。默认情况下为英寸。

  2. 目标区域包含: Width:包括条形码条宽+左右的静态区域(QuietZoneWidth属性) Height:只包括条形码条款的高度

假设您已经为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));
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值