Home arrow C# arrow Page 6 - Printing Using C#

Printing Using C#

In this article Matthew shows us how to build a print engine in C# allowing us to print our application data easily.

Author Info:
By: Wrox Team
Rating: 4 stars4 stars4 stars4 stars4 stars / 371
January 27, 2003
  1. · Printing Using C#
  2. · Creating a Printing Application
  3. · Creating the Project
  4. · Primitives and Elements
  5. · Pagination and Printing
  6. · Changing the Page Settings
  7. · Conclusion

print this article

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

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

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

blog comments powered by Disqus

- Introduction to Objects and Classes in C#, P...
- Visual C#.NET, Part 1: Introduction to Progr...
- C# - An Introduction
- Hotmail Exposed: Access Hotmail using C#
- Razor Sharp C#
- Introduction to Objects and Classes in C#
- Making Your Code CLS Compliant
- Programming with MySQL and .NET Technologies
- Socket Programming in C# - Part II
- Socket Programming in C# - Part I
- Creational Patterns in C#
- Type Conversions
- Creating Custom Delegates and Events in C#
- Inheritance and Polymorphism
- Understanding Properties in C#

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials