Printing Using C# - Changing the Page Settings
(Page 6 of 7 )
When printing, you might want to change some of the page settings. You might want to use a different page size or a landscape orientation.
The Framework provides a class that lets you change the layout of the page. The PrintDocument class has a property called DefaultPageSettings. This contains the current settings for the page, and when the object is created this is populated with the default page settings for the default printer.
Add this method to PrintEngine:
// ShowPageSettings - let's us change the page settings...
public void ShowPageSettings()
{
PageSetupDialog setup = new PageSetupDialog();
PageSettings settings = DefaultPageSettings;
setup.PageSettings = settings;
// display the dialog and,
if(setup.ShowDialog() == DialogResult.OK)
DefaultPageSettings = setup.PageSettings;
} This method will take the current page settings from the dialog and invite the user to change them. If the user presses OK on the dialog, the changes settings are saved.
Add this event handler to Form1 to finish this off:
private void cmdPageSettings_Click(object sender, System.EventArgs e)
{
// show the page settings...
_engine.ShowPageSettings();
} If you try this, remember that if you change the slider you'll lose the page settings. (i.e. if you start the application check the print preview, close it down, change the page settings, change the slider and then view the print preview, the page settings will not be saved as a new instance of PrintEngine would have been created.) You should see something like this:
Printing Without the Preview To print to another printer, you have to use the standard print dialog. We can bring this up in much the same way, so add this method to PrintEngine:
// ShowPrintDialog - display the print dialog...
public void ShowPrintDialog()
{
// create and show...
PrintDialog dialog = new PrintDialog();
dialog.PrinterSettings = PrinterSettings;
dialog.Document = this;
if(dialog.ShowDialog() == DialogResult.OK)
{
// save the changes...
PrinterSettings = dialog.PrinterSettings;
// do the printing...
Print();
}
} Again, if the user clicks OK we save the settings, but this time we also call Print to print directly to the configured printer. Add this event handler to Form1 to finish this off:
private void cmdPrint_Click(object sender, System.EventArgs e)
{
// print...
_engine.ShowPrintDialog();
}Next: Conclusion >>
More C# Articles
More By Wrox Team
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|