See Also
Features
Buy Now
Online Help
Demo
Licensing

Back

How to print barcode images with Barcode Professional SDK for .NET

Technologies used
  • Neodynamic Barcode Professional SDK 2.0 for .NET
  • Microsoft .NET Framework 2.0 (or greater)
  • Microsoft Visual Studio 2005/2008 or Visual Studio 2005/2008 Express Editions (Visual Basic 2005/2008 Express, Visual C# 2005/2008 Express, etc)
As you probably already know, most printing jobs within .NET Framework are performed by using the PrintDocument class under System.Drawing.Printing namespace. The PrintDocument class is used to set the properties that describe what and where to print content such us strings, images and so on.
The main method the PrintDocument class features is Print which starts the document's printing process. After Print method is invoked, the PrintDocument raises a PrintPage event for each page to be printed. Here is where you should add your printing logic to an event handler for that event.
If your application needs to feature barcoding capabilities, it's also probably that printing documents containing barcode images are required as well.
Barcode Professional control features an overloaded method called DrawOnCanvas which lets you to draw the barcode image on any GDI+ Graphics object - an instance of System.Drawing.Graphics class. The PrintDocument's PrintPage event exposes a Graphics object where to paint the page content and here is where that Graphics object must be passed to the DrawOnCanvas method in order to get the barcode image printed.
This guide describes common scenarios regarding barcode printing using the PrintDocument class and Barcode Professional's DrawOnCanvas method:
How to print barcodes using PrintDocument class
This is the simplest barcode printing scenario. Here, the Graphics object exposed by PrintDocument's PrintPage event must be passed to the Barcode Professional's DrawOnCanvas method in order to get the barcode printed at the specified point (x, y).
Example
Supposing you have already set an event handler for the PrintDocument's PrintPage event, the following code inside that handler procedure will create a Barcode Professional object and will print the generated barcode at the specified point (in this case x=1in, y=1in) in the document/page. IMPORTANT: The unit of measure for the barcode location/point must be specified to BarcodeUnit property of BarcodeProfessional object. By default it is INCH
Visual Basic .NET
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
Visual C# .NET
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));
}
How to print scaled barcodes using PrintDocument class
One version of DrawOnCanvas method lets you to print a scaled barcode image by specifying the scale parameter. For instance, if you want to scale the original barcode to 50%, then the scale parameter must be specified as 0.5; if you want to scale the original barcode to 200%, then the scale parameter must be specified as 2; and so on.
As the previous scenario, the Graphics object exposed by PrintDocument's PrintPage event must be passed to the Barcode Professional's DrawOnCanvas method in order to get the barcode printed at the specified point (x, y) and scaled at the specified value.
Example
Supposing you have already set an event handler for the PrintDocument's PrintPage event, the following code inside that handler procedure will create a Barcode Professional object and will print the generated barcode scaled at 50% at the specified point (in this case x=2in, y=3in) in the document/page. IMPORTANT: The unit of measure for the barcode location/point must be specified to BarcodeUnit property of BarcodeProfessional object. By default it is INCH
Visual Basic .NET
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
Visual C# .NET
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);
}
How to print barcodes that need to fit a given area using PrintDocument class
It's very common to need to print the barcode in order it fits a given area, for instance: The barcode must be printed on an area of size 1.5 inch x 1 inch.
One version of DrawOnCanvas method lets you to accomplish that by specifying the target area through barsAreaInInches parameter. Keep in mind the following points about the target area:
  • The target area's size (width and height) is measured in unit specified to BarcodeUnit property of the BarcodeProfessional object involved. By default it is INCH
  • The target area involves:
    • Width: it includes the barcode bars width plus left and right Quiet Zones (QuietZoneWidth property)
    • Height: it includes the barcode bar's height only

      The target area on which the barcode must be drawn
Again, as in the previous scenarios, the Graphics object exposed by PrintDocument's PrintPage event must be passed to the Barcode Professional's DrawOnCanvas method in order to get the barcode printed fitting the specified area.
Example
Supposing you have already set an event handler for the PrintDocument's PrintPage event, the following code inside that handler procedure will create a Barcode Professional object and will print the generated barcode in order it fits an area of size 2 inch x 1 inch.
Visual Basic .NET
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
Visual C# .NET
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));
}
If you need more information or assistance, please contact our .
 Copyright © 2003 - 2010 Neodynamic S.R.L. All rights reserved.