Click here to Skip to main content
15,899,000 members
Articles / Programming Languages / C#

DaReport PrintDocument - A Printing Utility for .NET

Rate me:
Please Sign up or sign in to vote.
4.80/5 (65 votes)
7 Jun 2018CPOL2 min read 454K   6K   168   155
A printing utility for .NET

Screenshot

Introduction

This component helps you generate simple but useful reports for printing in .NET. The idea is to generate XML-based templates, and then to call them from your program, and eventually to fill them with the data required. This is appropriate for generating small and simple printing documents.

Background

The basic class of this project is DaPrintDocument. It inherits from the standard .NET class PrintDocument. Therefore, you can use it pretty much the same way as the PrintDocument class.

This class simply paints attached drawing objects within its PrintPage() method. For simplicity, there are only four types of drawing objects supported: TextField (simple formatted text), PictureBox (images), ChartBox (simple charts), and StyledTable (table with static or dynamically loaded data).

Template File Overview

DaPrintDocument makes use of template files written in XML syntax. Those provide all static information relevant for printing a page.

You can write template files in any text editor, or alternatively, use the DaReport Designer (beta) provided in the distribution (it's a separate application: DaReportDesigner.exe).

The brief overview of the XML structure:

XML
<daReport>

<parameters>
   <parameter name="parameter1_name" />
   <parameter name="parameter2_name" />
</parameters>

<content>

<!-- Static objects are evaluated once per report -->
<staticContent>

      <!-- The list of static objects goes here. 
         Check in reportStatic.xml (in download package). -->
     
</staticContent>

<!-- Dynamic objects are evaluated once per page of report 
      (like data table segment or page number) -->
<dynamicContent>

      <!-- The list of dynamic objects goes here. 
        Check in reportDynamic.xml (in download package). -->

</dynamicContent>

</content>
</daReport>

Once again, the preferred way for XML template file generation is by using the DaReport Designer (beta) provided in the distribution.

Using the Code

First, add a reference to the daReport.dll file (provided in the distribution) to your project. If you feel like it, you can also put this component in your controls toolbar.

Second, add an instance of DaPrintDocument to your module (form); if you have previously attached it in your controls toolbar, simply drag it to your form:

C#
private daReport.DaPrintDocument daPrintDocument = 
                         new daReport.DaPrintDocument();

At the point where you wish to print some report, set the template file and fill in the parameters (if any):

C#
// set .xml file for printing
daPrintDocument.setXML("reportStatic.xml");

// fill in declared parameters (if any)
// (parameter names are case sensitive)
Hashtable parameters = new Hashtable();
parameters.Add("author","Predrag Dukanac");
daPrintDocument.SetParameters(parameters);

Using Tables

  • Static tables: Static tables have data defined in an XML template file (DATA tag). The DataSource property for these tables is not set. No specific action is required in code for these tables to print correctly (everything is done in the .xml file).
  • Dynamic tables: Dynamic tables present data calculated in your program (like database reports). The DataSource property is set, and must correspond to the DataTable.Name property in your code. Do it like this:
    C#
    // myTable is DataTable previously filled from database
    // (using DataAdapter.Fill method, for instance)
    
    DataTable printTable = myTable.Copy();
    printTable.Name = "printTable";
    // this is the DataSource attribute of my table in XML file
    
    // attach DataTable to printing document
    daPrintDocument.AddData(printTable);
    
    // now you're ready for print

Take a look at reportStatic.xml, reportDynamic.xml, reportCharts.xml, and the ReportTest project (all in the download package) for a deeper insight.

Latest Update

The DaReport print engine has turned into a commercial product - Stampa Reports System®. A Stampa Reports demo is available for download here. Stampa designer features backward compatibility with daReport in design mode. The Stampa printing engine can be used free of charge.

The DaReport project at this address is still an open-source project by all means.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Serbia Serbia
Working at Mihajlo Pupin Institute, Belgrade, Serbia.

Comments and Discussions

 
GeneralRe: Project now on SourceForge Pin
Predrag Dukanac17-May-05 2:25
Predrag Dukanac17-May-05 2:25 
GeneralRe: Project now on SourceForge Pin
angus_grant17-May-05 10:51
angus_grant17-May-05 10:51 
GeneralRe: Project now on SourceForge Pin
Predrag Dukanac17-May-05 20:01
Predrag Dukanac17-May-05 20:01 
GeneralRe: Project now on SourceForge Pin
angus_grant17-May-05 21:40
angus_grant17-May-05 21:40 
Questionwhere is the designer source code? Pin
Jorge Orellana12-May-05 10:38
Jorge Orellana12-May-05 10:38 
AnswerRe: where is the designer source code? Pin
Predrag Dukanac12-May-05 20:05
Predrag Dukanac12-May-05 20:05 
GeneralRich text box possibility Pin
DaaX12-May-05 3:02
DaaX12-May-05 3:02 
GeneralRe: Rich text box possibility Pin
Predrag Dukanac13-May-05 0:35
Predrag Dukanac13-May-05 0:35 
Hi, there.

My general guideline during development was to keep the component as simple as it can be.
Due to this I made only simple string painting in paint methods.Therefore, only simple(readable) strings are passed to ICustomPaint classes.

Formatted text contains much more information about itself (font, color, backcolor). According to this, one must have good parsing mechanism to resolve all that stuff. I beleive some parsers are available for free (HTML, RTF parsers) and might be somehow incorporated in painting classes. To be frank, I haven't tried this before. Yet, there is a problem on how to persist this formatted text in .xml file. It's error prone because
both HTML and RTF use angle brackets (<>) for tags, just like XML does. Unfortunatelly, XML syntax is strict on tags (yet RTF and HTML are not), which brings the danger of XML file becoming corrupted in its structure and useless (don't forget, xml tags should wrap HTML text). Moreover, implementing those standard markup language would impose the obligation to stay up to date with new versions.

Things may be solved using painting contexts, like GDI does. There would be a RichTextBox component associated with potential context-objects which would store information about font,color,etc. All you would have to do in printing text is to define context switching points , preferably with some special sign (like $C{n} ), where n would be the ordinal number of defined context to use in painting. Parsing text from left would have a mechanism for changing painting context for text to come after switching point. If none provided, default context is to be used. I would really like to hear some ideas on this.

As for the currency formating, do it in your code... use the proper ToString() function. Moreover, if your values are read from database, use database formating mechanisms. Modern relationl databases have powerful number->currency->string transformation functions.

All the best,
Predrag Dukanac
GeneralRe: Rich text box possibility Pin
angus_grant8-Jul-05 12:02
angus_grant8-Jul-05 12:02 
GeneralDesigner source code Pin
NicolasDepauw12-May-05 3:01
sussNicolasDepauw12-May-05 3:01 
GeneralNew source code Pin
Predrag Dukanac8-May-05 20:12
Predrag Dukanac8-May-05 20:12 
GeneralRe: New source code Pin
Yalcin Ozturk9-May-05 0:45
Yalcin Ozturk9-May-05 0:45 
Generala New Designer Pin
Yalcin Ozturk25-Apr-05 22:40
Yalcin Ozturk25-Apr-05 22:40 
GeneralRe: a New Designer Pin
Predrag Dukanac9-May-05 0:23
Predrag Dukanac9-May-05 0:23 
GeneralPatches to allow alignment the cell data in a StyledTable Pin
Learning .NET24-Apr-05 7:11
Learning .NET24-Apr-05 7:11 
GeneralRe: Patches to allow alignment the cell data in a StyledTable Pin
Predrag Dukanac24-Apr-05 20:55
Predrag Dukanac24-Apr-05 20:55 
GeneralRe: Patches to allow alignment the cell data in a StyledTable Pin
Learning .NET25-Apr-05 1:57
Learning .NET25-Apr-05 1:57 
GeneralRe: Patches to allow alignment the cell data in a StyledTable Pin
Yalcin Ozturk25-Apr-05 19:07
Yalcin Ozturk25-Apr-05 19:07 
GeneralA Great Chart Engine Example for printing Pin
Yalcin Ozturk20-Apr-05 19:37
Yalcin Ozturk20-Apr-05 19:37 
GeneralRe: A Great Chart Engine Example for printing Pin
Learning .NET21-Apr-05 10:42
Learning .NET21-Apr-05 10:42 
GeneralRe: A Great Chart Engine Example for printing Pin
Predrag Dukanac21-Apr-05 20:30
Predrag Dukanac21-Apr-05 20:30 
GeneralRe: for printing Pin
Yalcin Ozturk21-Apr-05 20:33
Yalcin Ozturk21-Apr-05 20:33 
GeneralAlignment of data within a cell Pin
Learning .NET23-Apr-05 16:37
Learning .NET23-Apr-05 16:37 
GeneralReally ,I don't see a good printing example until now Pin
Yalcin Ozturk14-Apr-05 3:31
Yalcin Ozturk14-Apr-05 3:31 
GeneralRe: Really ,I don't see a good printing example until now Pin
Predrag Dukanac14-Apr-05 6:23
Predrag Dukanac14-Apr-05 6:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.