C#
  Home arrow C# arrow Page 2 - 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 / 240
    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 a Printing Application


    (Page 2 of 7 )

    Part of the difficulty of printing from an application is that we lose the concept of using controls to present our program's output to the user. To clarify, with a VB.NET or C# application, if we're building a UI we usually do so by painting controls on the screen and allowing whoever developed them to worry about the whys and wherefores about how that control should look. We don't really care how many individual DrawLine calls it takes to draw a button on the screen.

    With printing, you lose this control-oriented abstraction and you have to get back to worry about where to draw lines, selecting fonts, drawing text and so on. This makes life difficult for developers maintaining and enhancing the application.

    Imagine you have an application that prints simple reports and in this application you have a class called Customer that represents a customer. As part of the report, you want to print all of the customer's details. What the developer wants is a really easy to use framework where she can just say, "Now print the customer's name" or "Print the URL of the customer's home page". She doesn't want to have to worry about where exactly on the page to position the text, or which font to use, just like as a UI developer using controls you don't care about how to find out the default color for the button.

    In this article we're going to build a fairly powerful printing framework that can be easily enhanced at a later date. We're going to provide the developer with such elements as print previews, printer settings and we're also going to take over all the hard work of pagination, adding headers and footers and so on. (For the uninitiated, "pagination" is the process of splitting up a document into pages and laying out each page so that it matches the layout that the user intended.)

    Objects, Elements and Primitives

    Let's assume that our application has access to objects that describe things, such as Customer, Company, Order and Product. Let's also assume that our reports are going to be fairly simple lists of these object, e.g. "Give me a list of all customers who have ordered from me today."

    Our report might look like this:

    Screenshot


    One way to break up this task is to say that we're going to build a page up of different "elements". In the above screenshot, we have three elements:

    Screenshot


    The elements we have, "Header", "Customer" and "Footer", are all comprised of different "primitives." In this case, we have two kinds of primitives: "text" and "horizontal rule".

    So, to build up our application we first of all need an object that describes the print functionality. We'll call this PrintEngine and this object will be responsible for laying out the elements on the page and choosing when new pages need to be added to the report. Each element will be represented by a PrintElement object. This PrintElement object will know how to contain primitives, specifically PrintPrimitiveText and PrintPrimitiveRule objects.

    It will be these objects that know how to draw themselves on the page and report back to PrintEngine as to their size. (In order for PrintEngine to lay out the elements, it needs to know how large each element is. To determine an elements size, the primitives have to be able to report on their size.)

    Now that we know what we're trying to achieve, let's look at building an application capable of printing. We'll build the printing components of this application in such a way that we could extract them and reuse them in another project.

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

    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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek