Link to home
Start Free TrialLog in
Avatar of jerrycsakanyjr
jerrycsakanyjr

asked on

Print a text file

I have a text file that is called log.txt . I want to print it . How do i do this in the most simple and uncomplicated way. No print dialog, no font changes, just print it to whatever the default printer is.
Avatar of iboutchkine
iboutchkine

Imports System.IO
Imports System.Drawing.Printing

Public Class Form1
    Inherits System.Windows.Forms.Form

    'check if declared in Windows Form Designer generated code
    'Private components As System.ComponentModel.Container
    Private printButton As System.Windows.Forms.Button
    Private printFont As Font
    Private streamToPrint As StreamReader

#Region " Windows Form Designer generated code "
#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            streamToPrint = New StreamReader("C:\temp\vb.txt")
            Try
                printFont = New Font("Arial", 10)
                Dim pd As New PrintDocument()
                pd.DocumentName = "C:\temp\vb.txt" 'we need it only for msgbox
                AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
                'pd.Print()
                MessageBox.Show(pd.DocumentName + " has finished printing.")
            Finally
                streamToPrint.Close()
            End Try
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    ' The PrintPage event is raised for each page to be printed.
    Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim linesPerPage As Single = 0
        Dim yPos As Single = 0
        Dim count As Integer = 0
        Dim leftMargin As Single = ev.MarginBounds.Left
        Dim topMargin As Single = ev.MarginBounds.Top
        Dim line As String = Nothing

        ' Calculate the number of lines per page.
        linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)

        ' Print each line of the file.
        While count < linesPerPage
            line = streamToPrint.ReadLine()
            If line Is Nothing Then
                Exit While
            End If
            yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
            ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
            count += 1
        End While

        ' If more lines exist, print another page.
        If Not (line Is Nothing) Then
            ev.HasMorePages = True
        Else
            ev.HasMorePages = False
        End If
    End Sub
End Class
Avatar of jerrycsakanyjr

ASKER

ddHandler pd.PrintPage, AddressOf Me.pd_PrintPage

is giving me an error saying:
'pd_PrintPage' is not a member of 'BarrettaFTP.frmFTP'

i got rid of that error now i'm getting a different one that says:

'0' is not az valid value for 'emSize' 'emSize' should be greater than 0 and less than or equal to system.single.maxvalue. Parameter name: emSize
using System.Diagnostics

ProcessStartInfo pi = new ProcessStartInfo(@"C:\log.txt");
pi.Verb = "print";
Process p = new Process ();
p.StartInfo = pi;
p.Start();
ASKER CERTIFIED SOLUTION
Avatar of Fahad Mukhtar
Fahad Mukhtar
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
add
pi.WindowStyle = ProcessWindowStyle.Hidden

if you want to avoid the Notepad screen