Click here to Skip to main content
15,568,045 members
Articles / Programming Languages / C#
Article
Posted 6 Apr 2005

Stats

431.4K views
6K downloads
169 bookmarked

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

 
PraisePrint without XML Pin
Member 92447718-Dec-18 0:54
Member 92447718-Dec-18 0:54 
PraiseXML Generation - Easy Pin
Member 92447716-Dec-18 9:29
Member 92447716-Dec-18 9:29 
I found this is very good for one page report print. Little difficult for XML generation.
Attached class is a helper which returns the necessary XML. With sample code to call the
XML class.


using System;
using System.Collections.Generic;
using System.Text;

namespace ReportTest
{
public class ReportGen
{


public string CreateHeader()
{
string Header;
string XML;

XML = @"<daReport papersize=" + "\"A4\"" + " layout=" + "\"Portrait\"" + ">";
XML = XML + @"<margins left=" + "\"50\"" + " top=" + "\"50\"" + " right=" + "\"50\"" + " bottom=" + "\"50\"" + "></margins>" + Environment.NewLine;
XML = XML + @"<parameters> <parameter name=" + "\"author\"" + "/></parameters>" + Environment.NewLine + "<content>" + Environment.NewLine;

Header = XML;

return Header;
}
public string CreateFooter()
{
string Footer;
string XML;

XML = @"<dynamicContent>" + Environment.NewLine;
XML = XML + @"<textField x=" + "\"476\"" + " y=" + "\"1066\"" + " width=" + "\"300\"" + " height=" + "\"30\"" + " horAlignment=" + "\"None\"" + " verAlignment=" + "\"None\"" + " Selectable=" + "\"True\">";
XML = XML + @"<text horAlignment=" + "\"Right\"" + " verAlignment=" + "\"Top\">" + "Page $P{pageNumber} of $P{totalPages}</text>" + "<font family=" + "\"Tahoma\"";
XML = XML + @" size=" + "\"10\"" + "></font>" + "<foregroundColor color=" + "\"Black\"" + "></foregroundColor><backgroundColor color=" + "\"Transparent\"" + "></backgroundColor>";
XML = XML + @" <border width=" + "\"0\"" + " color=" + "\"Black\"" + "></border></textField>" + Environment.NewLine + "</dynamicContent>";
XML = XML + Environment.NewLine;
XML = XML + @"</content> " + Environment.NewLine + "</daReport>";

Footer = XML;

return Footer;
}

public string AddTextField(int x, int y, int Height, int Width, string DisplayText, string FontName, string FontStyle, int FontSize, string hAlignment, string vAlignment)
{
string XML;
XML = @"<textField x=" + "\"" + x + "\" y=\"" + y + "\"" + " height=\"" + Height + "\" width=\"" + Width + "\" verAlignment=\"None\"" + " horAlignment=\"None\"" + " Selectable=\"True\">";
XML = XML + @"<text verAlignment=" + "\"" + vAlignment + "\" horAlignment=\"" + hAlignment +"\">" + DisplayText + "</text> <font family=\"" + FontName + "\" style=\"" + FontStyle + "\" size=\"" + FontSize + "\"></font>";
XML = XML + @" <foregroundColor color= " + "\"Black\"></foregroundColor> <backgroundColor color=\"Transparent\"></backgroundColor> <border width=\"0\" color=\"Black\"></border></textField>";
XML = XML + Environment.NewLine;

return XML;
}

public string AddPictureBox(int x, int y, int Height, int Width, string FileName, string BorderColor, int BorderWidth)
{
string XML;
XML = @"<pictureBox x=" + "\"" + x + "\" y=\"" + y + "\" height=\"" + Height + "\" width=\"" + Width + "\" stretch=\"true\" verAlignment=\"None\"" + " horAlignment=\"None\"" + " Selectable=\"False\">";
XML = XML + @"<file>"+ FileName+"</file>";
XML = XML + @"<border width=" + "\"" + BorderWidth + "\" color=\"" + BorderColor +"\"></border>";
XML = XML + @"</pictureBox>" + Environment.NewLine;
return XML;
}

public string AddLine(int x, int y, int Height, int Width, string DisplayText, string FontName, string FontStyle, int FontSize, string hAlignment, string vAlignment)
{
string XML;

DisplayText = string.Empty;
Height = 1;
XML = @"<textField x=" + "\"" + x + "\" y=\"" + y + "\"" + " height=\"" + Height + "\" width=\"" + Width + "\" verAlignment=\"None\"" + " horAlignment=\"None\"" + " Selectable=\"True\">";
XML = XML + @"<text verAlignment=" + "\"" + vAlignment + "\" horAlignment=\"" + hAlignment + "\">" + DisplayText + "</text> <font family=\"" + FontName + "\" style=\"" + FontStyle + "\" size=\"" + FontSize + "\"></font>";
XML = XML + @" <foregroundColor color= " + "\"Black\"></foregroundColor> <backgroundColor color=\"Black\"></backgroundColor> <border width=\"0\" color=\"Black\"></border></textField>";
XML = XML + Environment.NewLine;

return XML;
}

public string AddBox(int x, int y, int Height, int Width, string DisplayText, string FontName, string FontStyle, int FontSize, string hAlignment, string vAlignment)
{
string XML;

DisplayText = string.Empty;
XML = @"<textField x=" + "\"" + x + "\" y=\"" + y + "\"" + " height=\"" + Height + "\" width=\"" + Width + "\" verAlignment=\"None\"" + " horAlignment=\"None\"" + " Selectable=\"True\">";
XML = XML + @"<text verAlignment=" + "\"" + vAlignment + "\" horAlignment=\"" + hAlignment + "\">" + DisplayText + "</text> <font family=\"" + FontName + "\" style=\"" + FontStyle + "\" size=\"" + FontSize + "\"></font>";
XML = XML + @" <foregroundColor color= " + "\"Black\"></foregroundColor> <backgroundColor color=\"Transparent\"></backgroundColor> <border width=\"1\" color=\"Black\"></border></textField>";
XML = XML + Environment.NewLine;

return XML;
}


}
}

