Printing with C#

Shmuelpro

The world is as we preceive it.
Posted by Shmuelpro // Wed, Jan 11, 2006 11:19 AM


I need Help with printing.
In the DO loop the text lowers itself each increment.
it is supposedly simple though I have no clue as how to do so.
thanks,
Shmuel
 



[Code]
private
void PrintReport()

{

PrintDocument pd = new PrintDocument();

PrintDialog pDialog = new PrintDialog();

pDialog.Document = pd;

PrintPreviewDialog prevDialog = new PrintPreviewDialog();

prevDialog.Document = pd;

pd.PrintPage += new PrintPageEventHandler(Inven_Report);

prevDialog.ShowDialog();

if (pDialog.ShowDialog()== DialogResult.OK)

{ pd.Print();}

}

private void Inven_Report(object sender, PrintPageEventArgs e)

{

Graphics g = e.Graphics;

int i = 0;

Font myFont = new Font("Arial", 10);

try

{

do

{

g.DrawString("Sample Output", myFont, Brushes.Black, 10, 10);

i++;

} while (i < 10);

myFont.Dispose();

}

catch (Exception ex)

{

MessageBox.Show(ex.Message.ToString());

}

}
[/Code]

  JohnAskew
  fish or chicken?
 
  Wed, Jan 11 2006 11:47 AM
What does Courier New look like instead of Arial? Same problem?

Most (truetype) Fonts are malicious this way, adding offsets is one way to deal with them. Fixed size fonts (I think that's what they're called) like Courier New may help, I can't remember offhand.


I'd use Google to search for solutions...


EDIT: You didn't post this in Techoff forum?  

  blowdart
  ILOVEBUNNY32=1
 
  Wed, Jan 11 2006 11:49 AM
Why do you think the text lowers itselfs when the DrawString uses the same co-ordinates every time?

g.DrawString("Sample Output", myFont, Brushes.Black, 10, 10);


Surely you should be incrementing the final parameter, which is the y co-ordinate?


  Shmuelpro
  The world is as we preceive it.
 
  Wed, Jan 11 2006 12:13 PM
blowdart wrote:
Why do you think the text lowers itselfs when the DrawString uses the same co-ordinates every time?

g.DrawString("Sample Output", myFont, Brushes.Black, 10, 10);


Surely you should be incrementing the final parameter, which is the y co-ordinate?

oh bloody hell I guess there is no limit to stupidty.
Thanks, you have no idea how stupid i feel now and i did it in front of  everybody.

Thanks again blowdart

  JohnAskew
  fish or chicken?
 
  Wed, Jan 11 2006 12:19 PM

I've never done anything stupid in my life; Shmuel.

Never, ever.



  amotif
  Tell it to the GC.
 
  Wed, Jan 11 2006 11:54 PM
JohnAskew wrote:

I've never done anything stupid in my life; Shmuel.

Never, ever.



Me neither. And if anyone asks, I've never done anything stupid two or three times in a row.





  lorad
  Lorad
 
  Mon, Jan 16 2006 11:07 AM

Replace the do loop with the following. Should work for any font etc. somespacer should be what ever value seems to work for you. There is ways to get the spacing from the font, I will leave that up to you. All this can easily be found looking at the Graphics class.......


int offsetY = 10;
do
{
   string text = "Sample Output";
   g.DrawString(text, myFont,
Brushes.Black, 10, offsetY);
   SizeF s = g.MeasureString(text, myFont);
   offsetY += (
int)s.Height + somespacer;
   i++;
}
while (i < 10);