Solved

Print a text file

Posted on 2004-09-09
6
228 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

20 Experts available now in Live!

Get 1:1 Help Now