|
-
Apr 13th, 2008, 12:09 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Text File Column Alignment While Printing
I have got my text file sorted into 5 columns and it looks good

using this code
Code:
Dim sw As New IO.StreamWriter("C:\StickFrameExpress\Jobs\" & Me.lblJobNumber.Text & ".txt", False)
'write blanks lines
sw.WriteLine("")
sw.WriteLine("")
'write job number and date
sw.WriteLine(ControlChars.Tab & "Job Number:" & " " & Me.lblJobNumber.Text & " " & Now)
sw.WriteLine("")
sw.WriteLine("")
sw.WriteLine("")
'header
sw.WriteLine(ControlChars.Tab & "Name".PadRight(20, " "c) & _
ControlChars.Tab & "Material".PadRight(50, " "c) & _
ControlChars.Tab & "Length".PadRight(15, " "c) & _
ControlChars.Tab & "Qty".PadRight(8, " "c) & _
ControlChars.Tab & "Units".PadRight(8, " "c))
sw.WriteLine("")
Dim i As Int16
With Me.ListView1
For i = 0 To CShort(.Items.Count - 1)
If .Items(i).Text <> "" Then
sw.WriteLine(ControlChars.Tab & .Items(i).Text.PadRight(20, " "c) & _
ControlChars.Tab & .Items(i).SubItems(1).Text.PadRight(50, " "c) & _
ControlChars.Tab & .Items(i).SubItems(2).Text.PadRight(15, " "c) & _
ControlChars.Tab & .Items(i).SubItems(3).Text.PadRight(8, " "c) & _
ControlChars.Tab & .Items(i).SubItems(4).Text.PadRight(8, " "c))
sw.writeLine("")
When i print the file using the file > print from the notepad program the columns do not appear as they do on the screen..
They no longer line up.
How can i overcome this?
regards
toe
Last edited by toecutter; Apr 13th, 2008 at 01:01 AM.
Reason: Title
-
Apr 13th, 2008, 09:54 AM
#2
Re: Text File Column Alignment While Printing
I should have said this in the other thread. Don't mix tabs and padding.
I'd switch the font to mono-spaced (Courier) with padding or figure out the correct tab stops, which won't be the same in a textbox and printer.
Courier is pretty common in printers.
-
Apr 14th, 2008, 03:11 AM
#3
Thread Starter
Frenzied Member
Re: Text File Column Alignment While Printing
Am i correct in saying PadRight will move the next string so many spaces to the right?
If so i dont think think this method will work due to when the string increases in chartacters the columns do not line up which seems to be happing no matter how i try to implment it.
Code:
sw.WriteLine("".PadRight(20, " "c) & "Name".PadRight(20, " "c) & _
"Material".PadRight(120, " "c) & _
"Length".PadRight(15, " "c) & _
"Qty".PadRight(8, " "c) & _
"Units".PadRight(8, " "c)) ' & _
sw.WriteLine("")
Dim i As Int16
With Me.ListView1
For i = 0 To CShort(.Items.Count - 1)
If .Items(i).Text <> "" Then
sw.WriteLine("".PadRight(20, " "c) & .Items(i).Text.PadRight(20, " "c) & _
.Items(i).SubItems(1).Text.PadRight(120, " "c) & _
.Items(i).SubItems(2).Text.PadRight(15, " "c) & _
.Items(i).SubItems(3).Text.PadRight(8, " "c) & _
.Items(i).SubItems(4).Text.PadRight(8, " "c)) ' & _
sw.WriteLine("")
End If
Next
End With
-
Apr 14th, 2008, 09:23 AM
#4
Member
Re: Text File Column Alignment While Printing
Code:
Dim aString As String = "1234", eString As String = "END"
Debug.WriteLine("Debug Output Follows")
For padCtr As Integer = 1 To 10
Debug.WriteLine(aString.PadRight(aString.Length + padCtr, "*") & eString)
Next
Debug Output Follows
1234*END
1234**END
1234***END
1234****END
1234*****END
1234******END
1234*******END
1234********END
1234*********END
1234**********END
-
Apr 14th, 2008, 01:35 PM
#5
Re: Text File Column Alignment While Printing
This is the description for String.PadRight(Int32) method from MSDN: Left-aligns the characters in this string, padding with spaces on the right, for a specified total length.
So if you want your data written to a text file to line up in columns, you'd define the size of each column (in term of character count) then use String.PadRight() on each and every field (it does looks like you're already doing this). The next step is to use a fix-width font. Since this is a text file, you simply open it in notepad and change the font to Courrier or Courrier New. You'll see that you columns are lined up nice and neat. Now you're ready to print. However, if the length of your lines are longer than the width of the printable area, some wrapping will occur.
-
Apr 15th, 2008, 03:19 AM
#6
Thread Starter
Frenzied Member
Re: Text File Column Alignment While Printing
I tried what you have said and it still wont line up

As you can see, as the text string increase in characters the columns are moved out of alignment
Last edited by toecutter; Apr 15th, 2008 at 06:26 AM.
-
Apr 15th, 2008, 07:28 AM
#7
Re: Text File Column Alignment While Printing
 Originally Posted by toecutter
