Introduction
Barcode is one kind of graphic identifier, formed with different width black bars and blanks. It is widely used in our daily life, for example, each product in supermarket has a barcode. Through barcode, we can get much information about one product, such as production date, category, price and so on.
In this post, I will show how to draw barcode in a PDF document by using VB.NET.
Background
In my example, I will draw four kinds of barcodes. Because Spire.PDF is used, so the code below graphic should fit with format which this component provides, for example, 00:12-3456/7890 is allowed, while (00)1234(56)7890 not allowed.
Note: before starting with steps, please add Spire.PDF dll file as reference if you want to use the following code.
Steps
Main Coding
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Barcode
Module Barcode
Sub Main()
'Create PDF
Dim doc As New PdfDocument()
'Margin
Dim unitCvtr As New PdfUnitConvertor()
Dim margin As New PdfMargins()
margin.Top = unitCvtr.ConvertUnits(2.54F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)
margin.Bottom = margin.Top
margin.Left = unitCvtr.ConvertUnits(3.17F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)
margin.Right = margin.Left
Dim section As PdfSection = doc.Sections.Add()
section.PageSettings.Margins = margin
section.PageSettings.Size = PdfPageSize.A4
'Add Page
Dim page As PdfPageBase = section.Pages.Add()
Dim y As Single = 20
Dim brush1 As PdfBrush = PdfBrushes.DarkCyan
Dim font1 As New PdfTrueTypeFont(New Font("Calibri", 12.0F, FontStyle.Regular), True)
Dim rctg As New RectangleF(New PointF(0, 0), page.Canvas.ClientSize)
'Draw BarCode
Dim text As New PdfTextWidget()
text.Font = font1
text.Brush = brush1
text.Text = "BarCode-1:"
Dim result As PdfLayoutResult = text.Draw(page, 0, y)
page = result.Page
y = result.Bounds.Bottom + 2
Dim barcode1 As New PdfCodabarBarcode("00:329-061106")
barcode1.BarcodeToTextGapHeight = 1.0F
barcode1.EnableCheckDigit = True
barcode1.ShowCheckDigit = True
barcode1.TextDisplayLocation = TextLocation.Bottom
barcode1.TextColor = Color.DarkOrange
barcode1.Draw(page, New PointF(0, y))
y = barcode1.Bounds.Bottom + 5
text.Text = "BarCode-2"
result = text.Draw(page, 0, y)
page = result.Page
y = result.Bounds.Bottom + 2
Dim barcode2 As New PdfCode11Barcode("001-420160")
barcode2.BarcodeToTextGapHeight = 1.0F
barcode2.TextDisplayLocation = TextLocation.Bottom
barcode2.TextColor = Color.DarkOrange
barcode2.Draw(page, New PointF(0, y))
y = barcode2.Bounds.Bottom + 5
text.Text = "BarCode-3"
result = text.Draw(page, 0, y)
page = result.Page
y = result.Bounds.Bottom + 2
Dim barcode3 As New PdfCode39Barcode("25-41061990257")
barcode3.BarcodeToTextGapHeight = 1.0F
barcode3.TextDisplayLocation = TextLocation.Bottom
barcode3.TextColor = Color.DarkOrange
barcode3.Draw(page, New PointF(0, y))
y = barcode3.Bounds.Bottom + 5
text.Text = "BarCode-4"
result = text.Draw(page, 0, y)
page = result.Page
y = result.Bounds.Bottom + 2
Dim barcode4 As New PdfCode32Barcode("30026901")
barcode4.BarcodeToTextGapHeight = 1.0F
barcode4.TextDisplayLocation = TextLocation.Bottom
barcode4.TextColor = Color.DarkOrange
barcode4.Draw(page, New PointF(0, y))
y = barcode4.Bounds.Bottom + 5
'Save
doc.SaveToFile("Barcode.pdf")
doc.Close()
'Launch
Process.Start("Barcode.pdf")
End Sub
EndModule
Result Shown by Following

__________________________________________________________________________________________
Click Here to Learn more about Spire.PDF
Click Here to Download Spire.PDF