C#
  Home arrow C# arrow Page 3 - 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 / 237
    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# - Creating the Project


    (Page 3 of 7 )

    Create a new Visual C# - Windows Application project now and call it Printing. Add these controls to Form1:

    Screenshot


    Here are the controls:
    • trackCustomers - this slider bar should have a minimum value of 0 and a maximum value of 128. Set Value to 1.
    • cmdPrintPreview - the print preview button.
    • cmdPageSettings - the page settings button.
    • cmdPrint - the print button.
    That's all there is to the form. Let's now look at building the rest of the classes.

    "PrintEngine"

    The Framework provides a class called PrintDocument that encapsulates everything related to printing. Those of you who have used MFC, don't worry too much about the word "document". There's nothing related to the "document/view" model you might be thinking of. The "document" is simply something that you want to print.

    To use this class you have two options: either respond to events fired by the class or inherit from the class and build upon the functionality. In this instance, we're going to inherit from it. Create a new class called PrintEngine and add these:

    using statements:
    using System;
    using System.Collections;
    using System.Drawing;
    using System.Drawing.Printing;
    using System.Windows.Forms;


    Next, derive PrintEngine from PrintDocument:

      public class PrintEngine : PrintDocument

    This project is a little "chicken and egg", so we'll be jumping between classes as we try and build up the example. Perhaps the best place to start is maintaining a list of the objects that we want to include on the report.

    As we build the report, we'll add Customer objects to an ArrayList maintained in PrintEngine. Whenever we want to print the report, or display a preview, we'll use methods to add objects that understand how to print themselves to this ArrayList.

    Add this member to PrintEngine:

      public class PrintEngine : PrintDocument
      {
        private ArrayList _printObjects = new ArrayList();


    In a moment, we'll build an interface called IPrintable that an object must support if it wants to be printed by PrintEngine. Add this method to PrintEngine that accepts an object supporting this interface and adds it to _printObjects:

        // AddPrintObject - add a print object the document...
        public void AddPrintObject(IPrintable printObject)
        {
          _printObjects.Add(printObject);
        }


    Now, create a new class called IPrintable and add this code:

    using System;

    namespace Printing
    {
      public interface IPrintable
      {
        // Print
        void Print(PrintElement element);
      }
    }


    As you can see, our interface only has a single method: Print. You'll also notice that it takes a parameter of PrintElement type. We haven't built that yet, but it will represent a single instance of an element on a page. Bear with me until we get to it!

    To complete this circle, create a new object called Customer. Add these members:

      public class Customer : IPrintable
      {
        // members...
        public int Id;
        public String FirstName;
        public String LastName;
        public String Company;
        public String Email;
        public String Phone;
      }


    As we've told Customer that it must support IPrintable, we need to implement the method. However, we won't build any print functionality in just yet. Add this method to Customer:

        // Print...
        public void Print(PrintElement element)
        {
        }


    OK, so we're now at a point where we can create instances of a class called Customer that, because it implements IPrintable, we can use PrintEngine.AddPrintObject to add it to the final document. Let's look now at building up the primitives and then take a look at implementing the elements themselves.

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

    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-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT