Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Print a text file

Posted on 2004-09-09
6
231 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net - Project Software to help the Developer 7 40
Crystal reports vb.net 2 52
VB.NET 1 27
vb.net Configuration, settings file location? 8 27
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

790 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