Results 1 to 1 of 1

Thread: Simple .NET String Printer

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    Simple .NET String Printer

    Description
    The following code allows a user to print a String with various options.




    Source Code
    Code:
    Option Strict On
    Option Explicit On
    
    Imports System.Drawing
    Imports System.Drawing.Printing
    Namespace Printer
        Public Module Text
            Private defaultFont As Font = SystemFonts.DefaultFont
            Private pDocument As PrintDocument
            Private pData As String
    
            ''' <summary>
            ''' Prints the desired text using the System's default Font.
            ''' </summary>
            ''' <param name="text">The text to print.</param>
            ''' <remarks>The text will automatically word wrap and it prints to the default printer.</remarks>
            Public Sub Print(ByVal text As String)
                'Create a new instance of the PrintDocument object
                pDocument = New PrintDocument
    
                'Create the event handler to print text using the default font
                AddHandler pDocument.PrintPage, AddressOf pDocument_PrintPage
    
                'Set the print data to the text
                pData = text
    
                'Print!
                pDocument.Print()
            End Sub
    
            ''' <summary>
            ''' Prints the desired text using specified Font.
            ''' </summary>
            ''' <param name="text">The text to print.</param>
            ''' <param name="font">The font to use while printing.</param>
            ''' <remarks>The text will automatically word wrap and it prints to the default printer.</remarks>
            Public Sub Print(ByVal text As String, ByVal font As Font)
                'Create a new instance of the PrintDocument object
                pDocument = New PrintDocument
    
                'Create the event handler to print text using the default font
                AddHandler pDocument.PrintPage, AddressOf pDocument_PrintPage
    
                'Set the print data to the text
                pData = text
    
                'Set the font
                defaultFont = font
    
                'Print!
                pDocument.Print()
            End Sub
    
            ''' <summary>
            ''' Prints the desired text using the System's default Font to the specified printer.
            ''' </summary>
            ''' <param name="text">The text to print.</param>
            ''' <param name="printerName">The name of the printer to print to.</param>
            ''' <remarks>The text will automatically word wrap. The printer name should be verfied that it is contained in PrinterSettings.InstalledPrinters</remarks>
            Public Sub Print(ByVal text As String, ByVal printerName As String)
                'Create a new instance of the PrintDocument object
                pDocument = New PrintDocument
    
                'Set the printer's name
                pDocument.PrinterSettings.PrinterName = printerName
    
                'Create the event handler to print text using the default font
                AddHandler pDocument.PrintPage, AddressOf pDocument_PrintPage
    
                'Set the print data to the text
                pData = text
    
                'Print!
                pDocument.Print()
            End Sub
    
            ''' <summary>
            ''' Prints the desired text using the specified Font to the specified printer.
            ''' </summary>
            ''' <param name="text">The text to print.</param>
            ''' <param name="font">The font to use while printing.</param>
            ''' <param name="printerName">The name of the printer to print to.</param>
            ''' <remarks>The text will automatically word wrap. The printer name should be verfied that it is contained in PrinterSettings.InstalledPrinters</remarks>
            Public Sub Print(ByVal text As String, ByVal font As Font, ByVal printerName As String)
                'Create a new instance of the PrintDocument object
                pDocument = New PrintDocument
    
                'Set the printer's name
                pDocument.PrinterSettings.PrinterName = printerName
    
                'Create the event handler to print text using the default font
                AddHandler pDocument.PrintPage, AddressOf pDocument_PrintPage
    
                'Set the print data to the text
                pData = text
    
                'Set the font
                defaultFont = font
    
                'Print!
                pDocument.Print()
            End Sub
    
            Private Sub pDocument_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
                'Get the text, the amount of characters on the page, and the amount of lines per page
                Dim charactersOnPage As Integer = 0
                Dim linesPerPage As Integer = 0
    
                'Sets the value of charactersOnPage to the number of characters of s that will fit within the bounds of the page.
                e.Graphics.MeasureString(pData, defaultFont, e.MarginBounds.Size, StringFormat.GenericTypographic, charactersOnPage, linesPerPage)
    
                'Draws the string within the bounds of the page
                e.Graphics.DrawString(pData, defaultFont, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic)
    
                'Remove the portion of the string that has been printed.
                pData = pData.Substring(charactersOnPage)
    
                'Check to see if more pages are to be printed.
                e.HasMorePages = (pData.Length > 0)
            End Sub
        End Module
    End Namespace


    Example
    Code:
    'Prints the desired text to the default printer using the default font
    Printer.Text.Print("Hello World")
    
    'Prints the desired text to the default printer using the specified font
    Printer.Text.Print("Hello World", New Font("Consolas", FontStyle.Regular))
    
    'Prints the desired text to the specified printer using the default font
    Printer.Text.Print("Hello World", "My Printer Name")
    
    'Prints the desired text to the specified printer using the specified font
    Printer.Text.Print("Hello World", New Font("Consolas", FontStyle.Regular), "My Printer Name")
    Last edited by dday9; Dec 9th, 2016 at 10:50 AM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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