PrintDocument

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bobby C. Jones

    #1

    PrintDocument

    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_P rintPage(object sender,
    System.Drawing. Printing.PrintP ageEventArgs e)
    {
    printFont = new Font("Arial", 10);
    float yPos = 0;
    float leftMargin = e.MarginBounds. Left;
    float topMargin = e.MarginBounds. Top;
    yPos = topMargin;
    e.Graphics.Draw String("Bobby C. Jones", printFont, Brushes.Black,
    leftMargin, yPos, new StringFormat()) ;
    e.HasMorePages = false;
    }
    --
    Bobby C. Jones



  • Michael Mayer

    #2
    Re: PrintDocument

    I've been "having fun" trying to get printing to work as well. I
    highly recommend this site:
    http://msdn.microsoft.com/library/de...ntwinforms.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_P rintPage(object sender,
    System.Drawing. Printing.PrintP ageEventArgs e)
    {
    Graphics g = e.Graphics;
    g.PageUnit = GraphicsUnit.In ch;

    // 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 headerTextLayou t = new RectangleF (
    leftMargin, topMargin, width, height );

    float headerHeight;
    StringFormat headerStringFor mat = new
    StringFormat(St ringFormatFlags .LineLimit);
    headerStringFor mat.Alignment = StringAlignment .Center;
    headerStringFor mat.Trimming = StringTrimming. EllipsisWord;

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

    headerHeight = g.MeasureString (headerText,
    this.HeaderFont , headerTextLayou t.Size,
    headerStringFor mat).Height;
    headerTextLayou t.Height = headerHeight;

    // do the actual drawing
    g.FillRectangle (new SolidBrush(Colo r.AliceBlue),
    headerTextLayou t);
    g.DrawString(he aderText, printFont, Brushes.Black,
    headerTextLayou t, headerStringFor mat);

    }


    "Bobby C. Jones" <bobbyj@acadx.c om> wrote in message
    news:OdtReniUDH A.964@TK2MSFTNG P09.phx.gbl...[color=blue]
    > Ok, so I had a couple of minutes after lunch to play around a bit[/color]
    with the[color=blue]
    > System.Drawing. Printing namespace. I just wanted to print out a[/color]
    single line[color=blue]
    > of text. I had to work very hard to resist the urge to print out[/color]
    "Hello[color=blue]
    > World!" <g>[/color]


    Comment

    • Brendan Duffy

      #3
      Re: PrintDocument

      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


      Comment

      Working...