C#
  Home arrow C# arrow Page 6 - Printing Using C#
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Printing Using C#
By: Wrox Team
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 256
    2003-01-27

    Table of Contents:
  • Printing Using C#
  • Creating a Printing Application
  • Creating the Project
  • Primitives and Elements
  • Pagination and Printing
  • Changing the Page Settings
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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:

    Screenshot


    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();
        }

    More C# Articles
    More By Wrox Team


       · Hi,Great article, but did you miss out the Print() method in the PrintElement...
       · Hi, thanks for this implementation, i've just find out one error, this is the...
       · Thanks for that great article. but I would like to know that how I can add image...
       · Congrats! Exceptional article. I followed and I have implemented a print engine that...
       · Can you please tell me what version of .Net you are using as I cannot see the...
       · I have a created a word doc and now wat i need is i should not see or open the word...
       · Hi Anonymous Loozah,The "PrintBrush" and "PrintFont" methods you mention, are...
       · Hey, I have not used C# in a year ... (i bought C# 2005 with .net3.0 ) .. I really...
       · HiI have an application that is a kiosk app running in full screen. I have...
       · Forget it! It's work as long you have free format on layout, but on preprinted...
       · Forget it!It's works as long you have free format on paper, but if you work...
       · hey .. u r right. it is crossing the right side .... can u suggest a solution
       · I got PrintDialog shown only after setting dialog.UseEXDialog = true;[using 64bit...
       · PrintPrimitiveText::CalculateHeight() is incorrect if the text contains...
       · I would like to center the header or the footer or any text really. How can...
       · everything works, but i cant get the customer details to get printed in the preview....
       · Does anyone know how to print text from the TextBox?i tried and it want work!I...
       · Hi, can some1 help me?! I'm using this engine, but I cannot print anything from the...
       · Could you do the changes for the labeling?If you have some ideas please let me...
       · This is a great article on printing...nice work :)i have a problem relating to...
       · Wow, this is an amazing article. Seven years on and still people are commenting on...
       · If anyone is interested, I made a solution today for the problem of an element that...
     

    C# ARTICLES

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







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 7 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek