CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Printing from winforms..

    I've been looking at various forums and MSDN articles for a while now, but I can't seem to get my head around how to print something from my winforms app...

    I have a form with a button called "print" that shows the standard PrintDialog... The problem is that I can't figure out how the PrintDocument thing works... I have a bunch of strings that I want to place in a specific place on the page, but how do I even use this thing?

    If someone could give me a quick knock on the head and tell me how this works, I'd be very grateful
    It's not a bug, it's a feature!

  2. #2
    Join Date
    Aug 2005
    Location
    Seattle, Wa
    Posts
    179

    Re: Printing from winforms..

    Here's a printing snippet I use in some of my code:

    In some Print button, do the following:
    Code:
    //Setup and print the document
    PrintDocument document = new PrintDocument();
    PageSettings pgSettings = new PageSettings();
    
    document.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
    
    document.DefaultPageSettings = pgSettings;
    PrintDialog dlg = new PrintDialog();
    dlg.Document = document;
    if (dlg.ShowDialog() == DialogResult.OK) 
    {
        document.Print();
    }

    Then create a print event where you "draw" your print output:
    Code:
    private void pd_PrintPage(object sender, PrintPageEventArgs e)
    {
        //Print finished bitmap
        Point point = new Point(0, 30);
        e.Graphics.DrawImageUnscaled(PrintReadyBitmap, point);
    }
    In my case, im printing a bitmap.
    You may also consider using a webbrowser control to construct your document in HTML, and then calling the browsers print methods.

    -CSixx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured