Printing tabs

When printing tab delimited text, I would like for there to be aligned columns in the output.

Anyone know of a routine to achieve this?


tabs (0x09) are interpeted by the printing device… so you’d need to know what that is to insure correct spacing.
otherwise use spaces (assuming of course a fixed width font like Courier)

Thanks, Dave.

I was able to get this working, sort of.

Now it looks like I will have to define where a page ends, etc.

Any good printing tutorials out there?

Once upon a lifetime ago Aaron (a form engineer) wrote a “Proper printing for windows” post & a small app
I have the app but cant find the post

[quote=211055:@Michael Brogdon]Now it looks like I will have to define where a page ends, etc.

You could use Graphics.NextPage

But you have to compute how many lines are allowed (or lines * character height; or more complex if you use different sizes in the same line) for the page before issuing NextPage.

Some things are more complex as tey seems at first look.

And for Header and Footers… you have to define their top start / top stop [the top margin) and start printing the standard text below. For Footers, you have to do the same with Bottom Start / Bottom End and stop printing regular lines before Bottom Start.

A drawing in a piece of paper (with rects to symbolize all parts) will greatly help. Something like:

? Top Of Printing Page Area

| | Top margin

| | First line of the standard printable area
| |

Same for the Bottom margin

? Bottom Of Printing Page Area

Add measures too.

Then you will have a good starting point to code.

Note about the Margins (Top / Bottom): I put the code out of the Printing loop into two Methods. This allows serinity while coding; you only have the specialized part in front of your eyes / mind to care for / make changes to if needed. Seems silly ? No, you only have to concentrate yourself on that part.

At last, on OS X you can print to pdf / on WIndows, you can add a Print to PDF driver: that allows you to not waste physical papers.

And yes, lat last, when all is done, you can make some printings using different paper size: US Letter, US Legal, US Legal to simulate A4 if you are outside of the USA; A3 to simulate US Legal if you are in an A4 area.


With help from pretty much everyone who replied (and the Language Reference), I was able to piece together something that is pretty rough, but will work and will give me something to build on.

I’m now placing the raw text into a hidden TextArea which should give me more freedom in the end anyway.

While this doesn’t really address the tab issue, it does address the page count issue. I changed the font to Courier, and though it’s ugly, it seems to align consistently.

Another interesting, and confusing, note is that although I was attempting to pad the columns using spaces (see PrintText method below), once it prints (to PDF), the spaces become what I assume are tabs.

This is what I pieced together from the LR:

[code] Dim stp As StyledTextPrinter
Dim g As Graphics
Dim p As PrinterSetup

p = New PrinterSetup

If p.PageSetupDialog Then
g = OpenPrinterDialog§

If g <> Nil Then
  taHidden.Text = PrintText
  // 72 pixels per inch * 7.5 inches
  Dim textWidth As Integer = 72 * 7.5
  stp = taHidden.StyledTextPrinter(g, textWidth)
  Do until stp.eof
    stp.DrawBlock(p.PageLeft, p.PageTop, 72*10)
    If Not stp.eof then
    End if
End If

End If[/code]

PrintText method:

[code]Dim row, col as Integer
Dim s, tcell as String


s = “”

For col = 0 to (lbDetails.ColumnCount - 1)
s = s + lbDetails.Heading(col)
s = s + PrintPadSpaces(colMax(col) - Len(lbDetails.Heading(col)) + 5)

s = s + CHR(13)

For row = 0 to (lbDetails.ListCount - 1)

For col = 0 to (lbDetails.ColumnCount - 1)
  tcell =  lbDetails.Cell(row, col)
  s = s + tcell
  If col < (lbDetails.ColumnCount - 1) then
    s = s + PrintPadSpaces(colMax(col) - Len(tcell) + 5)
  End If

s = s + CHR(13)


Return s[/code]

How about converting the CSV file into an HTML file? Each row becomes a and each element is surrounded with a . Through some CSS magic, you can control the wrapping and the cell spacing.

I personally would avoid to use padding to print different columns,
but having fields separated by tabs I would print each of them separately, using split to obtain an array and printing each of them at same top position but different left positions.
this has some advantages:

  1. you can use the font you want, and not be forced to use a fixed one

  2. what happens if one of the items is longer of the maximum pad? Printing each field separately allows you to print them in two ( or more ) rows, separately from the others