Library tutorials & articles

Printing Reports in .NET

Report Document classes

Two important classes are introduced in this article.

  • ReportDocument - a class that inherits from PrintDocument and greatly assists in printing tabular reports consisting of data from a DataTable.
  • PrintControl - a control that simplifies the process of guiding the user through the printing process.

The section on the ReportDocument includes many helper classes to define sections, columns, and text styles. The PrintControl section includes a brief description of some of the dialogs used in the printing process.

Latest documentation - NDOC generated documentation.

There are several classes introduced into the ReportPrinting namespace. They work together for the printing of the above report (in addition to all the .NET Framework base classes that are used). Here is a quasi-UML diagram that shows the relationship between these classes. An open triangle is generalization (i.e. it points to the super-class in the inheritance chain). The black diamonds are composite (i.e. shows that one class instantiates members of another class). The dashed-lines are dependency (i.e. it uses the class).

Figure 2 - UML diagram of classes

ReportDocument

ReportDocument extends from PrintDocument and is customized for printing reports from one or more tables of data. A ReportDocument object is the top-level container for all the sections that make up the report. (This consists of a header, body, and footer.)

The ReportDocument's main job is printing, which occurs when the Print() method is called of the base class. The Print() method iterates through all the ReportSections making up the document, printing each one.

The strategy design pattern is employed for formatting the report. An object implementing IReportMaker may be associated with the ReportDocument. This IReportMaker object is application specific and knows how to create a report based on application state and user settings. This object would be responsible for creating sections, associating DataViews, and applying any required styles through use of the TextStyle class. It will generally use the ReportBuilder class to assist with the complexity of building a report.

ReportSection

ReportSection is an abstract class that represents a printable section of a report. There are several subclasses of ReportSection, including ReportSectionText (which represents a string of text) and ReportSectionData (which represents a printable DataView). There are also container sections (which derive from SectionContainer class, which in turn derives from ReportSection). These containers hold child ReportSection objects (also known as subsections) to be printed. Lets take a quick look at how this might work with an example.

In the sample report shown at the top of this article, there is a paragraph of text followed by a table of data. (There are actually two paragraphs of text, one of which is a heading. Plus there is a page header, but well ignore all that for now.) We would create a ReportSectionText object to print the paragraph of text and a ReportSectionData object to print the table of data. To add both of these ReportSections to the ReportDocument, we must create a container. We would create a LinearSections container to hold these two sections. This container is then made the body of the ReportDocument. When the document is printed, the section container will first print the ReportSectionText, and then below that, it will print the ReportSectionData. Simply simply printing each section below the preceding one will result in the finished report. But there are many other ways to set up these classes.

SectionContainer

This abstract class defines a container of sections. There are two types provided with the framework: LinearSections and LayeredSections.

LinearSections

The LinearSections class is a subclass of SectionContainer, which is a subclass of ReportSection. Therefore, the LinearSections can be thought of as "a printable section of a report." However, it is also a container of one or more sections.

As its name implies, it lays sections out linearly -- that is, in a row or in a column. A property named Direction specifies if this container will layout sections going down the page (typical) or across the page (not as typical).

(see Layouts for more information about how this class works.)

LayeredSections

The LayeredSections class is also a subclass of SectionContainer, which is a subclass of ReportSection. Therefore, the LayeredSections can be thought of as "a printable section of a report." It is also a container of one or more sections.

The child sections of a LayeredSections object are all painted on top of one another (creating layers). The first section added to a LayeredSections object is the bottom layer.  Subsequent ReportSection objects added to the LayeredSections object will be shown on top of each other.

(see Layouts for more information about how this class works.)

SectionText

The SectionText prints a string to the page. Two public properties are used to setup this section. Text is used to specify the string to print. TextStyle, described later, sets the font, color, alignment and other properties for the how the text is printed.

It is interesting to note that the string specified for this section can be just one word, or many paragraphs of text.

SectionTable

The SectionTable prints a table of data.  It uses a DataView object (from the .NET System.Data namespace) as the source of data.  It then uses a series of ReportDataColumns to provide the fomatting details.  These ReportDataColumns are similar to the DataGridColumnStyle class. Table wide formatting includes setting Header, Row, and Alternating row TextStyle's, along with setting the width and margins.

ReportDataColumn

The ReportDataColumn provides the necessary information for formatting data for a column of a report. For every column to be presented within a section of data, a new ReportDataColumn object is instantiated and added to the ReportSection. At a minimum, each column describes a source field from the DataSource (that is, a column name from the DataView) and a maximum width on the page.

The ReportDataColumn can be setup with its own unique TextStyle for both header and normal rows. Therefore, each column's data can be formatted differently (e.g. an important column could be bold and red). The TextStyle is also used to set the horizontal alignment (justification).

TextStyle

The TextStyle class allows styles and fonts to be added to text selectively, allowing default styles to be used when not explicitly set. All styles (except for the static TextStyle.Normal) have another style as their "default" style. Until a property is set (like bold, underline, size, font family, etc), a TextStyle object always uses the corresponding value from its default (or parent) style.

For example, a new style can be defined using Normal as its default, but setting bold.

TextStyle paragraphStyle = new TextStyle(TextStyle.Normal);
paragraphStyle.Bold = true;

It will have all the same properties as TextStyle.Normal, except it will be bold. A later change to Normal (such as below) will have the effect of increasing the size of both styles (Normal and paragraphStyle).

TextStyle.Normal.Size += 1.0f

Comments

  1. 24 Aug 2009 at 15:36

    So complicated. Why not use RAQ Report? With it, users can print reports easily.

  2. 02 Jun 2009 at 11:34

    Hi, Is Maria still here ?

  3. 04 Mar 2007 at 22:21
    Fist of all you have to learn how to program, but I think it's better that you learn how to read.

  4. 08 Jul 2006 at 02:52

    Simple, I hope, question - What and how do I add the "Library" to my C# VS2005 project?

    I.m new to C# and VS 2005.  This is my first project.  ReportPrinting appears to be just what I was looking for, but it doesn't run in VS 2005, yet I can execute the ReportDocumentTesting.exe from within the Debug directory.

    Thank you,

    Jim

  5. 27 Jun 2005 at 14:12
    Borland Delphi and Borland C++Builder users often ask us, “Which report generator to choose: either FreeReport or FastReport 2.*, or FastReport 3.*?”, “Why FastReport 2 and FastReport 3 both are available for registration?”

    We are always glad to help you with your choice. Moreover, everything is rather simple – answer some questions and the choice will be obvious.

    So:

    1.    Are you going to create application programs with report generator functions only for Windows? (in that case we advise you to decide only between FreeReport and FastReport 3)
    2.    Do you write free programs, go in for programming as a hobby – for yourself and do not intend (not plan yet) that this occupation will bring you profit? (in that case we advise you to use FreeReport –  a fully free solution, which even now has a more serious functionality than most of commercial report generators for Delphi. And the availability of detailed documentation /that is absolutely unique for free product/ allows us to recommend this solution even to beginners in programming).
    3.    Do you intend to create cross-platform application programs or programs for Linux via using Borland Kyilx development framework? Or do you prefer application suite, which partially Linux-based, and partially MS Windows-based? (In that case FastReport 2.* CLX or FastReport 2.* CLX + FastReport 2.* VCL bundle – remarkable for the full compatibility; moreover, FastReport 2.* VCL supports a great number of capabilities peculiar to MS Windows platform - will suit you.
    4.    Do you use high technology in your work, know XML advantages? Are you interested in multithreaded report building? Do you need the full set of functions that report generator possesses (beginning with turning of the text to any angle and floating of the text from one block to another and ending with export filters precisely set in different formats including such as PDF, HTML, XLS etc. and report servers creation for distributed corporative applications <detailed comparison table is here >? (Then the entire spectrum of FastReport 3 solutions is at your service)

    And some more additions:

    1.    FastReport 3 exists today only in VCL variant (that is why VCL is not pointed out in its name), whereas FastReport 2.5* exists both in VCL and CLX variants – that is why the variant in FastReport 2.5* name is pointed out obligatory.
    2.    There is no need to be afraid of becoming a “solution hostage” after choosing FastReport 2 or FreeReport. Both FastReport 2 and FreeReport contain built-in means for converting reports from FastReport 2 format into FastReport 3 format.
    3.    For all FastReport 2 and FastReport 3 users of earlier delivered variants a privileged transition to FastReport 3 (of any variants of delivering) for variation in prices is foreseen.
    4.    FastScript, which is delivered being included into FastReport 3 Standard and higher, may be used in applications and separately from FastReport 3.
  6. 12 Apr 2005 at 08:31
    • added Delphi 4-7, 2005, C++Builder 4-6 support
    • added FastReport 3 (*.fr3) format support in report designer

      ????


    ????



    FreeReport is a reporting tool component. It consists of report engine, designer and preview. Its capabilities are comparable with that of Crystal Reports. It is written on 100% Object Pascal and can be installed in Delphi 4-7,2005 and C++Builder 4-6.
     This version of FreeReport is based on FastReport ver. 2.32.
     ROYALTY-FREE with FULL source code


    See more here: http://www.fast-report.com

  7. 15 Oct 2004 at 16:24


    Hello,


    By now you probably figured it out, but you have to do something like this:


           dv = dt.DefaultView;
           SectionTable st = builder.AddTable(dv, false, 100.0f);
           st.SuppressHeaderRow = true;
           st.OuterPens = st.InnerPenHeaderBottom = st.InnerPenRow = null;
           builder.AddColumn("Col1", "", 1.5f);
           builder.CurrentColumn.RightPen = null;
           builder.AddColumn("Col2", "", 2.7f);
           builder.CurrentColumn.RightPen = null;


    I've been fighting with this library for the entire day, and I hope it helps you!


     -- LuisR

  8. 28 Jul 2004 at 08:37
    Hi Mike

    Thanx for the great stuff you gave us for free. It saved me centuries of my time.
    I hope you still read this coz I have a small problem. Please tel me how to remove grid when I print dataview.

    Regards
    Al
  9. 19 Jul 2004 at 02:17

    Some information on FastReport 3:
    - Rewritten class architecture.
    - Storing reports in XML format.
    - Full WYSIWYG (now for text objects too).
    - Text rotation 0..360 degrees.
    - Memo object supports simple html-tags (font color, b, i, u, sub, sup).
    - Styles, Text flow.
    - URLs, Anchors.
    - Dot matrix reports.
    - One script in the report (like an unit in the Delphi), Multi-language architecture allows you to use many languages
    (Pascal, C++, Basic, Java).
    - Access to any object inside your application (in case you've allowed this). Standard libraries to access to base classes,
    controls and forms. Easily expandable library architecture.
    - Debugger, Improved Object Inspector.
    - Zooming, Rulers, guides.
    - Wizard for base type reports.
    - Full Undo/Redo.


    Read more: http://fast-report.com/en/fr3.php


    Features comparison (compare FastReport 3 with the current version of FastReport):
    http://fast-report.com/en/comparison.php


    Price-list: http://fast-report.com/en/upgrade.php


    Download FastReport 3: http://www.fast-report.com/en/download.php

  10. 23 Jun 2004 at 01:05

    Hello all,


    I have a little .NET vs. VS 6 user control curiosity!  ^_^  any takers?  


    If I have a project that contains a user control that I made in VS6 and then want to convert it to VS.NET, can I still call the component /dll from within a test application that was created mainly in VB6?  Does anyone have exerience with the conversion of user controls - because when the project with the user control is converted to .Net, the user control code is totally gone in the .Net version of the project!


    Well a little insight would help me a long way!  
    Thanks guys,


    Maria

  11. 01 Jan 1999 at 00:00

    This thread is for discussions of Printing Reports in .NET.

Leave a comment

Sign in or Join us (it's free).

Mike Mayer Mike Mayer is a Microsoft MVP in Visual C#.

Related podcasts

  • A Practical Look at Silverlight 2 Part 1

    Now that Silverlight 2 is at the Olympics and making a big splash, we wanted to explore this fascinating technology more. Microsoft Silverlight 2 is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive ap...

Events coming up

  • Dec 9

    GL.net Group Meeting - December 2009

    Gloucester, United Kingdom

    The beginning of this year holiday season will belong to mocks. Ronnie and Stephen will take us for a tour around exciting world of unit testing.

Want to stay in touch with what's going on? Follow us on twitter!