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.
ASKER
ddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
is giving me an error saying:
'pd_PrintPage' is not a member of 'BarrettaFTP.frmFTP'
is giving me an error saying:
'pd_PrintPage' is not a member of 'BarrettaFTP.frmFTP'
ASKER
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
'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();
ProcessStartInfo pi = new ProcessStartInfo(@"C:\log.
pi.Verb = "print";
Process p = new Process ();
p.StartInfo = pi;
p.Start();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
add
pi.WindowStyle = ProcessWindowStyle.Hidden
if you want to avoid the Notepad screen
pi.WindowStyle = ProcessWindowStyle.Hidden
if you want to avoid the Notepad screen
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.Cont
Private printButton As System.Windows.Forms.Butto
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.t
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.Documen
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.Gra
' 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.Gra
ev.Graphics.DrawString(lin
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