Results 1 to 2 of 2

Thread: how to print a file in the background [RESOLVED]

  1. #1

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539

    how to print a file in the background [RESOLVED]

    Hey all Happy Christmas

    How can i print a txt file without the need of opening it ?
    from my vb.net application i just have a button (printInv) and when they click it i just want to print a file c:\temp\inv.txt

    thats all
    Last edited by carlblanchard; Dec 28th, 2003 at 10:59 AM.
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Try this example . I don't have printer at home to test it though .

    MSDN Help
    VB Code:
    1. Example
    2. The following example prints the file that is specified through the command line to the default printer.
    3.  
    4. Note: The example assumes that each line fits within the page width.
    5.  
    6. Use the System.ComponentModel, System.Drawing, System.Drawing.Printing, System.IO, and System.Windows.Forms namespaces for this example.
    7.  
    8. Public Class PrintingExample
    9.     Private printFont As Font
    10.     Private streamToPrint As StreamReader
    11.     Private Shared filePath As String
    12.    
    13.     Public Sub New()
    14.         Printing()
    15.     End Sub    
    16.    
    17.     ' The PrintPage event is raised for each page to be printed.
    18.     Private Sub pd_PrintPage(sender As Object, ev As PrintPageEventArgs)
    19.         Dim linesPerPage As Single = 0
    20.         Dim yPos As Single = 0
    21.         Dim count As Integer = 0
    22.         Dim leftMargin As Single = ev.MarginBounds.Left
    23.         Dim topMargin As Single = ev.MarginBounds.Top
    24.         Dim line As String = Nothing
    25.        
    26.         ' Calculate the number of lines per page.
    27.         linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
    28.        
    29.         ' Iterate over the file, printing each line.
    30.         While count < linesPerPage
    31.             line = streamToPrint.ReadLine()
    32.             If line Is Nothing Then
    33.                 Exit While
    34.             End If
    35.             yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
    36.             ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, _
    37.                 yPos, New StringFormat())
    38.             count += 1
    39.         End While
    40.        
    41.         ' If more lines exist, print another page.
    42.         If Not (line Is Nothing) Then
    43.             ev.HasMorePages = True
    44.         Else
    45.             ev.HasMorePages = False
    46.         End If
    47.     End Sub
    48.      
    49.     ' Print the file.
    50.     Public Sub Printing()
    51.         Try
    52.             streamToPrint = New StreamReader(filePath)
    53.             Try
    54.                 printFont = New Font("Arial", 10)
    55.                 Dim pd As New PrintDocument()
    56.                 AddHandler pd.PrintPage, AddressOf pd_PrintPage
    57.                 ' Print the document.
    58.                 pd.Print()
    59.             Finally
    60.                 streamToPrint.Close()
    61.             End Try
    62.         Catch ex As Exception
    63.             MessageBox.Show(ex.Message)
    64.         End Try
    65.     End Sub 'Printing    
    66.    
    67.     ' This is the main entry point for the application.
    68.     Public Shared Sub Main()
    69.         Dim args() As String = System.Environment.GetCommandLineArgs()
    70.         Dim sampleName As String = args(0)
    71.         If args.Length <> 1 Then
    72.             Console.WriteLine("Usage: " & sampleName & " <file path>")
    73.             Return
    74.         End If
    75.         filePath = args(0)
    76.     End Sub
    77. End Class

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