Click here to Skip to main content
15,885,899 members
Articles / Programming Languages / C#

Visual Reporting with .NET 2003 (C#)

Rate me:
Please Sign up or sign in to vote.
4.61/5 (8 votes)
27 Oct 2006CPOL1 min read 52.3K   429   39   5
Generate report files with your own controls.

Sample Image

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:

C#
//
// Init main object & root node background attributes
//
VisualReport    vr = new VisualReport (320,200);//set size ofroot node
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:
C#
// Create a new object and link it to 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);

// Create a new object and link it to root node
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:

C#
// Export to XML file
string strFileName = "./output.xml";
vr.SaveXml (strFileName);
vr.Draw ();
vr.GlobalBitmap.Save ("./output.bmp", 
                      System.Drawing.Imaging.ImageFormat.Bmp);

// Reload XML and save it to compare
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionproblem with downloading Pin
nagyist25-May-09 20:01
nagyist25-May-09 20:01 
AnswerRe: problem with downloading Pin
Vincent DUVERNET (Nolmë Informatique)28-May-09 0:21
Vincent DUVERNET (Nolmë Informatique)28-May-09 0:21 
GeneralRe: problem with downloading Pin
nagyist7-Jun-09 20:32
nagyist7-Jun-09 20:32 
GeneralPDFSharp Pin
Paul Selormey27-Oct-06 23:54
Paul Selormey27-Oct-06 23:54 
GeneralRe: PDFSharp Pin
Vincent DUVERNET (Nolmë Informatique)28-Oct-06 5:02
Vincent DUVERNET (Nolmë Informatique)28-Oct-06 5:02 

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.