Solved

Print a text file

Posted on 2004-09-09
6
229 Views
Last Modified: 2011-04-14
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.
0
Comment
Question by:jerrycsakanyjr
  • 3
  • 2
6 Comments
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12017187
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
0
 

Author Comment

by:jerrycsakanyjr
ID: 12017731
ddHandler pd.PrintPage, AddressOf Me.pd_PrintPage

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

0
 

Author Comment

by:jerrycsakanyjr
ID: 12018020
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 Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 19

Expert Comment

by:Desp
ID: 12018029
using System.Diagnostics

ProcessStartInfo pi = new ProcessStartInfo(@"C:\log.txt");
pi.Verb = "print";
Process p = new Process ();
p.StartInfo = pi;
p.Start();
0
 
LVL 19

Accepted Solution

by:
Desp earned 500 total points
ID: 12018152
in vb.net:

imports System.Diagnostics

        Dim pi As ProcessStartInfo = New ProcessStartInfo("C:\log.txt")
        pi.Verb = "print"
        Dim p As Process = New Process()
        p.StartInfo = pi
        p.Start()
0
 
LVL 19

Expert Comment

by:Desp
ID: 12018183
add
pi.WindowStyle = ProcessWindowStyle.Hidden

if you want to avoid the Notepad screen
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now