Click here to Skip to main content
15,898,751 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 453.9K   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: 0.6.0.3 RC1 Pin
angus_grant5-Aug-05 17:10
angus_grant5-Aug-05 17:10 
GeneralRe: 0.6.0.3 RC1 Pin
angus_grant5-Aug-05 17:14
angus_grant5-Aug-05 17:14 
GeneralXSL Pin
Kastellanos Nikos19-Jul-05 23:47
Kastellanos Nikos19-Jul-05 23:47 
GeneralLocation of margins(?) changes during print Pin
BarCode14-Jul-05 8:35
BarCode14-Jul-05 8:35 
GeneralRe: Location of margins(?) changes during print Pin
Predrag Dukanac14-Jul-05 21:15
Predrag Dukanac14-Jul-05 21:15 
GeneralRe: Location of margins(?) changes during print Pin
Kyle Schultz7-Sep-05 12:10
Kyle Schultz7-Sep-05 12:10 
GeneralRe: Location of margins(?) changes during print Pin
Predrag Dukanac13-Sep-05 0:28
Predrag Dukanac13-Sep-05 0:28 
GeneralRe: Location of margins(?) changes during print Pin
Kyle Schultz13-Sep-05 5:31
Kyle Schultz13-Sep-05 5:31 
Predrag,

Thanks for the update, I can totally understand being too busy with work. The good news is, since the time that I sent this previous message, I have been able to actually work out the printer margin issue. It required a lot of frustration, a lot of late nights, and almost a whole ream of paper, but I finally got it worked out. Smile | :)

After downloading your daReport project and stepping through it line by line in run-time I was able to pin-point the problem. I took your project, then added in the PrinterBounds class that was supplied on here by Philippe Leybaert here on CodeProject.

http://www.codeproject.com/csharp/DotNetprinterbounds.asp

After including the PrinterBounds class I then modified your code to accommodate a new rectangle, as well as a boolean PreviewMode value. You see, once I got the margins to print out correctly, they then would not Preview correctly (as the PrintPreviewControl doesn't know about the hard margins, and was then shifted to the left and up).

Now, if the document is in PrintPreview mode, the document is viewed correctly, then when you go to print, I use the rectangle that I generated from PrinterBounds and print it invisible on the form (no border) then line up my left and top margins based on the upper left corner of the rectangle. I would be more than happy to send you your project back, updated of course, for you to re-post, as I do not want to post it as my work.

Another change I did was for my own specific reason's and that was to include the xml report file in my DLL (embedded) and then generate it from a stream, I then pass the stream to the setXML function that now looks for an XmlDocument and not the string filename. I am generating the XmlDocument from a System.IO.Stream and then reading it from the System.Reflection.Assembly

I then changed all your functions that were looking for "Graphics g" as a parameter to "System.Drawing.Printing.PrintPageEventArgs e". This allowed me to pass the e variable and get the PrinterBounds using its graphics (it required a PrintPageEventArgs as a parameter, and I couldn't figure a way around it). I then create your "Graphics g" variable from the "e" that I passed.

The only peice of your project that I acutally updated was the StyledTable.cs, TextField.cs, and I believe just the DaPrintDocument.cs, as I have no need for charts at this time.

I hope that you are able to understand what I did to get the entire thing working. Just below is a small code sample of what I did to get it to work (based on the StyledTable.cs file) and if you have any questions about what I did please feel free to email me anytime at kyle@ksdinfusion.com (I have not even started on my website yet, so ksdinfusion.com is only for email at this time).

The best way for you to see what I did to get this working is to do a search for "rBounds" in your project. Hope all is well with your work.

Thanks for creating such a kick @$$ report generator.

###################################################################
Code Sample from drawHeader function (only the first few lines)
##Your Code:
private int drawHeader(Graphics g)
{
int MaxLinesDrawn = 0;
int leftBorder = theRegion.X;

ArrayList[] headerLabels = new ArrayList[columns.Length];



##My Code:
private int drawHeader(System.Drawing.Printing.PrintPageEventArgs e)
{
Graphics g = e.Graphics;
PrinterBounds objBounds = new PrinterBounds(e);
rBounds = objBounds.Bounds;

int leftBorder =0;

if( DaPrintDocument.PreviewMode )
leftBorder = theRegion.X; //MAKES DOCUMENT PREVIEW CORRECTLY
else
leftBorder = rBounds.Left; //THIS WAS THE KEY!!!!!! WOOHOO!

int MaxLinesDrawn = 0;

ArrayList[] headerLabels = new ArrayList[columns.Length];

###################################################################

I hope this helps, and again, if you would like the updated project sent back to you, just let me know, and get me an email address I can send it to.

Thanks again


Kyle Schultz
KS Design
"Technology infused with Imagination"
kyle@ksdinfusion.com
GeneralRe: Location of margins(?) changes during print Pin
Predrag Dukanac14-Sep-05 1:53
Predrag Dukanac14-Sep-05 1:53 
GeneralRe: Location of margins(?) changes during print Pin
Kyle Schultz14-Sep-05 6:41
Kyle Schultz14-Sep-05 6:41 
General0.6.0.3 nightly: FormatMask editor (alpha code) Pin
angus_grant7-Jul-05 19:46
angus_grant7-Jul-05 19:46 
GeneralRe: 0.6.0.3 nightly: FormatMask editor (alpha code) Pin
Anonymous19-Jul-05 23:52
Anonymous19-Jul-05 23:52 
GeneralRe: 0.6.0.3 nightly: FormatMask editor (alpha code) Pin
angus_grant20-Jul-05 14:24
angus_grant20-Jul-05 14:24 
GeneralSourceForge 0.6.0.2 Pin
angus_grant29-Jun-05 1:16
angus_grant29-Jun-05 1:16 
GeneralRe: SourceForge 0.6.0.2 Pin
Predrag Dukanac29-Jun-05 1:51
Predrag Dukanac29-Jun-05 1:51 
GeneralRe: SourceForge 0.6.0.2 Pin
Vetch29-Jun-05 4:01
Vetch29-Jun-05 4:01 
GeneralRe: SourceForge 0.6.0.2 Pin
angus_grant29-Jun-05 15:52
angus_grant29-Jun-05 15:52 
GeneralRe: SourceForge 0.6.0.2 Pin
Vetch29-Jun-05 21:10
Vetch29-Jun-05 21:10 
GeneralRe: SourceForge 0.6.0.2 Pin
angus_grant29-Jun-05 15:54
angus_grant29-Jun-05 15:54 
GeneralReport Header and Footer Pin
Yalcin Ozturk8-Jun-05 21:54
Yalcin Ozturk8-Jun-05 21:54 
GeneralRe: Report Header and Footer Pin
angus_grant29-Jun-05 1:10
angus_grant29-Jun-05 1:10 
GeneralRe: Report Header and Footer Pin
angus_grant20-Jul-05 20:17
angus_grant20-Jul-05 20:17 
General0.6.0.2 RC1 Pin
angus_grant6-Jun-05 13:35
angus_grant6-Jun-05 13:35 
GeneralSourceForge 0.6.0.2 beta Pin
angus_grant30-May-05 15:21
angus_grant30-May-05 15:21 
GeneralPrinting Over Network Pin
Syri27-May-05 11:37
Syri27-May-05 11:37 

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.