CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Sep 2002
    Location
    Israel
    Posts
    396

    [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

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    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.

  3. #3
    Join Date
    Sep 2002
    Location
    Israel
    Posts
    396

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

  4. #4
    Join Date
    Sep 2002
    Location
    Israel
    Posts
    396

    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

  5. #5
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    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.

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.

  7. #7
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    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.

  8. #8
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.

  9. #9
    Join Date
    Sep 2002
    Location
    Israel
    Posts
    396

    Resolved 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.

  10. #10
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Need help with printing in VB.Net 2008

    Please mark your thread resolved

  11. #11
    Join Date
    Sep 2002
    Location
    Israel
    Posts
    396

    Re: Need help with printing in VB.Net 2008

    No prob, how do i do it?

  12. #12
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    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
  •  





Click Here to Expand Forum to Full Width

Featured