Results 1 to 16 of 16

Thread: print barcodes using printdocument

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    34

    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?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: print barcodes using printdocument

    try this:

    vb Code:
    1. e.Graphics.DrawString(label.text, _
    2. New Font("Code 128", 28), Brushes.Black, 25, 77 + LabelsOnPage * 135)

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    34

    Re: print barcodes using printdocument

    Thank you for your reply.

    However this still outputs the print incorrectly:


    where as in the label (what i want looks like this):

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: print barcodes using printdocument

    you might have to print them as images

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    34

    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?

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: print barcodes using printdocument

    have a look at the label's .drawtobitmap method:

    http://msdn.microsoft.com/en-us/libr...wtobitmap.aspx

  7. #7
    Member
    Join Date
    May 2010
    Posts
    60

    Re: print barcodes using printdocument

    Is a font problem.
    You dont get junk you get the barcode in the default font.

    vb Code:
    1. Public Class Form1
    2.     Dim fuente As Font
    3.     Dim WithEvents documento As Printing.PrintDocument
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.         fuente = New Font("Code 128", 28)
    6.         Label1.Text = "Í     !È3DÎ"
    7.         Label1.Font = fuente 'New Font("Code 128", 28)
    8.         documento = New Printing.PrintDocument
    9.     End Sub
    10.  
    11.     Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    12.         e.Graphics.DrawString(Label1.Text, New Font("Code 128", 28), Brushes.LightPink, 1, 1)
    13.     End Sub
    14.  
    15.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    16.         Me.PrintPreviewDialog1.Document = documento
    17.         Me.PrintPreviewDialog1.Show()
    18.     End Sub
    19.  
    20.     Private Sub documento_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles documento.PrintPage
    21.         e.Graphics.DrawString(Label1.Text, fuente, Brushes.Black, 1, 1)
    22.     End Sub
    23. 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
    Last edited by yo mismo; Dec 1st, 2011 at 09:51 AM.

  8. #8
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  9. #9
    Member
    Join Date
    May 2010
    Posts
    60

    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.
    Last edited by yo mismo; Dec 1st, 2011 at 11:56 AM.

  10. #10

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    34

    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
    Last edited by ww99w; Dec 1st, 2011 at 09:04 PM.

  11. #11

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    34

    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
    Last edited by ww99w; Dec 1st, 2011 at 11:34 PM.

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: print barcodes using printdocument

    vb Code:
    1. Dim img As New Bitmap(Label1.Width, Label1.Height)
    2. Label1.DrawToBitmap(img, Rectangle.Round(img.GetBounds(Drawing.GraphicsUnit.Pixel)))

  13. #13
    Member
    Join Date
    May 2010
    Posts
    60

    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.
    Attached Images Attached Images  

  14. #14
    New Member
    Join Date
    Nov 2012
    Posts
    3

    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.

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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

  16. #16

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    34

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width