|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionPrinting a document programmatically can be quite involved. Using the The reports are comprised of plain text sections (such as the title "Birthdays", and the other paragraphs) and grids of data from a database (more specifically, from a I do not plan to keep this page up-to-date with the latest code and documentation. This will just be a quick presentation of what you can do and the classes involved. For the latest version, see here. However, this project is not actively being developed, so use at your own risk. This was all written for the .NET 1.1 Framework, and I know a lot of new namespaces were added in .NET 2.0 that may make a lot of this obsolete. It solved a problem I had at the time. Step-by-StepThis section will take you through the process of using the Create a DataViewThe first step is to create a public static 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)});
dt.Rows.Add(new Object[] {"Louis", "Armstrong",
new DateTime(1901, 8, 4)});
dt.Rows.Add(new Object[] {"Igor", "Stravinski",
new DateTime(1882, 6, 17)});
dt.Rows.Add(new Object[] {"Bill", "Gates",
new DateTime(1955, 10, 28)});
dt.Rows.Add(new Object[] {"Albert", "Einstein",
new DateTime(1879, 3, 14)});
dt.Rows.Add(new Object[] {"Marilyn", "Monroe",
new DateTime(1927, 6, 1)});
dt.Rows.Add(new Object[] {"Mother", "Teresa",
new DateTime(1910, 8, 27)});
DataView dv = dt.DefaultView;
return dv;
}
This function will return a Create a ReportMakerThe public void MakeDocument(ReportDocument reportDocument)
{
Let's take a look at the implementation of this method step-by-step. First, it is a good idea to reset the TextStyle.ResetStyles();
Next, setup the default margins for the document, if desired. // Setup default margins for the document (units of 1/100 inch)
reportDocument.DefaultPageSettings.Margins.Top = 50;
reportDocument.DefaultPageSettings.Margins.Bottom = 50;
reportDocument.DefaultPageSettings.Margins.Left = 75;
reportDocument.DefaultPageSettings.Margins.Right = 75;
As mentioned, the // Setup the global TextStyles
TextStyle.Heading1.FontFamily = new FontFamily("Comic Sans MS");
TextStyle.Heading1.Brush = Brushes.DarkBlue;
TextStyle.Heading1.SizeDelta = 5.0f;
TextStyle.TableHeader.Brush = Brushes.White;
TextStyle.TableHeader.BackgroundBrush = Brushes.DarkBlue;
TextStyle.TableRow.BackgroundBrush = Brushes.AntiqueWhite;
TextStyle.Normal.Size = 12.0f;
// Add some white-space to the page. By adding a 1/10 inch margin
// to the bottom of every line, quite a bit of white space will be added
TextStyle.Normal.MarginBottom = 0.1f;
Using our method defined earlier, we'll get a // create a data table and a default view from it.
DataView dv = GetDataView();
// set the sort on the data view
if (myPrintDialog.cmbOrderBy.SelectedItem != null)
{
string str = myPrintDialog.cmbOrderBy.SelectedItem.ToString();
if (myPrintDialog.chkReverse.Checked)
{
str += " DESC";
}
dv.Sort = str;
}
The next step is creating an instance of the // create a builder to help with putting the table together.
ReportBuilder builder = new ReportBuilder(reportDocument);
Creating a header and footer are quite easy with the builder class's five overloaded functions. The one below creates a simple header with text on the left side (Birthdays Report) and on the right side (page #). The footer has the date centered. // 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);
Now the real fun begins: we start a vertical, linear layout because every section from here should be added below the preceding section. builder.StartLinearLayout(Direction.Vertical);
Now add two text sections. The first section added will be a heading (as defined by // Add text sections
builder.AddTextSection("Birthdays", TextStyle.Heading1);
builder.AddTextSection("The following are various birthdays of people " +
"who are considered important in history.");
Next, we add a section of a data table. The first line adds a data section with a visible header row. Then three column descriptors are added. These are added in the order that the columns are displayed. That is, The first parameter passed to // Add a data section, then add columns
builder.AddDataSection(dv, 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);
We set a format expression for the last column added (the date column). These format expressions are identical to those used by // Set the format expression to this string.
builder.CurrentColumn.FormatExpression = "{0:D}";
And the very last thing is to finish the builder.FinishLinearLayout();
}
Create a Form for PrintingThere are only a handful of controls on the following form: a label, a combo box, a check box, and a usercontrol from
This form also has an instance of the private ReportDocument reportDocument;
public ReportPrinting.PrintControl PrintControls;
public System.Windows.Forms.ComboBox cmbOrderBy;
public System.Windows.Forms.CheckBox chkReverse;
public SamplePrintDialog1()
{
InitializeComponent();
this.reportDocument = new ReportDocument();
this.PrintControls.Document = reportDocument;
SampleReportMaker1 reportMaker = new SampleReportMaker1(this);
this.reportDocument.ReportMaker = reportMaker;
this.cmbOrderBy.Items.Clear();
this.cmbOrderBy.Items.Add("FirstName");
this.cmbOrderBy.Items.Add("LastName");
this.cmbOrderBy.Items.Add("Birthdate");
}
In this constructor, an instance of You're FinishedThe above code prints a fairly simple document. Just to note, you can use the standard This entire sample can be found in the download of the Report Document ClassesThere are several classes introduced into the
ReportDocument
The The strategy design pattern is employed for formatting the report. An object implementing ReportSection
In the sample report shown at the top of this article, there is a paragraph of text followed by a table of data. (There are actually two paragraphs of text, one of which is a heading. Plus there is a page header, but we'll ignore all that for now.) We would create a SectionContainerThis abstract class defines a container of sections. There are two types provided with the framework: LinearSectionsThe As its name implies, it lays sections out linearly -- that is, in a row or in a column. A property named LayeredSectionsThe The child sections of a ReportSectionTextThe It is interesting to note that the ReportSectionDataThe ReportDataColumnThe The TextStyleThe For example, a new style can be defined using TextStyle paragraphStyle = new TextStyle(TextStyle.Normal);
paragraphStyle.Bold = true;
It will have all the same properties as TextStyle.Normal.Size += 1.0f
ReportBuilder
To instantiate a We've already seen an example of using IReportMaker
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 implement the ConclusionThat summarizes the Revision History
| ||||||||||||||||||||