Asking for help for printing string with fixed format.

G

Guest

Hi

I am trying to print table format text out in C#. The approach I use is to format the string varaible a
temp=string.Format("{0,-10}{1,-8}{2,-12}",id,name,cost)
Then I keep adding values by useing temp=temp+"\n"+string.fromat...
At the end, I put the string variable to a memorystream and send it to printer
The problem is that it looks ok if I use Console.WriteLine to print out hte string in console windows, all aligned as I expected, but it does not align well after out to printer. I tried padright with '-' to see what is going on, it seems that each character has different width and it is handled nicely by WriteLine in console, but not working while sending to printer, so I could get something lik

ID Name Cos
1 Chris 1.
12 Jason 2.

I looked through the newsgroup but I can not find any solution for it

Could anyone here please help me out on this
Thanks a loooooooo

CHri

MemoryStream ms = new MemoryStream((new System.Text.ASCIIEncoding()).GetBytes(stringToPrint))



StreamReader streamToPrint = new StreamReader(ms)
try

//Assumes the default printe
TextFilePrintDocument pd = new TextFilePrintDocument(streamToPrint)

PrintDialog dlg = new PrintDialog()
dlg.Document = pd
DialogResult result = dlg.ShowDialog()

if (result == DialogResult.OK)

pd.Print()
 
N

Nicholas Paldino [.NET/C# MVP]

Chris,

The reason this works is that the console screen uses a fixed-width
font, so using spaces to align your values will work. However, on a
printer, the font might be something different. You have to have your
TextFilePrintDocument class set the font for the printing correctly (or set
it on the print dialog that comes up).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

chrisben said:
Hi,

I am trying to print table format text out in C#. The approach I use is to format the string varaible as
temp=string.Format("{0,-10}{1,-8}{2,-12}",id,name,cost);
Then I keep adding values by useing temp=temp+"\n"+string.fromat....
At the end, I put the string variable to a memorystream and send it to printer.
The problem is that it looks ok if I use Console.WriteLine to print out
hte string in console windows, all aligned as I expected, but it does not
align well after out to printer. I tried padright with '-' to see what is
going on, it seems that each character has different width and it is handled
nicely by WriteLine in console, but not working while sending to printer, so
I could get something like
 
P

Philip Rieck

You should make sure you're using a fixed-width font, such as Courier.

It seems that you are using the TextFilePrintDocument from the GotDotNet
samples, if so, you can modify the OnBeginPrint method where it states:

printFont = new Font("Arial", 10)

to:

printFont = new Font("Courier", 10)


If you're doing the printing yourself, make sure to pass in a fixed-width
font to the graphics.DrawString() method that you are using to write each
string.

chrisben said:
Hi,

I am trying to print table format text out in C#. The approach I use is to format the string varaible as
temp=string.Format("{0,-10}{1,-8}{2,-12}",id,name,cost);
Then I keep adding values by useing temp=temp+"\n"+string.fromat....
At the end, I put the string variable to a memorystream and send it to printer.
The problem is that it looks ok if I use Console.WriteLine to print out
hte string in console windows, all aligned as I expected, but it does not
align well after out to printer. I tried padright with '-' to see what is
going on, it seems that each character has different width and it is handled
nicely by WriteLine in console, but not working while sending to printer, so
I could get something like
 
N

Nicholas Paldino [.NET/C# MVP]

Chris,

The TextFilePrintDocument is not in the framework (at least I can't find
it in the documentation). Is it a third-party class or your own?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

chrisben said:
Thanks Nicholas. Do you know how to define fixed-width font in
TextFilePrintDocument? Not quite sure how to do it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top