I can send a textfile to a printer.
What i would like to do is read the first line in the textfile and print the first line of text in coordintaes x and y specified in
my code.
Then read the next line of text from the textfile and print the
line of text on a new page coordinates x and y.
LOOP - Get next line from textfile and print on new page
coordinates x and y until end of file.
Can anyone help
Thanks
Adam
Replies (13)
You will call Private Sub PrintDocument1_PrintPage from a button.click from a Button.Click
e.g.
Private Sub Button1_Click(ByVal Sender As System.Object, ByVal e as system.eventargs)
Handles Button1.Click
PrintDocument1.Print()
End Sub
Where PrintDocument1 is a PrintDocument component from the ToolBox .
Mfnt is a font that you created using
Dim mfnt as Font = New Font("Verdana" , 10, FontStyle.Regular, GraphicsUnit.Point)
Thanks
i still have an error with:
e.Graphics.DrawString(CType(al(item), System.String), mfnt, Brushes.Black, 5)
i changed to:
e.Graphics.DrawString(CType(al(item), System.String), mfnt, Brushes.Black, 5,5)
an extra field of 5 which i thought was print location
it starts the printer but nothing is printed.
my text file has 5 lines. It only says it is printing 1 page but does not.
I will get there in the end
thanks again
adam
'////
' declare the StreamReader, for accessing the file
Dim din As StreamReader = File.OpenText("c:test.txt")
Dim str As String
'Store the string values
Dim al As ArrayList = New ArrayList
'The current string to be printed
Dim item As Integer = 0
'///
'//////////////////////////////////////////////////////////////////////
Function Print(ByVal TextFile As String) As String
Try
Console.WriteLine("Reading data from a textfile.." & TextFile)
Console.WriteLine()
Do
Str = din.ReadLine()
If str Nothing Then
al.Add(str)
End If
Loop Until str = Nothing
Catch E As Exception
Console.WriteLine("There was an error reading the file… " & TextFile)
End Try
Return TextFile
End Function
'////////////////////////////////////////////////////////////
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim mfnt As Font = New Font("Verdana", 10, FontStyle.Regular, GraphicsUnit.Point)
'height of font
Dim intFontHeight As Integer = New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point).GetHeight(e.Graphics)
'Print the text using the font
e.Graphics.DrawString(CType(al(item), System.String), mfnt, Brushes.Black, 5, 5)
item = item + 1
If item thanks
adam
Change This Section
/////////////////////////////////////////
If item < al.Count Then
e.HasMorePages = False
Else
e.HasMorePages = True
End If
////////////////////////////////////////
TO
If item < al.Count Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
Please find below some sample code
Modify it to suit your needs and give us a feedback
Imports System
Imports System.IO
Namespace DotNetSmart
' declare the StreamReader, for accessing the file
Dim din As StreamReader= File.OpenText(TextFile)
Dim str As String
'Store the string values
Dim al As ArrayList = New ArrayList()
'The current string to be printed
Dim item as Integer = 0
class Printing
public shared Function Print(TextFile As String) As String
Try
Console.WriteLine("Reading data from a textfile.." & TextFile)
Console.WriteLine()
Do
str = din.ReadLine()
If str Nothing Then
al.Add(str)
End If
Loop Until str = Nothing
catch E As Exception
Console.WriteLine("There was an error reading the file… " & TextFile)
End Try
Return TextFile
End FunctionPrivate Sub PrintDocument1_PrintPage(ByVal sender as Object, ByVal e As _
System.Drawing.Printing.PrintPageEventArgs) _
Handles PrintDocument1.PrintPage
‘Determine the height of the font
Dim intFontHeight As Integer = New Font(“Arial”,10,FontStyle.Regular, _
GraphicsUnit.Point).GetHeight(e.Graphics)
‘Print the text using the font
e.Graphics.Drawstring(CTypeal(item), String), mfnt, Brushes.Black, 5)
item = item + 1
If item
sorry, i dont understand the way you have implemented this code in visualstudio.net windows form.
Mine is:
Imports System.Drawing.Printing
Imports System.IO
'CREATED BY VISUAL STUDIO
Public Class PrintFrm
Inherits System.Windows.Forms.Form
Did you use a blank textfile
Also Dim din As StreamReader= File.OpenText(TextFile)
should this be Dim din As StreamReader= File.OpenText(c:Test.txt)
I ask this as i see you use
public shared Function Print(TextFile As String) As String
Thanks alot for the help, much appriciated
Adam
Yes, use the fully qualified path name to the Text File by using File.OpenText(c:Test.txt)
1. Call the function Function Print(TextFile As String) As String :
this will read the text into an Array List
2. Start printing assuming that this function handles your printing ;
PrintDocument1_PrintPage(ByVal sender as Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
3. The printing loops through the members fo the array list. Each member of the ArrayList will be one line from your text file.
Print the line and set the HasMorePages property change the line of code to :
If item
Ask A Data Miner - 75,000+ Members
Follow On Twitter
Request More Information
sorry to be a pain
i have changed code to
'///////////////////////////////////////////////////////////////////
' declare the StreamReader, for accessing the file
Dim din As StreamReader = File.OpenText("c:test.txt")
Dim str As String
'Store the string values
Dim al As ArrayList = New ArrayList
'The current string to be printed
Dim item As Integer = 0
'//////////////////////////////////////////////////////////////////////////
Function Print(ByVal TextFile As String) As String
Try
Console.WriteLine("Reading data from a textfile.." & TextFile)
Console.WriteLine()
Do
Str = din.ReadLine()
If str Nothing Then
al.Add(str)
End If
Loop Until str = Nothing
Catch E As Exception
Console.WriteLine("There was an error reading the file… " & TextFile)
End Try
Return TextFile
End Function
'//////////////////////////////////////////////////////////////////////////////////////////////
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
'Determine the height of the font
Dim intFontHeight As Integer = New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point).GetHeight(e.Graphics)
'Print the text using the font
e.Graphics.Drawstring(CTypeal(item), String), mfnt, Brushes.Black, 5)
item = item + 1
If item < al.Count Then
e.HasMorePages = False
Else
e.HasMorePages = True
End If
End Sub
'////////////////////////////////////////
i get errors with
(CTypeal(item), String),
thanks again
adam