Files
docs-desktop/dotnet-desktop-guide/framework/winforms/advanced/how-to-create-standard-windows-forms-print-jobs.md
T
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

3.6 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
Create Standard Print Jobs 03/30/2017
csharp
vb
cpp
printing [Windows Forms]
printing [Windows Forms], creating print jobs
printing [Visual Basic], in Windows applications
03342b90-9cfe-40b2-838b-b479a13c5dea

How to: Create Standard Windows Forms Print Jobs

The foundation of printing in Windows Forms is the xref:System.Drawing.Printing.PrintDocument component—more specifically, the xref:System.Drawing.Printing.PrintDocument.PrintPage event. By writing code to handle the xref:System.Drawing.Printing.PrintDocument.PrintPage event, you can specify what to print and how to print it.

To create a print job

  1. Add a xref:System.Drawing.Printing.PrintDocument component to your form.

  2. Write code to handle the xref:System.Drawing.Printing.PrintDocument.PrintPage event.

    You will have to code your own printing logic. Additionally, you will have to specify the material to be printed.

    In the following code example, a sample graphic in the shape of a red rectangle is created in the xref:System.Drawing.Printing.PrintDocument.PrintPage event handler to act as material to be printed.

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage  
       e.Graphics.FillRectangle(Brushes.Red, New Rectangle(500, 500, 500, 500))  
    End Sub  
    
    private void printDocument1_PrintPage(object sender,
    System.Drawing.Printing.PrintPageEventArgs e)  
    {  
       e.Graphics.FillRectangle(Brushes.Red,
         new Rectangle(500, 500, 500, 500));  
    }  
    
    private:  
       void printDocument1_PrintPage(System::Object ^ sender,  
          System::Drawing::Printing::PrintPageEventArgs ^ e)  
       {  
          e->Graphics->FillRectangle(Brushes::Red,  
             Rectangle(500, 500, 500, 500));  
       }  
    

    (Visual C# and Visual C++) Place the following code in the form's constructor to register the event handler.

    this.printDocument1.PrintPage += new  
       System.Drawing.Printing.PrintPageEventHandler  
       (this.printDocument1_PrintPage);  
    
    printDocument1->PrintPage += gcnew  
       System::Drawing::Printing::PrintPageEventHandler  
       (this, &Form1::printDocument1_PrintPage);  
    

    You may also want to write code for the xref:System.Drawing.Printing.PrintDocument.BeginPrint and xref:System.Drawing.Printing.PrintDocument.EndPrint events, perhaps including an integer representing the total number of pages to print that is decremented as each page prints.

    Note

    You can add a xref:System.Windows.Forms.PrintDialog component to your form to provide a clean and efficient user interface (UI) to your users. Setting the xref:System.Windows.Forms.PrintDialog.Document%2A property of the xref:System.Windows.Forms.PrintDialog component enables you to set properties related to the print document you are working with on your form. For more information about the xref:System.Windows.Forms.PrintDialog component, see PrintDialog Component.

    For more information about the specifics of Windows Forms print jobs, including how to create a print job programmatically, see xref:System.Drawing.Printing.PrintPageEventArgs.

See also