Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Print a text file

Posted on 2004-09-09
6
Medium Priority
?
236 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
[X]
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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:Fahad Mukhtar
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:
Fahad Mukhtar earned 2000 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:Fahad Mukhtar
ID: 12018183
add
pi.WindowStyle = ProcessWindowStyle.Hidden

if you want to avoid the Notepad screen
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

722 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