Export (0) Print
Expand All
12 out of 29 rated this helpful - Rate this topic

PrintDocument Class

Defines a reusable object that sends output to a printer, when printing from a Windows Forms application.

Namespace:  System.Drawing.Printing
Assembly:  System.Drawing (in System.Drawing.dll)
public class PrintDocument : Component

Typically, you create an instance of the PrintDocument class, set properties such as the DocumentNameand PrinterSettings, and call the Print method to start the printing process. Handle the PrintPage event where you specify the output to print, by using the GraphicsGraphics property of the PrintPageEventArgs.

For more information about printing from a Windows Form application, see the Windows Forms Print Support. If you wish to print from a Windows Presentation Foundation application, see the System.Printing namespace.

The following code example prints the file named C:\My Documents\MyFile.txt on the default printer. To run the example, create a new Windows Forms project and paste the example code into the form, replacing the file contents. For C#, you will need to delete the Form1.Designer.cs file. Also, change the path to the file you want to print.

NoteNote:

The example requires that each line fits within the page width.

Use the System.ComponentModel, System.Windows.Forms, System.Drawing, System.Drawing.Printing, and System.IO namespaces for this example.

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;


 public class PrintingExample : System.Windows.Forms.Form 
 {
     private System.ComponentModel.Container components;
     private System.Windows.Forms.Button printButton;
     private Font printFont;
     private StreamReader streamToPrint;

    public PrintingExample() : base() 
    {
       // The Windows Forms Designer requires the following call.
       InitializeComponent();
    }

    // The Click event is raised when the user clicks the Print button. 
    private void printButton_Click(object sender, EventArgs e) 
    {
       try 
       {
           streamToPrint = new StreamReader
              ("C:\\My Documents\\MyFile.txt");
           try 
           {
              printFont = new Font("Arial", 10);
              PrintDocument pd = new PrintDocument();
              pd.PrintPage += new PrintPageEventHandler
                 (this.pd_PrintPage);
              pd.Print();
           }  
           finally 
           {
              streamToPrint.Close();
           }
       }  
       catch(Exception ex) 
       {
           MessageBox.Show(ex.Message);
       }
    }

    // The PrintPage event is raised for each page to be printed. 
    private void pd_PrintPage(object sender, PrintPageEventArgs ev) 
    {
       float linesPerPage = 0;
       float yPos = 0;
       int count = 0;
       float leftMargin = ev.MarginBounds.Left;
       float topMargin = ev.MarginBounds.Top;
       string line = null;

       // Calculate the number of lines per page.
       linesPerPage = ev.MarginBounds.Height / 
          printFont.GetHeight(ev.Graphics);

       // Print each line of the file. 
       while(count < linesPerPage && 
          ((line=streamToPrint.ReadLine()) != null)) 
       {
          yPos = topMargin + (count * 
             printFont.GetHeight(ev.Graphics));
          ev.Graphics.DrawString(line, printFont, Brushes.Black, 
             leftMargin, yPos, new StringFormat());
          count++;
       }

       // If more lines exist, print another page. 
       if(line != null)
          ev.HasMorePages = true;
       else
          ev.HasMorePages = false;
    }


    // The Windows Forms Designer requires the following procedure. 
    private void InitializeComponent() 
    {
       this.components = new System.ComponentModel.Container();
       this.printButton = new System.Windows.Forms.Button();

       this.ClientSize = new System.Drawing.Size(504, 381);
       this.Text = "Print Example";

       printButton.ImageAlign = 
          System.Drawing.ContentAlignment.MiddleLeft;
       printButton.Location = new System.Drawing.Point(32, 110);
       printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
       printButton.TabIndex = 0;
       printButton.Text = "Print the file.";
       printButton.Size = new System.Drawing.Size(136, 40);
       printButton.Click += new System.EventHandler(printButton_Click);

       this.Controls.Add(printButton);
    }

    // This is the main entry point for the application. 
    public static void Main(string[] args) 
    {
       Application.Run(new PrintingExample());
    }
 }
System.Object
  System.MarshalByRefObject
    System.ComponentModel.Component
      System.Drawing.Printing.PrintDocument
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.