0% found this document useful (0 votes)
145 views4 pages

PrintDocument Class (System - Drawing

The PrintDocument class defines a reusable object that sends output to a printer when printing from a Windows Forms application. It allows setting properties like document name and printer settings. The Print method starts the printing process. The PrintPage event is used to specify the output to print using the Graphics object of the PrintPageEventArgs. The example code shows how to print a text file on the default printer by handling the PrintPage event to draw each line of text.

Uploaded by

Arkngl Chel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views4 pages

PrintDocument Class (System - Drawing

The PrintDocument class defines a reusable object that sends output to a printer when printing from a Windows Forms application. It allows setting properties like document name and printer settings. The Print method starts the printing process. The PrintPage event is used to specify the output to print using the Graphics object of the PrintPageEventArgs. The example code shows how to print a text file on the default printer by handling the PrintPage event to draw each line of text.

Uploaded by

Arkngl Chel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

7/1/2015

PrintDocumentClass(System.Drawing.Printing)

PrintDocument Class
.NET Framework 3.5

12 out of 29 rated this helpful

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

Syntax
C#

publicclassPrintDocument:Component

Remarks
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.

Examples
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.
Note:
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.
C#

usingSystem;
usingSystem.IO;
usingSystem.Drawing;
usingSystem.Drawing.Printing;
usingSystem.Windows.Forms;

publicclassPrintingExample:System.Windows.Forms.Form
{
privateSystem.ComponentModel.Containercomponents;
privateSystem.Windows.Forms.ButtonprintButton;
privateFontprintFont;
privateStreamReaderstreamToPrint;
publicPrintingExample():base()
{
http://msdn.microsoft.com/enus/library/vstudio/system.drawing.printing.printdocument%28v=vs.90%29

1/4

7/1/2015

PrintDocumentClass(System.Drawing.Printing)

{
//TheWindowsFormsDesignerrequiresthefollowingcall.
InitializeComponent();
}
//TheClickeventisraisedwhentheuserclicksthePrintbutton.
privatevoidprintButton_Click(objectsender,EventArgse)
{
try
{
streamToPrint=newStreamReader
("C:\\MyDocuments\\MyFile.txt");
try
{
printFont=newFont("Arial",10);
PrintDocumentpd=newPrintDocument();
pd.PrintPage+=newPrintPageEventHandler
(this.pd_PrintPage);
pd.Print();
}
finally
{
streamToPrint.Close();
}
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
}
//ThePrintPageeventisraisedforeachpagetobeprinted.
privatevoidpd_PrintPage(objectsender,PrintPageEventArgsev)
{
floatlinesPerPage=0;
floatyPos=0;
intcount=0;
floatleftMargin=ev.MarginBounds.Left;
floattopMargin=ev.MarginBounds.Top;
stringline=null;
//Calculatethenumberoflinesperpage.
linesPerPage=ev.MarginBounds.Height/
printFont.GetHeight(ev.Graphics);
//Printeachlineofthefile.
while(count<linesPerPage&&
((line=streamToPrint.ReadLine())!=null))
{
yPos=topMargin+(count*
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line,printFont,Brushes.Black,
leftMargin,yPos,newStringFormat());
count++;
}
//Ifmorelinesexist,printanotherpage.
if(line!=null)
ev.HasMorePages=true;
else
ev.HasMorePages=false;
}

//TheWindowsFormsDesignerrequiresthefollowingprocedure.
privatevoidInitializeComponent()
{
this.components=newSystem.ComponentModel.Container();
this.printButton=newSystem.Windows.Forms.Button();
this.ClientSize=newSystem.Drawing.Size(504,381);
http://msdn.microsoft.com/enus/library/vstudio/system.drawing.printing.printdocument%28v=vs.90%29

2/4

7/1/2015

PrintDocumentClass(System.Drawing.Printing)

this.ClientSize=newSystem.Drawing.Size(504,381);
this.Text="PrintExample";
printButton.ImageAlign=
System.Drawing.ContentAlignment.MiddleLeft;
printButton.Location=newSystem.Drawing.Point(32,110);
printButton.FlatStyle=System.Windows.Forms.FlatStyle.Flat;
printButton.TabIndex=0;
printButton.Text="Printthefile.";
printButton.Size=newSystem.Drawing.Size(136,40);
printButton.Click+=newSystem.EventHandler(printButton_Click);
this.Controls.Add(printButton);
}
//Thisisthemainentrypointfortheapplication.
publicstaticvoidMain(string[]args)
{
Application.Run(newPrintingExample());
}
}

Inheritance Hierarchy
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Drawing.Printing.PrintDocument

Thread Safety
Any public static Shared in Visual Basic members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Platforms
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.

Version Information
.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

See Also
Reference

PrintDocument Members
System.Drawing.Printing Namespace
Graphics
PageSettings
PrintController
PrinterSettings

Community Additions
http://msdn.microsoft.com/enus/library/vstudio/system.drawing.printing.printdocument%28v=vs.90%29

3/4

7/1/2015

PrintDocumentClass(System.Drawing.Printing)

Bug in this example. Quick fix.


The sample code will give a blank page, if there are the exact number of lines to fill a page.

Change the check for "line" from null to check for end of stream. The "line" variable will not be null at the end of a full page.

//If more lines exist, print another page


if streamToPrint.EndOfStream == false
e.HasMorePages = true;
else
e.HasMorePages = false;

Shane Stewart
12/14/2012

2015 Microsoft

http://msdn.microsoft.com/enus/library/vstudio/system.drawing.printing.printdocument%28v=vs.90%29

4/4

You might also like