About Me

My photo
Software Developer and ideas man!

Wednesday, January 5, 2011

Crystal Reports - How to insert a logo into the Report Header at runtime

Now how hard can it be to insert a picture into the report header of a Crystal Report at runtime - apparently this is not a straightforward exercise and I implore the folks at SAP to look at this as an area for improvement.

Anyway,  I wanted the users of our reports to be able to customize the business logo they used on the reports before they generate the report - I thought this would be easily done by passing the image as a report parameter in some way, but I thought wrong.  After much searching around and reading copious amounts of posts from other's many of which led to the backstreets of nowhere, I developed the following 8 easy steps.


1. Create a dataset,

2. Create a table in that dataset,

3. Add a column to the table that will store the image

4. Define an xml schema for that table

5. Read in the image as a byte array, to the dataset

6. Set the dataset as the datasource for the report.

7. Add in the logo->image field to the Crystal Report

8. Create the dataset connection in the Crystal Report Database Expert based on the xsd created in the code below [check out the last line] and insert logo blob field on report.


Below is the C# code developed using Visual Studio 2010 and SAP Crystal Reports for Visual Studio 2010:

private void Form1_Load(object sender, EventArgs e)
        {
            DataSet ReportDS = new DataSet();

            //Create a "Logo" Table and insert an image column
            DataTable logoTable = new DataTable("Logo");
            logoTable.Columns.Add(new DataColumn("Image", typeof(System.Byte[])));

            //Add table to the dataset to be associated with the report
            ReportDS.Tables.Add(logoTable);

            //Add a row to our new table
            DataRow logoDR = ReportDS.Tables["Logo"].NewRow();

            //Only required to generate a matching xsd for use by the Crystal Report Field Explorer!
            ReportDS.WriteXmlSchema(@"c:\temp\test.xsd");

            //Read in the image file to a filestream
            FileStream fileStream = new FileStream(@"c:\temp\test.bmp", FileMode.Open);

            BinaryReader binaryReader = new BinaryReader(fileStream);
            logoDR["Image"] = binaryReader.ReadBytes((int)binaryReader.BaseStream.Length);

            ReportDS.Tables["Logo"].Rows.Add(logoDR);

            fileStream.Close();
            binaryReader.Close();

            this.CrystalReport11.SetDataSource(ReportDS);
        }




The Test.xsd generated by the .WriteXmlschema() above should look like this - 


Now it is time to place the Logo Blob field in the Report Header.
i.) From the Field Explorer->Database Fields->Database Expert, create a new ADO.NET(xml) connection. 
 
ii). From the Database Expert-> Available Datasources, select the newly created datasource and move the table over to the "Selected Tables" listbox.


iii). Under the "Database Fields" in the "Field Explorer" should be the new table called "logo"plus a field called "image" which can be dragged into the Report Header.


...And that's it...


More from this Blogger:

1 comment:

  1. Hi, thanks for this article but on my side, I have a logo and a stored procedure which return some rows. And it seems that the SQL query related to the image in the report header is repeated many times... Do you know why ? Thanks a lot

    ReplyDelete