Introduction
Generating report is used very often, CodeProject has some samples, but not always with source code. In my case, I needed to generate some reports with charts. My first article was a first try about it. Another article on XML serialization has been extended to support most of the common objects. At least, for PDF generation, I've tried many solutions (SharpPDF, PDFSharp, Report.NET,iTextSharp) and kept iTextSharp.
Here is a list of controls:
Label
(text, image, colors, gradients, text formatting, anti-aliasing, markings and clipping)
Grid
Grid3d
Chart
Ownerdraw
Shape
(using GraphicsPath
)
Table
(with cells and rows)
Background
You need to download and install iTextSharp (itextsharp-3.1.5 for this version) in order to run some samples.
Using the code
As usual, many samples are brief descriptions of how to use the article or code. Tutorial 1 to 7 generate a bitmap file and XML file in the Debug or Release output directory which contain the rendering of objects.
All objects are derived from VisualObject
and each of these have a collection of children. The root object contains the main rendering information (text, background, margins, clipping, smoothing...).
The main object (VisualReport
) contains a root node (VisualObject
). Here is the first initialization part (very easy) from Tutorial 1:
VisualReport vr = new VisualReport (320,200);
vr.RootObject.BackColor = System.Drawing.Color.WhiteSmoke;
vr.RootObject.BackColor2 =
System.Drawing.Color.FromArgb (242,205,242);
vr.RootObject.GradientMode =
System.Drawing.Drawing2D.LinearGradientMode.Vertical;
Here is the hierarchy generation, we're adding two objects to the root node:
VisualLabel vlLabel = new VisualLabel ();
vlLabel.Init (90, 10, 200, 90);
vlLabel.Text = "Hellow World";
vlLabel.TextColor =
System.Drawing.Color.FromArgb (108,91,151);
vlLabel.TextFont = new System.Drawing.Font("Arial",
20F, FontStyle.Bold);
vlLabel.TextShadowColor = System.Drawing.Color.Black;
vlLabel.TextShadowDistanceX = 1;
vlLabel.TextShadowDistanceY = 1;
vlLabel.TextSmoothMode =
System.Drawing.Text.TextRenderingHint.AntiAlias;
vr.RootObject.AddChild (vlLabel);
VisualLabel vlLabel2 = new VisualLabel ();
vlLabel2.Init (90, 110, 200, 90);
vlLabel2.Text = "Hellow World";
vlLabel2.TextColor =
System.Drawing.Color.FromArgb (108,91,151);
vlLabel2.TextFont =
new System.Drawing.Font("Arial", 20F, FontStyle.Bold);
vlLabel2.TextSmoothMode =
System.Drawing.Text.TextRenderingHint.SystemDefault;
vr.RootObject.AddChild (vlLabel2);
The final part contains XML exporting and bitmap saving:
string strFileName = "./output.xml";
vr.SaveXml (strFileName);
vr.Draw ();
vr.GlobalBitmap.Save ("./output.bmp",
System.Drawing.Imaging.ImageFormat.Bmp);
VisualReport vr2 = new VisualReport ();
vr2.LoadXml (strFileName);
vr2.SaveXml ("output2.xml");
vr2.Draw ();
vr2.GlobalBitmap.Save ("./output2.bmp",
System.Drawing.Imaging.ImageFormat.Bmp);
Bug Report
There are a few bugs or missing code for the following cases:
- Cloning objects with hierarchy
- Drawing charts with specific rendering mode
- Export to XML doesn't save custom shapes graphic data, image streams, and custom objects
History
- 2006-10-18: First version.