-
May 4th, 2011, 04:37 AM
#1
[RESOLVED] Need help with printing in VB.Net 2008
Hi,
I never printed in VB.Net code so I don’t know how to do it at all.
My task is to be able to print Invoices from my application.
The Invoices need to be printed with a given Logo image (From file) and with text that I can set from my application.
All the help I can get here is very welcome
Thanks
-
May 4th, 2011, 06:07 AM
#2
Re: Need help with printing in VB.Net 2008
You need to use the printers Object..
here is a sample Printing Class that i use to print simple Barcoded tags on a lable printer
Code:
Imports System.Drawing
Namespace Printers
Public Class PrintDocument
Private WithEvents _PrintDialog As New System.Windows.Forms.PrintDialog
Private _PrinterObject As New System.Printing.LocalPrintServer
Private Regfunctions As New DataFunctions.Regfunctions.Regfunctions
Private _PrinterName As DataFunctions.Regfunctions.Regfunctions.Regkeys
Private WithEvents _PrintItem As Drawing.Printing.PrintDocument
Public Event PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Public Sub New(ByVal PrinterID As String)
_PrinterName = Regfunctions.Load(DataFunctions.Regfunctions.Regfunctions.Reg_type.Current_Config, DataFunctions.Regfunctions.Regfunctions.KeyRegloc & "\Printers", PrinterID, _PrinterObject.DefaultPrintQueue.FullName)
_PrintDialog.PrinterSettings.PrinterName = _PrinterName.KeyVal
End Sub
Public Function ShowDialog() As Windows.Forms.DialogResult
Dim TmpResult As Windows.Forms.DialogResult
TmpResult = _PrintDialog.ShowDialog()
_PrinterName.KeyVal = _PrintDialog.PrinterSettings.PrinterName
Regfunctions.Save(_PrinterName)
Return TmpResult
End Function
Public Function Print() As Windows.Forms.DialogResult
_PrintItem = New Printing.PrintDocument
_PrintItem.PrinterSettings = _PrintDialog.PrinterSettings
_PrintItem.Print()
End Function
Private Sub _PrintItem_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles _PrintItem.PrintPage
RaiseEvent PrintPage(sender, e)
End Sub
End Class
Public Class PrintTags
Private _BarcodeNo As String
Private _DateString As String
Private _PartNO As String
Private _Station As String
Private _BarCode As Image
Private Barcodes As New Barcodes.BarcodeModule
Public Sub PrintTag(ByVal BarcodeNo As String, ByVal DateStr As String, ByVal PartNO As String, ByVal Station As String)
Dim TmpRec As New Rectangle
TmpRec.Width = 280
TmpRec.Height = 100
_BarcodeNo = BarcodeNo
_DateString = DateStr
_PartNO = PartNO
_Station = Station
_BarCode = Barcodes.NewBarcode(_BarcodeNo, TmpRec)
Dim TmpPrinter As New Printers.PrintDocument("BCPrinter")
AddHandler TmpPrinter.PrintPage, AddressOf PrintTheTag
TmpPrinter.Print()
End Sub
Private Sub PrintTheTag(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim canvas As Graphics = e.Graphics
Dim Font1 As New Font("Arial", 24, FontStyle.Bold, GraphicsUnit.Pixel)
canvas.DrawString(_Station, Font1, Brushes.Black, 10, 10)
canvas.DrawString(_BarcodeNo, Font1, Brushes.Black, 10, 50)
canvas.DrawString(_PartNO, Font1, Brushes.Black, 10, 90)
canvas.DrawString(_DateString, Font1, Brushes.Black, 10, 130)
canvas.DrawImage(_BarCode, 0, 180, 280, 100)
End Sub
End Class
End Namespace
and where ever i need to print the tag i use..
Code:
Private TagPrint As New Printers.Printers.PrintTags
TagPrint.PrintTag(Fonumber, Date.Now.ToString, Part, StationName)
Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
WPF Articles : 3D Animation 1 , 2 , 3
Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.
-
May 4th, 2011, 07:25 AM
#3
Re: Need help with printing in VB.Net 2008
Thanks but i will understand better if it will be simple sample code like add lines, texts, images... send to print.
still need help...
-
May 4th, 2011, 07:42 AM
#4
Re: Need help with printing in VB.Net 2008
Actually your code give some clue about how to do it (Print what i tested at least...).
Here is the code i wrote in my form:
Code:
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim canvas As Graphics = e.Graphics
Dim Font1 As New Font("Arial", 24, FontStyle.Bold, GraphicsUnit.Pixel)
canvas.DrawString("Hello", Font1, Brushes.Black, 10, 10)
canvas.DrawString("שלום", Font1, Brushes.Black, 10, 50)
canvas.DrawString("sfddsfasdf", Font1, Brushes.Black, 10, 90)
canvas.DrawString("hhfhfhfhfhfhfhfhf", Font1, Brushes.Black, 10, 130)
canvas.DrawImage(Image.FromFile("C:\Document Icon.JPG"), 0, 180, 280, 100)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PrintDocument1.Print()
End Sub
Can you please tell me if this ok to start with?
And the most important thing how can i set the text and all the page to be as right to left and not as English left to right?
Thanks a lot
-
May 9th, 2011, 05:20 AM
#5
Re: Need help with printing in VB.Net 2008
What you have there is perfect to start with .. as for right to left ... Not too sure... you'll have to dig around in the properties for how to set that ... else you could just right justify all the text on the page....
Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
WPF Articles : 3D Animation 1 , 2 , 3
Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.
-
May 9th, 2011, 08:07 AM
#6
Re: Need help with printing in VB.Net 2008
I noticed all the code above uses the graphics object. Surely the Printer object can print plain text without the need for a graphics object.
In VB6 you could call
Printer.Print "Text String"
Always use [code][/code] tags when posting code.
-
May 9th, 2011, 08:46 AM
#7
Re: Need help with printing in VB.Net 2008
The graphics object is required when ..
The Invoices need to be printed with a given Logo image (From file)
.. and while you got it why not just put everything into the object ..
Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
WPF Articles : 3D Animation 1 , 2 , 3
Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.
-
May 9th, 2011, 09:05 AM
#8
Re: Need help with printing in VB.Net 2008
Ok, makes sense. Guess I wasn't paying enough attention
Always use [code][/code] tags when posting code.
-
May 9th, 2011, 10:18 AM
#9
Re: Need help with printing in VB.Net 2008
Many thanks to you all,
I finally got how to work with it and complete my task.
GremlinSA your code gave me all what i needed to start, thanks.
-
May 10th, 2011, 03:00 AM
#10
Re: Need help with printing in VB.Net 2008
Please mark your thread resolved
-
May 10th, 2011, 12:25 PM
#11
Re: Need help with printing in VB.Net 2008
-
May 10th, 2011, 01:34 PM
#12
Re: Need help with printing in VB.Net 2008
You click Thread tools above your first post, then you select Mark Thread resolved
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|