Link to home
Start Free TrialLog in
Avatar of gleznov
gleznov

asked on

Printing - PrintDocument object

Hi,

     I've never printed from VB.NET before, so I'm kinda confused.  Here's what I've been able to piece together for code:

At the top:
Imports System.Drawing.Printing
Imports System.Drawing.Image

Sub:
   Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        ' Create a default font to print with
        Dim hl_Font As Font = New Font("Arial", 5, FontStyle.Regular, GraphicsUnit.Point)
        Dim s_Font As Font = New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point)
        Dim m_Font As Font = New Font("Free 3 of 9", 36, FontStyle.Regular, GraphicsUnit.Point)
        Dim R_Font As Font = New Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Point)
        Dim rs_Font As Font = New Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Point)
        Dim rbig_Font As Font = New Font("Arial", 44, FontStyle.Bold, GraphicsUnit.Point)
        Dim ms_Font As Font = New Font("Free 3 of 9", 26, FontStyle.Regular, GraphicsUnit.Point)
        ' Number of lines printed so far
        Dim m_Lines As Integer = 0
        ' Custom settings for this page
        Dim pgsCustom As PageSettings = New PageSettings
        ' When the user clicks the Print button, just
        ' call the Print method of the PrintDocument and
        ' wait for the events to come back
        Dim PrintDocument1 As PrintDocument
        PrintDocument1.PrinterSettings.PrinterName = "Zebra"
        PrintDocument1.DefaultPageSettings.Landscape = True
        PrintDocument1.DefaultPageSettings.PrinterSettings.Copies = 1
        PrintDocument1.DefaultPageSettings.Margins.Left = 60
        PrintDocument1.DefaultPageSettings.Margins.Top = 90
        PrintDocument1.Print()
    End Sub

I'm getting an error when I push the button:
************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
   at PrBarPrep.Form1.btnPrint_Click(Object sender, EventArgs e) in C:\Documents and Settings\owensjo.TX\My Documents\Visual Studio Projects\PrBarPrep\Form1.vb:line 99
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


What I'm trying to do is learn to print to this barcode printer - I want to print one text line that's a barcode and then one under it in normal numbers.  Any help would be appreciated.
For example purposes, feel free to show me how to print *12345* for the barcode and 12345 underneath.

JP
Avatar of Howard Cantrell
Howard Cantrell
Flag of United States of America image

If you are printing to a bar code printer then there should be some software and instructions with it.
Most of the time you have to use an API from the barcode software that works inside your app..
Avatar of JohnWoo
JohnWoo

Can I know teh format of the file you are trying to print (eg. bitmap, text, etc....). I used to print barcodes as well but not on a barcode printer but a normal printer. Base on your code, I think its' probably you didn't setup the printpage for the printdocument you are trying to print. The printpage is one of the event invoked when you fire the print button. It's in the form as follow:

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

End Sub

It is within that event where you will setup how you want your document to be printed out.
FWIW, Printing was a nightmare for me until I found this article, which made my life so much easier - check it out here:
http://www.developer.com/net/asp/article.php/3102381
Avatar of gleznov

ASKER

Really all I need to do is send a couple strings to the printer as though it were a normal printer.

JP
Yup - just add the class from the article listed above to your project. It derives from PrintDocument anyway, it just fixes it shortcomings.

Then, to print, do this:

 
 Dim MyPrintObject As New TextPrint("This is the text I'd like to print.")
 MyPrintObject.Print()

That looks a lot easier to me than all the lines of code you have listed above?
Avatar of gleznov

ASKER

That class is great but I can't figure out how to move the printout around - on these little labels, at the bottom right I'm getting the very top of the barcode.  I need to be able to reposition it or define the label size or something.  How do I do that?

JP
It's hard to say without seeing it. It sounds like you are printing perhaps just a little more than one page, so perhaps you can make the text a little smaller, allowing more space for the barcode to print at the bottom? Like this:

MyPrintObject.Font = New Font("Arial", 8)

Otherwise, position it so it prints on page two.

Printing a couple lines of text (as you mentioned above) is pretty easy. Formatting printing is an art unto itself however.


Avatar of gleznov

ASKER

No, it's actually much much less than a page - the labels are about 3 inches long by 2 inches wide.  I need to be able to pull the text further up.  Are there maybe margins I can change?

JP
Avatar of gleznov

ASKER

So how do I set X and Y coordinates for print?

JP
Instead of having your barcode as text why don't you try printing it as image......draw everything into an image then only set the layout of the printing. I am not exactly familiar with the text printing class. Unless you know how to modify its' properties to suite your needs I would rather do the printing layout manually. This is how I am going to set my printpage event after I finish drawing my barcode and hit the print method of the PrintDocument

 Private Sub PDoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PDoc.PrintPage
        Dim xpos, ypos As Single
        Dim lineHeight As Integer
        Dim linewidth As Integer
        Dim i As Integer

        'set the height and width of print object
        lineHeight = oImage.Height
        linewidth = oImage.Width

        ' set the starting position at the left and top margins
        ypos = e.MarginBounds.Top
        xpos = leftMargin.Left
       
        'y = number of lines printed
        'x = space occupied by the printed object on a line
        'keeping track of the y position the object being printed on the page
        While ypos < e.MarginBounds.Bottom
            'keeping track of the space availabe to print the object on a line
            While xpos < e.MarginBounds.Right
                'print the object
                e.Graphics.DrawImage(oImage, xpos, ypos)
                'the next x position for the object to be printed on the same line
                xpos = xpos + linewidth
            End While
           
            'go to next line if the line was fully occupied
            If  xpos > e.MarginBounds.Right Then
                ypos = ypos + lineHeight
            End If
           
            'reset the starting position of the printing on the new line
            If xpos > e.MarginBounds.Right Then
                xpos = e.MarginBounds.Left - leftMargin
            End If
           
            'exits the loop if the page was fully printed
            If ypos > e.MarginBounds.Bottom Then
                Exit While
            End If
        End While
End Sub

You can use the property of "e.HasMorePages" at the end of the printpage event and set it true or false to tell your application whether there are still pages to print or not. If it is true, the event will automatically repeats itself to print to the next page until the HasMorePages value is set to false. The "oImage" used above the example was an image buffer I declared to draw the barcode on it.  
You can also use text as your print object but the reason I use image is because the size of the image can be easily set and fixed before you start printing so you could determine the height and width required for each line to print the object.
Avatar of gleznov

ASKER

Tell me about turning text into an image?

JP
well, the most direct way to draw string onto your image is as follow:

Private oImage As Bitmap
Private oGraphics As Graphics
Private strFormat As New StringFormat
Private FontForText As Font
Private textToBeDrawn as String

'the size of the image was set according to t he size of the picture box
oImage = New Bitmap(objPicBox.Width, objPicBox.Height, PixelFormat.Format64bppPArgb)
oGraphics = Graphics.FromImage(oImage)

'setting up the properties of the text to be drawn
strFormat.FormatFlags = StringFormatFlags.NoWrap
strFormat.Alignment = StringAlignment.Center
FontForText = New Font("Times New Roman", 10)

textToBeDrawn  = "12345678"

'Sart drawing the text onto the image
'x-coordinate = 155
'y-coordinate = 255
oGraphics.DrawString(textToBeDrawn, FontForText, New System.Drawing.SolidBrush(objPicBox.ForeColor), 155, 255, strFormat)

after doing as shown above you are ready to print your text out by sendig the oImage buffer to your PrintDocument. :)

ASKER CERTIFIED SOLUTION
Avatar of JohnWoo
JohnWoo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial