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.PrinterSett ings.Print erName = "Zebra"
PrintDocument1.DefaultPage Settings.L andscape = True
PrintDocument1.DefaultPage Settings.P rinterSett ings.Copie s = 1
PrintDocument1.DefaultPage Settings.M argins.Lef t = 60
PrintDocument1.DefaultPage Settings.M argins.Top = 90
PrintDocument1.Print()
End Sub
I'm getting an error when I push the button:
************** Exception Text **************
System.NullReferenceExcept ion: Object reference not set to an instance of an object.
at PrBarPrep.Form1.btnPrint_C lick(Objec t sender, EventArgs e) in C:\Documents and Settings\owensjo.TX\My Documents\Visual Studio Projects\PrBarPrep\Form1.v b:line 99
at System.Windows.Forms.Contr ol.OnClick (EventArgs e)
at System.Windows.Forms.Butto n.OnClick( EventArgs e)
at System.Windows.Forms.Butto n.OnMouseU p(MouseEve ntArgs mevent)
at System.Windows.Forms.Contr ol.WmMouse Up(Message & m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Contr ol.WndProc (Message& m)
at System.Windows.Forms.Butto nBase.WndP roc(Messag e& m)
at System.Windows.Forms.Butto n.WndProc( Message& m)
at System.Windows.Forms.Contr olNativeWi ndow.OnMes sage(Messa ge& m)
at System.Windows.Forms.Contr olNativeWi ndow.WndPr oc(Message & m)
at System.Windows.Forms.Nativ eWindow.Ca llback(Int Ptr 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
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.PrinterSett
PrintDocument1.DefaultPage
PrintDocument1.DefaultPage
PrintDocument1.DefaultPage
PrintDocument1.DefaultPage
PrintDocument1.Print()
End Sub
I'm getting an error when I push the button:
************** Exception Text **************
System.NullReferenceExcept
at PrBarPrep.Form1.btnPrint_C
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Nativ
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
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(B yVal sender As Object, ByVal e As System.Drawing.Printing.Pr intPageEve ntArgs) Handles PrintDocument1.PrintPage
End Sub
It is within that event where you will setup how you want your document to be printed out.
Private Sub PrintDocument1_PrintPage(B
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
http://www.developer.com/net/asp/article.php/3102381
ASKER
Really all I need to do is send a couple strings to the printer as though it were a normal printer.
JP
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?
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?
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
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.
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.
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
JP
ASKER
So how do I set X and Y coordinates for print?
JP
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.Pr intPageEve ntArgs) 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(oImag e, 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.
Private Sub PDoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.Pr
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(oImag
'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.
ASKER
Tell me about turning text into an image?
JP
JP
Here is how you add text to an image:
http://www.wwwcoder.com/main/Default.aspx?tabid=68&mid=407&site=1727&parentid=177
http://www.wwwcoder.com/main/Default.aspx?tabid=68&mid=407&site=1727&parentid=177
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.Format64bppPAr gb)
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(textT oBeDrawn, 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. :)
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.Format64bppPAr
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(textT
after doing as shown above you are ready to print your text out by sendig the oImage buffer to your PrintDocument. :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Most of the time you have to use an API from the barcode software that works inside your app..