|
Common dialog boxes for printing
The C# Column - Yashawant Kanetkar
In
this article we will see how to print a multi-page document using
common dialog boxes of printing such as PrintPreview, Print and
PageSetup. For these dialog boxes .NET has provided the PrintPreviewDialog
control, the PrintDialog control and the PageSetupDialog
control.
To illustrate the use of these
dialog boxes let us create a Windows Form application called prntdlg.
We also added a widely used OpenFileDialog control to the
form along with two textboxes—filename and mytext-and
five buttons—a ‘Browse’ button named browse, an ‘Open’
button named open, a ‘PageSetup’ button named pgsetup,
a ‘Print Preview’ button named preview, and a ‘Print’ button
named print. Next we added five components to the component
tray and changed their names as shown below:
| Component |
Name |
| PrintDocument |
mypdoc |
| PageSetupDialog |
mypgsdlg |
| PrintPreviewDialog |
mypreviewdlg |
| PrintDialog |
myprintdlg |
| OpenFileDialog |
myopendlg |
The UI of the prntdlg
application is shown in the following figure.

Next we have added Click
event handlers for all the five buttons. The handler for the browse
button is given below.
private void browse_Click (object
sender,
System.EventArgs e)
{
myopendlg.Filter = "Text Files ( *.txt ) | *.txt" ;
myopendlg.ShowDialog( ) ;
if (myopendlg.FileName != "" )
filename.Text = myopendlg.FileName ;
}
The ‘Browse’ button lets the
user select a file for printing. The user selects the file from
the standard Open File dialog box. We have displayed the dialog
box in this handler. The selected file name is displayed in the
text box.
As soon as the user clicks
the ‘Open’ button the following handler gets called:
private void open_Click ( object
sender,
System.EventArgs e )
{
StreamReader reader = new StreamReader
( filename.Text ) ;
mytext.Text = reader.ReadToEnd( ) ;
reader.Close( ) ;
}
Here we have read the file
contents with the help of an object of the StreamReader class
and displayed them on the mytext textbox. Now if the user
wants to change the page settings he needs to click the ‘Page Settings’
button. On doing so the following handler gets called:
private void pgsetup_Click ( object
sender,
System.EventArgs e )
{
try
{
mypgsdlg.PageSettings = ps ;
mypgsdlg.ShowDialog( ) ;
}
catch ( Exception ex )
{
MessageBox.Show ( ex.Message ) ;
}
}
We have added an object referred
to by ps of the PageSettings class as a data member
of the form class as follows:
PageSettings ps = new PageSettings(
) ;
The zero-argument constructor
of this class creates an object of the PageSetting class
for the default printer. The default constructor initialises all
fields to their default values. Next we have set the PageSettings
property of the PageSetupDialog class to ps. Then we have
displayed the Page Setup Dialog box using the ShowDialog( )
method.
The user can also preview the
page to be printed by clicking on the ‘Print Preview’ button. On
doing so the following handler gets called:
private void preview_Click ( object
sender,
System.EventArgs e )
{
try
{
str = mytext.Text ;
mypdoc.DefaultPageSettings = ps ;
mypreviewdlg.Document = mypdoc ;
mypreviewdlg.ShowDialog( ) ;
}
catch ( Exception ex )
{
MessageBox.Show ( ex.Message ) ;
}
}
Here first we have collected
the string in mytext textbox in str. We have added
str of type string as a data member of the form class. Next
we set the DefaultPageSettings property of the PrintDocument
class to ps. Then we set the Document property
of the PrintPreviewDialog class to mypdoc, which specifies
the document to be previewed. Next we have displayed the dialog
using the ShowDialog( ) method. The PrintPreviewDialog
is shown in the following figure:

On clicking the ‘Print’ button
the following handler gets called.
private void print_Click ( object
sender,
System.EventArgs e )
{
try
{
mypdoc.DefaultPageSettings = ps ;
str = mytext.Text ;
myprintdlg.Document = mypdoc ;
if (myprintdlg.ShowDialog( ) == DialogResult.OK)
mypdoc.Print( ) ;
}
catch ( Exception ex )
{
MessageBox.Show ( ex.Message ) ;
}
}
Here we have first set the
DefaultPageSettings property of the PrintDocument class
to ps. Next we have collected the text of mytext textbox,
which is to be printed in str. The document to be printed
is then set to the Document property of the PrintDialog
class. Next we displayed the PrintDialog box using the ShowDialog(
) method and if the result happens to be DialogResult.OK,
i.e if the user clicks OK, we call the Print( ) method
of the PrintDocument class.
On calling the Print( )
method the following handlers get invoked:
private void mypdoc_BeginPrint (
object sender,
System.Drawing.Printing.PrintEventArgs e )
{
f = new Font ( "Arial", 12 ) ;
b = new SolidBrush ( Color.Black ) ;
strformat.Trimming = StringTrimming.Word ;
}
private void mypdoc_PrintPage ( object sender,
System.Drawing.Printing.PrintPageEventArgs e )
{
RectangleF myrect = new RectangleF
(e.MarginBounds.Left, e.MarginBounds.Top,
e.MarginBounds.Width, e.MarginBounds.Height ) ;
SizeF sz = new SizeF ( e.MarginBounds.Width,
e.MarginBounds.Height - f.GetHeight(e.Graphics ) ) ;
e.Graphics.MeasureString ( str, f, sz, strformat,
out chars, out lines ) ;
printstr = str.Substring ( 0, chars ) ;
e.Graphics.DrawString ( printstr, f, b, myrect,
strformat ) ;
if ( str.Length > chars )
{
str = str.Substring ( chars ) ;
e.HasMorePages = true ;
}
else
e.HasMorePages = false ;
}
The code written in this handler
takes care of multiple pages being printed as well as word wrapping.
 |
Yashavant Kanetkar, one of the first
Express Computer columnists, is an established software expert,
speaker and author with several best-sellers to his credit,
including titles like “Let Us C” and the “Fundas” series. Contact
him at kanet@nagpur.dot.net.in |
|