GeneralMy vote of 1 Pin
raiserle1-Jul-18 7:26
raiserle1-Jul-18 7:26 
GeneralRe: My vote of 1 Pin
raiserle1-Jul-18 7:33
raiserle1-Jul-18 7:33 
QuestionMalware? Pin
Pushaw13-Jun-18 5:11
Pushaw13-Jun-18 5:11 
SuggestionVery nice ! Pin
Gluups8-Jun-18 10:46
Gluups8-Jun-18 10:46 
GeneralMy vote of 5 Pin
raiserle30-Dec-17 9:17
raiserle30-Dec-17 9:17 
Newswww.stampa-reports.com Pin
Predrag Dukanac7-Dec-12 4:14
Predrag Dukanac7-Dec-12 4:14 
GeneralRe: www.stampa-reports.com Pin
Tajniak511-Mar-13 5:57
Tajniak511-Mar-13 5:57 
QuestionStampa Reports Pin
Aciobanita Costel18-Oct-12 2:30
Aciobanita Costel18-Oct-12 2:30 
GeneralMy vote of 5 Pin
biye5u1-Mar-12 21:08
biye5u1-Mar-12 21:08 
QuestionThanks a lot for this nice utility. Pin
Sunil Apte13-Oct-11 22:02
Sunil Apte13-Oct-11 22:02 
Questionparameter in static table Pin
MCY5-Dec-10 2:43
professionalMCY5-Dec-10 2:43 
GeneralMy vote of 5 Pin
Anirudha_Gohokar5-Jul-10 0:22
Anirudha_Gohokar5-Jul-10 0:22 
QuestionMem usage problem!!!!!!!!!!! [Report design] Pin
utehn9-Jan-09 0:37
utehn9-Jan-09 0:37 
QuestionUSE FREE Pin
Alessandro Arnoldi31-Dec-08 6:00
Alessandro Arnoldi31-Dec-08 6:00 
AnswerRe: USE FREE Pin
fraser12530-Jun-12 18:51
fraser12530-Jun-12 18:51 
Generalprinters on c#.net Pin
balu1234530-Jul-08 8:41
balu1234530-Jul-08 8:41 
QuestionHow can I draw text or table only at the end of last page? Pin
JM ® - DE CASTRO11-Jun-08 12:58
JM ® - DE CASTRO11-Jun-08 12:58 
GeneralVery nice Pin
Hugo Faselo13-Dec-07 12:16
Hugo Faselo13-Dec-07 12:16 
QuestionRe: Very nice Pin
dimazoid16-Oct-08 9:34
dimazoid16-Oct-08 9:34 
QuestionMonday's date at the top of the report. Pin
damonhogan16-Nov-07 6:48
damonhogan16-Nov-07 6:48 
Questionmultple page in staticContent [modified] Pin
abhishek nath5-Feb-07 21:28
abhishek nath5-Feb-07 21:28 
AnswerRe: multple page in staticContent Pin
negleden25-May-09 15:46
negleden25-May-09 15:46 
QuestionWhat royalties do i need to add into the help file? Pin
Support12331-Oct-06 20:50
Support12331-Oct-06 20:50 

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.