print barcodes using printdocument
I am trying to print a barcode
I have successfully made the barcode in a label using a reference .dll file
Imports BarCode
TempLabel = New Label
With TempLabel
.Text = BarcodeConverter128.StringToBarcode(CStr(tblPrintLabels.Rows(intLabelCount)("BARCODE_NO")))
.Font = New Font("Code 128", 28)
.AutoSize = True
.Location = New Point(25, 77 + 135 * intLabelCount)
End With
Me.Controls.Add(TempLabel)
however when i try to print this using :
e.Graphics.DrawString(BarcodeConverter128.StringToBarcode(CStr(tblPrintLabels.Rows(intLabelCount)("B ARCODE_NO"))), _
prFont1, Brushes.Black, 25, 77 + LabelsOnPage * 135)
I get a bunch of junk.
is there a way for the print document control to also use the reference that i added?
Re: print barcodes using printdocument
try this:
vb Code:
e.Graphics.DrawString(label.text, _
New Font("Code 128", 28), Brushes.Black, 25, 77 + LabelsOnPage * 135)
Re: print barcodes using printdocument
Thank you for your reply.
However this still outputs the print incorrectly:
http://img710.imageshack.us/img710/7...barcodebad.png
where as in the label (what i want looks like this):
http://img38.imageshack.us/img38/767...arcodegood.png
Re: print barcodes using printdocument
you might have to print them as images
Re: print barcodes using printdocument
gah i feared that i had to do it this way.
How would one go about changing it to an image?
Re: print barcodes using printdocument
Re: print barcodes using printdocument
Is a font problem.
You dont get junk you get the barcode in the default font.
vb Code:
Public Class Form1
Dim fuente As Font
Dim WithEvents documento As Printing.PrintDocument
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
fuente = New Font("Code 128", 28)
Label1.Text = "Í !È3DÎ"
Label1.Font = fuente 'New Font("Code 128", 28)
documento = New Printing.PrintDocument
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawString(Label1.Text, New Font("Code 128", 28), Brushes.LightPink, 1, 1)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.PrintPreviewDialog1.Document = documento
Me.PrintPreviewDialog1.Show()
End Sub
Private Sub documento_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles documento.PrintPage
e.Graphics.DrawString(Label1.Text, fuente, Brushes.Black, 1, 1)
End Sub
End Class
Open a new project with a label a button and a printpreviewdialog and paste this code.
I have tried and it shows perfectly fine.
Link to the font if anyone want to try
http://www.dafont.com/code-128.font
Re: print barcodes using printdocument
Are you sure the font is installed? You could use this code to check.
Code:
...
If Not IsFontInstalled("Code 128") Then
Throw New Exception("Code 128 font is not installed.")
End If
...
Private Function IsFontInstalled(ByVal fontName As String) As Boolean
Dim installedFonts As New System.Drawing.Text.InstalledFontCollection
Dim fontFamilies = installedFonts.Families
For Each family In fontFamilies
If fontName.Equals(family.Name, StringComparison.InvariantCultureIgnoreCase) Then
Return True
End If
Next
Return False
End Function
Re: print barcodes using printdocument
If he uses the same font is not very compatible with visual studio. If i try to use it directly at desing time i get an error."The font is not truetype". If you set the font at runtime all runs OK.
Re: print barcodes using printdocument
Thank you for all your replies,
@Wild_Bill My font has been installed. When I try your code, it doesn't throw the new exception.
@yo_mismo I want to confirm that when you use the code you provided, you can see the barcode instead of the letters IN the PRINT PREVIEW. Because I still letters and not the barcode in print preview. As a label on the form, the barcode is correct
and if any1 is interested, the dll reference to convert to barcode is
http://www.jtbarton.com/Barcodes/Bar...erExample.aspx
Re: print barcodes using printdocument
Decided to draw it as an image. since i cant figure out the blasted drawing font problem.
so just made a label, made it an image, and print the sucker
Thank you all for your inputs.
code below if interested:
Code:
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim img As New Bitmap(130, 40)
Dim imgsize As Size
Dim rect As New Rectangle(12, 12, 130, 40)
Dim myFont As New Font("Code 128", 28)
Label1.Text = "Í !È3DÎ"
Label1.Font = myFont
Label1.DrawToBitmap(img, rect) 'problem is here
imgsize = New Size(img.Width, img.Height)
e.Graphics.DrawImage(img, New Rectangle(New Point(12, 12), imgsize))
e.HasMorePages = False
End Sub
Re: print barcodes using printdocument
vb Code:
Dim img As New Bitmap(Label1.Width, Label1.Height)
Label1.DrawToBitmap(img, Rectangle.Round(img.GetBounds(Drawing.GraphicsUnit.Pixel)))
1 Attachment(s)
Re: print barcodes using printdocument
Yes the barcode shows as label in black as drawstring directly to the form in pink and in printpreview in black.
Screen attached to disipate doubts.
Re: print barcodes using printdocument
First, are you sure the font was installed?and then draw it as an image, you can choose the image format.
Re: print barcodes using printdocument
the last post was nearly 12 months ago... chances are the OP has an answer to their question by now:D
Re: print barcodes using printdocument
Yah wow... that was a while ago...
turned out I was doing it all wrong..
printing barcodes you need to use a dedicated barcode printer. all it will be very hard for scanners to read the barcodes.
These barcode printers use there own script which are in normal text .txt files.
and you control what is printed from the textfiles
if you required some major changes, you just repeatedly yell at the printer supplier over the phone until they come and take you request.