ReportBuilder
ReportBuilder
assists with the building of a report. This class is the main interface between your code and the ReportPrinting library. In many cases, you will never explicitly create any of the above objects. Instead, the ReportBuilder
will create them for you.
To instantiate a ReportBuilder
, you must provide the ReportDocument
to be built. Then you can call its various Add methods to sequentially add pieces to a report document.
Example:
The following example shows the creation of a report using the ReportBuilder. The following methods would be part of a class that implements IReportMaker
(this is from example1 in the sample project).
private DataView GetDataView()
{
DataTable dt = new DataTable("People");
dt.Columns.Add("FirstName", typeof(string));
dt.Columns.Add("LastName", typeof(string));
dt.Columns.Add("Birthdate", typeof(DateTime));
dt.Rows.Add(new Object[] {"Theodore", "Roosevelt", new DateTime(1858, 11, 27)});
dt.Rows.Add(new Object[] {"Winston ", "Churchill", new DateTime(1874, 11, 30)});
dt.Rows.Add(new Object[] {"Pablo", "Picasso", new DateTime(1881, 10, 25)});
dt.Rows.Add(new Object[] {"Charlie", "Chaplin", new DateTime(1889, 4, 16)});
dt.Rows.Add(new Object[] {"Steven", "Spielberg", new DateTime(1946, 12, 18)});
dt.Rows.Add(new Object[] {"Bart", "Simpson", new DateTime(1987, 4, 19)});
return dt.DefaultView;
}
public void MakeDocument(ReportDocument reportDocument)
{
// Clear the document
reportDocument.ClearSections();
// create a data table and a default view from it.
DataView dataView = this.GetDataView();
// create a builder to help with putting the table together.
ReportBuilder builder = new ReportBuilder(reportDocument);
// Add a simple page header and footer that is the same on all pages.
builder.AddPageHeader("Birthdays Report", String.Empty, "page %p");
builder.AddPageFooter(String.Empty, DateTime.Now.ToLongDateString(), String.Empty);
builder.StartLinearLayout(Direction.Vertical);
// Add text sections
builder.AddTextSection("Birthdays", TextStyle.Heading1);
builder.AddTextSection("The following are various birthdays of people who "
+ "are considered important in history.");
// Add a data section, then add columns
builder.AddDataSection(dataView, true);
builder.AddColumn ("LastName", "Last Name", 1.5f, false, false);
builder.AddColumn ("FirstName", "First Name", 1.5f, false, false);
builder.AddColumn ("Birthdate", "Birthdate", 3.0f, false, false);
// Set the format expression to this string.
builder.CurrentColumn.FormatExpression = "{0:D}";
builder.FinishLinearLayout();
}
IReportMaker
IReportMaker
is an interface used to implement the strategy design pattern. An object that implements IReportMaker
can be added to a ReportDocument
. When the document is about to be printed, it automatically calls the single method MakeDocument()
. The above example shows an implementation of that method to print a one-page report.
For example, you could have an application that can print either detailed reports or a shorter overview. The logic to make each of these reports would be located in separate classes. Each class would implementing the IReportMaker
interface. Your print dialog could have a "Print What" combo box to allow the user to select the type of report, and use the selection in the combo box to associate the correct implementation of IReportMaker
with the ReportDocument
.
Print Dialogs
The print dialog guides the user through the printing process. Most applications have some options that affect what is printed and how it is printed. Most windows applications customize the standard PrintDialog
, adding an additional section at the bottom for various options. There are articles on extending the standard PrintDialog
using MFC, but Ive yet to find anything for .NET. If someone creates a .NET control that looks like a standard PrintDialog
and could easily be added to new Forms to create a customized PrintDialog
or knows of some other way to extend the functionality of the .NET PrintDialog
, please let me know.
PrintControl
To make printing easy for my applications, I created this very basic control that can be dropped onto any form. It gives the user options to setup, preview, submit (ok) or cancel. Providing a preview button and a page setup button on a print dialog are not standard in the windows interface, but I wish they were. So this control provides that functionality to your print dialog. Note, you can still provide access via a File menu (File | Print Preview, File | Page Setup).
The control uses the following dialogs associated with printing:
-
PrintPreviewDialog
-
PageSetupDialog
-
PrintDialog
To use the print control, place it on a form. Set the Document
property to a valid PrintDocument
. (it doesnt have to just be the ReportDocument
described earlier). Thats it!
You can customize a few things with the following properties:
-
ShowStatusDialog
- The progress of the print job is shown in a status dialog. Default is true. -
PrintInBackground
- Indicates that printing should be done in the background. Default is true. -
Printing
- This event is raised prior to printing. It allows user code to setup for printing. (This is useful for dumping data from the GUI to a helper class, for instance).
Print Dialog with PrintControl
A sample form with a PrintControl is shown below. This dialog allows a user to select tables to print from the Northwind sample database.
Comments