Simple .NET ReportLibrary
By Fons Sonnemans (June 2002)
Download
ReportLibrary.zip
Introduction
Printing in .NET is greatly improved when compared with Visual Basic 6.0. In my
projects I almost never use external reporting tools like Crystal Reports. They
are too large and complex for my needs. In this article I demonstrate the use of
the ReportLibrary which I developed. It can be used to create simple reports which
can be printed and previewed.
ReportLibrary
The ReportingLibrary assembly contains a Report class. A report has a collection
of items of the type IReportItem. You buildup your report by adding Items to the
report. An item can be a ReportGroup, ReportText, ReportHorizontalLine or ReportPageBreak.
A report has a Title and SubTitle which are printed in the default pageheader.
ReportGroup class
A ReportGroup is used to group items together. All items within the group
are normally printed on the same line (Y coordinate), you can change this behavior
by setting the FixedTop property to 'false'.
ReportText class
A ReportText is used to print a string on a specific X (Left) coordinate.
You can use the 'Center' or 'Right' alignment when the Width property is set
to a non-zero value.
A ReportGroup and ReportText can have a BackColor and a BorderPen which only have
meaning when the Height property is set to a non-zero value.
Usage

The following code creates the report which preview is shown in the top
of this article.
private void buttonPreview_Click(object sender,
System.EventArgs e)
{
using
(Report r = CreateReport())
{
r.Preview();
}
}
private void buttonPrint_Click(object sender,
System.EventArgs e)
{
using
(Report r = CreateReport())
{
r.Print(new PrintDialog());
}
}
private Report CreateReport()
{
Report r = new Report();
r.DefaultPageSettings.Landscape =
(checkBoxLandscape.Checked);
r.Title
= textBoxTitle.Text;
r.SubTitle
= textBoxSubTitle.Text;
r.NewPage
+= new
NewPageHandler(ReportNewPage);
int
rows = Convert.ToInt32(numericUpDownRows.Value);
for (int t =
1; t <=
rows; t++)
{
ReportGroup g
= new ReportGroup();
r.Items.Add(g);
ReportText s
= new ReportText(t.ToString(), 10,
70);
s.Alignment
= HorizontalAlignment.Right;
g.Items.Add(s);
s =
new ReportText("Some Text",
100);
g.Items.Add(s);
}
r.Items.Add(new ReportHorizontalLine(0,
r.Width, 2));
return r;
}
Custom Page Headers
You can use the default page header or create your own. The NewPage event is triggered
each time a page is printed.
private void ReportNewPage(object
sender, ReportItemCollection
items, int
page)
{
if
(!checkBoxPageHeader.Checked)
items.Clear();
ReportGroup g =
new ReportGroup(0,
((Report)sender).Width, 20);
g.BorderPen
= new Pen(Brushes.Gray,
2);
g.BackColor
= Brushes.LightGray;
g.Margin.Top
= 2;
items.Add(g);
ReportText s = new ReportText("Column1", 10,
70);
s.Alignment
= HorizontalAlignment.Right;
g.Items.Add(s);
s =
new ReportText("Column2", 100);
g.Items.Add(s);
items.Add(new ReportGroup(5));
}
All ReportItems added to the items collection are used in the pageheader.
Conclusion
The ReportLibrary is an easy solution when you want to reports buildup from
code in .NET. You can off course extend it by creating your own classes which implement
the IReportItem interface. This makes it a very powerful solution.
Any suggestions and feedback for improving this article is most welcome. Send your
suggestions and feedback to Fons.Sonnemans@reflectionit.nl