I tried what you have said and it still wont line up
As you can see, as the text string increase in characters the columns are moved out of alignment
You are NOT using fixed width fonts, are you?
-
Apr 15th, 2008, 08:04 AM
#8
Member
Re: Text File Column Alignment While Printing
Lucida
Courier New
Test > < ? / (:987654321 Courier
Last edited by ttensabd; Apr 15th, 2008 at 08:16 AM.
-
Apr 15th, 2008, 11:35 AM
#9
Lively Member
Re: Text File Column Alignment While Printing
 Originally Posted by toecutter
I have got my text file sorted into 5 columns and it looks good
When i print the file using the file > print from the notepad program the columns do not appear as they do on the screen..
They no longer line up.
How can i overcome this?
regards
toe
Just an idea : why don' you use RichTextBox control to display your text file,set RTB font to "Courier" and a button to print ... use .NET for that too :
vb.NET Code:
Dim printFont As New Font("Courier New", 9) ..... e.Graphics.DrawString(mytextString, printFont, Brushes.Black, leftMargin, yPos, _ New StringFormat())
Last edited by ionut_y; Apr 15th, 2008 at 11:32 PM.
-
Apr 15th, 2008, 03:51 PM
#10
Thread Starter
Frenzied Member
Re: Text File Column Alignment While Printing
 Originally Posted by stanav
You are NOT using fixed width fonts, are you?
I have set the font of the listview to courier, but it seems thats not what you mean?
When i open the textfile it is set to couier as well...
Code:
What are monospaced fonts you ask? From Xerox:
> Monospace fonts (Such as Courier or LetterGothic), or "fixed pitch" fonts, contain characters that all have the same character width
@ ionut_y, thanks for the idea but ill try and get this to work first and then ill give a RTB a go.
-
Apr 15th, 2008, 05:18 PM
#11
Member
Re: Text File Column Alignment While Printing
You are writing the data to a text file?
if yes, Have you opened it, did a select all, set font to Courier? Then printed?
-
Apr 15th, 2008, 05:46 PM
#12
Thread Starter
Frenzied Member
Re: Text File Column Alignment While Printing
 Originally Posted by ttensabd
You are writing the data to a text file?
if yes, Have you opened it, did a select all, set font to Courier? Then printed?
Yes, i only had to set the font to courier once now all the text is automatically set to courier even when creating a new txt file.
-
Apr 15th, 2008, 06:59 PM
#13
Member
Re: Text File Column Alignment While Printing
The screenshots you have posted aren't courier.
Are the fields lined up in the text file? They should be if it is courier.
-
Apr 15th, 2008, 08:12 PM
#14
Re: Text File Column Alignment While Printing
 Originally Posted by toecutter
Yes, i only had to set the font to courier once now all the text is automatically set to courier even when creating a new txt file.
That's right. Text files do not contains any format information so there's no way you can tell a text file to use a specific font family. The font setting you set in notepad is for the notepad application itself and that's why when you change the font, every other subsequence text files open in notepad will be displayed with that font.
-
May 2nd, 2008, 08:04 PM
#15
Thread Starter
Frenzied Member
Re: Text File Column Alignment While Printing
Thanks all for the help on this but i have found it to difficult and decided to print from a richtextbox with a excellent Link Hack provided in this thread
Code:
Option Explicit On
Option Strict On
Imports System.IO
Imports System.Drawing.Printing
Public Class Form1
Inherits System.Windows.Forms.Form
Dim sw As IO.StreamWriter
Dim sr As IO.StreamReader
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'read a text file into richtextbox
Dim myOutput As StreamReader
Dim sValues As String
myOutput = New StreamReader("C:\StickFrameExpress\Jobs\adadad.txt")
sValues = myOutput.ReadToEnd
Me.RichTextBox2.Text = sValues
myOutput.Close()
End Sub
Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewToolStripMenuItem.Click
Me.PrintDocument1.DefaultPageSettings.Landscape = True
If PrintDialog1.ShowDialog = DialogResult.OK Then
'showDialog method makes the dialog box visible at run time
With Me.PrintDocument1
.PrinterSettings.DefaultPageSettings.Landscape = True
.Print()
End With
End If
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
'PrintPage is the foundational printing event. This event gets fired for every
' page that will be printed
Static intCurrentChar As Int32
' declaring a static variable to hold the position of the last printed char
Dim font As New Font("Courier New", 8)
' initializing the font to be used for printing
Dim PrintAreaHeight, PrintAreaWidth, marginLeft, marginTop As Int32
With PrintDocument1.DefaultPageSettings
.Landscape = True
' initializing local variables that contain the bounds of the printing area rectangle
PrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
PrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
' initializing local variables to hold margin values that will serve
' as the X and Y coordinates for the upper left corner of the printing
' area rectangle.
marginLeft = 20
marginTop = 10
' X and Y coordinate
End With
Me.PrintDocument1.DefaultPageSettings.Landscape = True
If PrintDocument1.DefaultPageSettings.Landscape Then
Dim intTemp As Int32
intTemp = PrintAreaHeight
PrintAreaHeight = PrintAreaWidth
PrintAreaWidth = intTemp
' if the user selects landscape mode, swap the printing area height and width
End If
Dim intLineCount As Int32 = CInt(PrintAreaHeight / font.Height)
' calculating the total number of lines in the document based on the height of
' the printing area and the height of the font
Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, PrintAreaWidth, PrintAreaHeight)
' initializing the rectangle structure that defines the printing area
Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
'instantiating the StringFormat class, which encapsulates text layout information
Dim intLinesFilled, intCharsFitted As Int32
e.Graphics.MeasureString(Mid(RichTextBox2.Text, intCurrentChar + 1), font, New SizeF(PrintAreaWidth, PrintAreaHeight), fmt, intCharsFitted, intLinesFilled)
' calling MeasureString to determine the number of characters that will fit in
' the printing area rectangle
e.Graphics.DrawString(Mid(RichTextBox2.Text, intCurrentChar + 1), font, Brushes.Black, rectPrintingArea, fmt)
' print the text to the page
intCurrentChar += intCharsFitted
Me.Close()
End Sub
End Class
Hope it can help someone else
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|