PrintDocument

B

Bobby C. Jones

Ok, so I had a couple of minutes after lunch to play around a bit with the
System.Drawing.Printing namespace. I just wanted to print out a single line
of text. I had to work very hard to resist the urge to print out "Hello
World!" <g>

I got very excited as I pressed the button on my form and the printer
cranked up. It spit out the most beautiful blank white page that you've
ever seen. I'm going to read up and play with it some more this evening,
but does anyone see the problem off hand. Thanks!

private void printAccounts_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
printFont = new Font("Arial", 10);
float yPos = 0;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
yPos = topMargin;
e.Graphics.DrawString("Bobby C. Jones", printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
e.HasMorePages = false;
}
 
M

Michael Mayer

I've been "having fun" trying to get printing to work as well. I
highly recommend this site:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/printwinforms.asp

If you haven't already figured out your problem, give the following a
try. I belive your problem has to do with units, but not really sure.
(The following is kindof a mess, sorry. It's a lot of cut & paste
from a project I was working on).

private void printAccounts_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
Graphics g = e.Graphics;
g.PageUnit = GraphicsUnit.Inch;

// convert to inches
float leftMargin = e.MarginBounds.Left / 100F;
float rightMargin = e.MarginBounds.Right / 100F;
float topMargin = e.MarginBounds.Top / 100F;
float bottomMargin = e.MarginBounds.Bottom / 100F;
float width = e.MarginBounds.Width / 100F;
float height = e.MarginBounds.Height / 100F;
float currentPosition = topMargin;


e.HasMorePages = false;

string headerText = "Hello Bobby C. Jones";
RectangleF headerTextLayout = new RectangleF (
leftMargin, topMargin, width, height );

float headerHeight;
StringFormat headerStringFormat = new
StringFormat(StringFormatFlags.LineLimit);
headerStringFormat.Alignment = StringAlignment.Center;
headerStringFormat.Trimming = StringTrimming.EllipsisWord;

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

headerHeight = g.MeasureString (headerText,
this.HeaderFont, headerTextLayout.Size,
headerStringFormat).Height;
headerTextLayout.Height = headerHeight;

// do the actual drawing
g.FillRectangle(new SolidBrush(Color.AliceBlue),
headerTextLayout);
g.DrawString(headerText, printFont, Brushes.Black,
headerTextLayout, headerStringFormat);

}
 
B

Brendan Duffy

Yor code works fine for me, VS2003 + HP Laserjet2200. Perhaps you should
have a look at the PrintDocument object's PrinterSetting before you call
Print( ) to see if everything is as you expect.

Brendan
 

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